Repository navigation
Fix comment right after a string literal - #320
Merged
nilproject merged 1 commit intoOct 31, 2026
Merged
Conversation
Parser.RemoveComments always advanced the index after ValidateString,
which had already moved it past the closing quote. The first character
after a string was skipped, so a comment directly following a string
literal ('x'// comment, 'x'/* comment */) was not removed and was later
parsed as a division or a regular expression literal.
Owner
|
Thanks! |
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.
A comment that directly follows a string literal is not removed and breaks parsing:
The same code with a space or a semicolon before the comment (
'x' // comment,'x';// comment) works, as does a comment right after a number or an identifier (1// comment). Reproduced on 2.5.1674, 2.5.1699 and 2.6.1722.Cause
Parser.RemoveCommentscallsValidateString, which on success already movesindexpast the closing quote, and then unconditionally doesindex++. So the first character after a string literal is skipped. If it is the/of a comment,SkipCommentno longer sees the comment, the scan stops at the next/(taken for a regexp), and the parser later treats the comment as a division or a regexp literal.Fix
Advance the index only when no string was consumed.
Tests
Added
ParserTests.CommentRightAfterStringLiteral(6 cases, all failing before the fix). Full suite on net10.0 with the T4-generated tests: before — 7 failed (the 6 new ones andShouldProduceCorrectResult), after — onlyShouldProduceCorrectResult, which fails the same way without this change.