Skip to content

Fix comment right after a string literal - #320

Merged
nilproject merged 1 commit into
nilproject:developfrom
xpg934:fix/comment-after-string-literal
Oct 31, 2026
Merged

nilproject merged 1 commit into
nilproject:developfrom
xpg934:fix/comment-after-string-literal

Conversation

@xpg934

@xpg934 xpg934 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

A comment that directly follows a string literal is not removed and breaks parsing:

var a = 'x'// comment          // SyntaxError: Unexpected token
var a = 'x'/* comment */       // SyntaxError: Unexpected token
var phone = '7900'//contact.phone ? contact.phone.replace(/\D/g, '') : 0;
                               // ArgumentException: Invalid flag in regexp definition

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.RemoveComments calls ValidateString, which on success already moves index past the closing quote, and then unconditionally does index++. So the first character after a string literal is skipped. If it is the / of a comment, SkipComment no 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 and ShouldProduceCorrectResult), after — only ShouldProduceCorrectResult, which fails the same way without this change.

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.
@nilproject

Copy link
Copy Markdown
Owner

Thanks!

@nilproject
nilproject merged commit 881a1a5 into nilproject:develop Oct 31, 2026
1 check passed
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.

2 participants