From bfd29f3ae377433cb810b51ff2b458eb04a19b46 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Tue, 11 Jun 2024 15:31:25 -0400 Subject: [PATCH] JS: fix parsing of async variable when async function is not allowed --- js/parse.go | 8 ++++---- js/parse_test.go | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/js/parse.go b/js/parse.go index 3a1bd3d..ddd9185 100644 --- a/js/parse.go +++ b/js/parse.go @@ -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 diff --git a/js/parse_test.go b/js/parse_test.go index 67c491d..1fd05da 100644 --- a/js/parse_test.go +++ b/js/parse_test.go @@ -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({ }))"}, @@ -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