Skip to content

fix: prevent comment continuation when '//' appears inside strings#307798

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/comment-continuation-in-strings
Open

fix: prevent comment continuation when '//' appears inside strings#307798
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/comment-continuation-in-strings

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

When pressing Enter on a line containing // inside a string literal (e.g. '//this is a string' or "http://localhost"), VS Code incorrectly treats it as a line comment and adds // on the next line.

For example:

let string = '//this is a string'.charAt(0)
// Pressing Enter after '.charAt(0)' produces:
let string = '//this is a string'.charAt(0)
// <-- unwanted comment continuation

Similarly with URLs:

axios.get("http://localhost/index").then()
// Pressing Enter produces:
axios.get("http://localhost/index").then()
// <-- unwanted comment continuation

Closes #241802

What is the new behavior?

Comment markers (//, /*, */) are now stripped from string and regex tokens during indentation context processing, just like brackets already are. This prevents the onEnter comment continuation rule from matching // that appears inside string literals, while preserving correct comment continuation behavior for actual line comments.

Additional context

Root cause: The IndentationLineProcessor.getProcessedTokens method already removes brackets from string/regex/comment tokens to prevent bracket-based indentation rules from being triggered by brackets inside strings. However, it did not remove comment markers from string/regex tokens, so the regex-based onEnter comment continuation rule could match // that appeared inside string content.

Fix approach: Added comment marker stripping (line comment token, block comment start/end tokens from the language configuration) for string and regex tokens, alongside the existing bracket stripping. This is a minimal, targeted fix that follows the same architectural pattern already used for brackets.

Files changed:

  • src/vs/editor/common/languages/supports/indentationLineProcessor.ts — Strip comment markers from string/regex tokens
  • src/vs/editor/contrib/indentation/test/browser/indentationLineProcessor.test.ts — Added tests for the fix

When pressing Enter on a line containing '//' inside a string literal
(e.g. '//this is a string' or "http://localhost"), VS Code incorrectly
treated it as a line comment and added '//' on the next line. This is
because the indentation context processor only stripped brackets from
string/regex tokens but not comment markers, allowing the onEnter
comment continuation rule to match '//' within strings.

Strip comment markers (line comment token, block comment start/end) from
string and regex tokens during indentation context processing, alongside
the existing bracket stripping. This prevents any comment-like sequences
inside strings from triggering comment continuation or other
comment-aware onEnter rules.

Closes microsoft#241802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Enter Press After ‘//’ Breaks Line and Converts Code to Comment in JavaScript

2 participants