Skip to content

Commit

Permalink
JS: fix parsing of async variable when async function is not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jun 11, 2024
1 parent 6c18dfd commit bfd29f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,13 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
}
stmt = p.parseFuncDecl()
case AsyncToken: // async function
if !allowDeclaration {
p.fail("statement")
return
}
async := p.data
p.next()
if p.tt == FunctionToken && !p.prevLT {
if !allowDeclaration {
p.fail("statement")
return
}
stmt = p.parseAsyncFuncDecl()
} else {
// expression
Expand Down
4 changes: 3 additions & 1 deletion js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func TestParse(t *testing.T) {
{"async();", "Stmt(async())"},
{"async(a=6, ...b)", "Stmt(async((a=6), ...b))"},
{"async(function(){})", "Stmt(async(Decl(function Params() Stmt({ }))))"},
{"async.value", "Stmt(async.value)"},
{"if(a)async.value", "Stmt(if a Stmt(async.value))"},
{"function a(){ async\nawait => b }", "Decl(function a Params() Stmt({ Stmt(async) Stmt(Params(Binding(await)) => Stmt({ Stmt(return b) })) }))"},
{"a + async\nb", "Stmt(a+async) Stmt(b)"},
{"a + async\nfunction f(){}", "Stmt(a+async) Decl(function f Params() Stmt({ }))"},
Expand Down Expand Up @@ -579,7 +581,7 @@ func TestParseError(t *testing.T) {

// no declarations
{"if(a) function f(){}", "unexpected function in statement"},
{"if(a) async function f(){}", "unexpected async in statement"},
{"if(a) async function f(){}", "unexpected function in statement"},
{"if(a) class c{}", "unexpected class in statement"},

// yield, async, await
Expand Down

0 comments on commit bfd29f3

Please sign in to comment.