fix(CodeWindow): don't treat // inside a string as a comment#7
Open
chinesepowered wants to merge 1 commit into
Open
fix(CodeWindow): don't treat // inside a string as a comment#7chinesepowered wants to merge 1 commit into
chinesepowered wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
CodeWindow's syntax highlighter finds a trailing line comment with:That also matches
//inside a string literal. So a perfectly normal line like:gets split at the
//inside the string — everything from//example.com'onward (including the closing quote) is rendered as a greycw-comcomment, 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 smallcommentStarthelper, and use that index instead ofindexOf('//').Lines with no code-level comment, and comments that genuinely trail code, are unaffected.
Changes
src/components/CodeWindow.tsx— addcommentStart(line); use it inhl()in place ofline.indexOf('//').Verification
npx tsc --noEmit✓npm run build✓