Skip to content

fix(CodeWindow): don't treat // inside a string as a comment#7

Open
chinesepowered wants to merge 1 commit into
stackblitz:mainfrom
chinesepowered:codewindow-string-comment
Open

fix(CodeWindow): don't treat // inside a string as a comment#7
chinesepowered wants to merge 1 commit into
stackblitz:mainfrom
chinesepowered:codewindow-string-comment

Conversation

@chinesepowered

Copy link
Copy Markdown

What

CodeWindow's syntax highlighter finds a trailing line comment with:

const ci = line.indexOf('//');

That also matches // inside a string literal. So a perfectly normal line like:

const url = 'https://example.com'

gets split at the // inside the string — everything from //example.com' onward (including the closing quote) is rendered as a grey cw-com comment, and the string is no longer highlighted correctly.

Fix

Scan the line for the first // that occurs outside any string literal (single, double, or backtick, with escape handling), via a small commentStart helper, and use that index instead of indexOf('//').

Lines with no code-level comment, and comments that genuinely trail code, are unaffected.

Changes

  • src/components/CodeWindow.tsx — add commentStart(line); use it in hl() in place of line.indexOf('//').

Verification

  • npx tsc --noEmit
  • npm run build

hl() located the line comment with line.indexOf('//'), which matches // inside string literals too. A line like const url = 'https://example.com' was split at the // inside the string, so the rest of the string (and its closing quote) rendered as a grey comment and the string highlighting broke. Scan for the first // that is outside any string literal instead.
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.

1 participant