Spec issues & errata · defect of the text · docs/spec-issues/57-asi-for-lexical-header.md
57. ASI in for ( LexicalDeclaration … ) headers, left-to-right prefix order, and [no LineTerminator here] in lookahead sets
- Location
- §12.10.1 Rules of Automatic Semicolon Insertion (
#sec-rules-of-automatic-semicolon-insertion), overriding condition; §14.7.4 TheforStatement (#sec-for-statement); §5.1.5 The Syntactic Grammar (#sec-syntactic-grammar); §5.1.6.7 Lookahead Restrictions (#sec-lookahead-restrictions). - Kind
- missing restriction and grammar-formalism gap (
ForStatement'sLexicalDeclarationowns the first header;, §5.1.5's declarative ASI conflicts with §12.10.1's left-to-right rule, and[no LineTerminator here]inside a lookahead set is undefined in §5.1.6.7). - Model erratum
ForLexicalHeaderASI, baked-in SpecTec reading test262 (layerparser,ForHeadersExplicit,ValidASI, andLookItem.noLTinspec/spectec/prose/04-derives.spectec). SpecTec forbids virtual;as the final token of aLexicalDeclarationchild ofForStatement, formalizes left-to-right maximal viable prefix ASI, and definesLookItem.noLTinside lookahead sequences.- Upstream
- Checked 2026-11-19: none found (searched for "header of a
forstatement" with "LexicalDeclaration"). - On this site
ForLexicalHeaderASIon Spec issues & errata (defect of the text; SpecTec bakes in the test262 reading)
Text
§12.10.1:
However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement or if that semicolon would become one of the two semicolons in the header of a
forstatement
§14.7.4:
ForStatement[Yield, Await, Return] :
for([lookahead !=let[] Expression[~In, ?Yield, ?Await]?;Expression[+In, ?Yield, ?Await]?;Expression[+In, ?Yield, ?Await]?)Statement[?Yield, ?Await, ?Return]for(varVariableDeclarationList[~In, ?Yield, ?Await];Expression[+In, ?Yield, ?Await]?;Expression[+In, ?Yield, ?Await]?)Statement[?Yield, ?Await, ?Return]for(LexicalDeclaration[~In, ?Yield, ?Await] Expression[+In, ?Yield, ?Await]?;Expression[+In, ?Yield, ?Await]?)Statement[?Yield, ?Await, ?Return]
Problem
for ( LexicalDeclaration … )first semicolon. §12.10.1's overriding condition forbids inserting "one of the two semicolons in the header of aforstatement", whereas §14.7.4 never defines "header of aforstatement" and in the thirdForStatementproductionForStatementitself has only one;terminal on its right-hand side — the first semicolon offor (let a = 1; a < 2; a++)is the trailing;inside childLexicalDeclaration : LetOrConst BindingList ;(orUsingDeclaration/AwaitUsingDeclaration), where ASI is otherwise permitted. Formalizing §12.10.1 at the production level must therefore scope that restriction acrossForStatement'sLexicalDeclarationchild rather thanForStatement's own;terminals alone.- Declarative (§5.1.5) vs left-to-right (§12.10.1) ASI. §5.1.5 introduces ASI declaratively ("if only semicolons
were added to the sequence in certain places"), while §12.10.1 states offending-token insertion operationally ("as the
source text is parsed from left to right, a token … is encountered that is not allowed by any production of the grammar")
without defining viable-prefix commitment for a non-LR(1) grammar. Without maximal left-to-right viable-prefix
commitment, declarative semicolon insertion admits extra parses on
a = b\n+c(a = b + c;vsa = b; +c;) andlet a;\na\n[\n] = [];(a; [] = [];), whereas all engines commit left-to-right to the longest viable prefix. [no LineTerminator here]inside lookahead sets.ExpressionStatementandExportDeclarationwrite`async` [no LineTerminator here] `function`inside[lookahead ∉ { … }], whereas §5.1.6.7 defines lookahead elements solely as token sequences and §5.1.6.6 defines[no LineTerminator here]only on production right-hand sides.
Examples
var bad = ["for (let a\n a < 1; a++) {}", "let a;\na\n[\n] = [];"];
for (var s of bad) {
try { (0, eval)(s); print("valid"); } catch (e) { print(e.name); }
}
for (let a LF a < 1; a++) {} |
let a; a LF [ LF ] = []; |
|
|---|---|---|
unscoped production ; / ASI without prefix commitment |
valid | valid (a; [] = [];) |
| V8, SpiderMonkey, JavaScriptCore | SyntaxError |
SyntaxError |
| formal model (test262 reading) | SyntaxError |
SyntaxError |
Proposed wording
Amend §12.10.1's overriding condition to include the terminating semicolon of a |LexicalDeclaration| in a
|ForStatement| header, define offending-token ASI via maximal viable left-to-right prefix continuation, and define
[no LineTerminator here] between adjacent tokens in §5.1.6.7 lookahead sequences.
test262
language/asi/S7.9_A5.2_T1.js and language/statements/for/ require SyntaxError when either semicolon in a for
header is omitted across a newline.