Skip to content

Commit

Permalink
JS: fix automatic semicolon insertion in empty for-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Nov 16, 2023
1 parent 862a1dc commit f48e531
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
body.List = p.parseStmtList("")
} else if p.tt != SemicolonToken {
body.List = []IStmt{p.parseStmt(false)}
} else {
p.next()
}
if init == nil {
varDecl := &VarDecl{TokenType: VarToken, Scope: p.scope, InFor: true}
Expand All @@ -408,6 +410,8 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
body.List = p.parseStmtList("")
} else if p.tt != SemicolonToken {
body.List = []IStmt{p.parseStmt(false)}
} else {
p.next()
}
if varDecl, ok := init.(*VarDecl); ok {
varDecl.InForInOf = true
Expand All @@ -424,6 +428,8 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
body.List = p.parseStmtList("")
} else if p.tt != SemicolonToken {
body.List = []IStmt{p.parseStmt(false)}
} else {
p.next()
}
if varDecl, ok := init.(*VarDecl); ok {
varDecl.InForInOf = true
Expand Down
4 changes: 3 additions & 1 deletion js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,10 @@ func TestParse(t *testing.T) {
{"function f(){return /*comment*/ a}", "Decl(function f Params() Stmt({ Stmt(return a) }))"},
{"function f(){return /*com\nment*/ a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"},
{"function f(){return //comment\n a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"},
{"a?.b\n`c`", "Stmt((a?.b)`c`)"},
{"if(a)while(true)\n;else;", "Stmt(if a Stmt(while true Stmt()) else Stmt())"},
{"if(a)for(;;)\n;else;", "Stmt(if a Stmt(for ; ; Stmt({ })) else Stmt())"},

{"a?.b\n`c`", "Stmt((a?.b)`c`)"},
{"() => { const v=6; x={v} }", "Stmt(Params() => Stmt({ Decl(const Binding(v = 6)) Stmt(x={v}) }))"},
{`([]=l=>{let{e}={e}})`, `Stmt(([]=(Params(Binding(l)) => Stmt({ Decl(let Binding({ Binding(e) } = {e})) }))))`}, // go-fuzz
}
Expand Down

0 comments on commit f48e531

Please sign in to comment.