Skip to content

JS: a semicolon after a newline still ends the statement - #139

Merged
tdewolff merged 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-semicolon-after-newline
Nov 16, 2026
Merged

tdewolff merged 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-semicolon-after-newline

Conversation

@bardrop

@bardrop bardrop commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

A ; on the line after a statement was turned into an empty statement, so these valid programs failed to parse (and minify refused them):

if (a) b()
; else c()          // unexpected else in expression

do a()
; while (b)         // expected while instead of ; in do-while statement

parseStmt only consumed the trailing ; when there was no line terminator before it. ASI only inserts a semicolon when the next token is not allowed by the grammar, and ; is allowed, so the ; is the terminator of the statement, not a new empty statement. Node, V8 and the spec agree (if (a) return\n; else b is valid too).

The change keeps that !p.prevLT check for blocks and other compound statements, so {}\n; still gives a block plus an empty statement (the case from dead295). For statements that end with a semicolon (expression, var/let/const, return, break, continue, throw, debugger, do-while, directive) the ; is now consumed after a newline as well.

Found by running the parser over tc39/test262-parser-tests: the pass/ files 209fc98bea7b9d67, 3c0c251ad455218d, 4efcd175a5db8b47 and 639b9076cc56e57c failed before and parse now. The fail/ and early/ results are unchanged.

Tests: 9 new cases in TestJS; 8 fail before the change, and a\n;\n; changes from a; ; ; to a; ;. go test ./... passes, and minify's go test ./... passes against this branch. With it:

$ printf 'if(a)b()\n;else c()\ndo d()\n;while(e)\n' | minify --type=js
a?b():c();do d();while(e)

parseStmt only consumed a trailing `;` when no line terminator came
before it, so in `if (a) b\n; else c` and `do a()\n; while (b)` the `;`
became an empty statement and the `else` / `while` failed to parse.
ASI only inserts a semicolon when the next token is not allowed, so for
statements that end with a semicolon (expression, var, return, break,
continue, throw, debugger, do-while) the `;` is their terminator.
Blocks and other statements keep the old behaviour (`{}\n;` is still a
block and an empty statement).
@tdewolff
tdewolff merged commit a93a75d into tdewolff:master Nov 16, 2026
2 checks 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