From 2389b873efe766e95083dac534f536577ae2c4c2 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Thu, 26 Oct 2023 12:30:15 -0300 Subject: [PATCH] JS: don't allow yield expression in arrow function body, see #108 --- js/parse.go | 10 ++++++---- js/parse_test.go | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/js/parse.go b/js/parse.go index e68f84a..923aa79 100644 --- a/js/parse.go +++ b/js/parse.go @@ -1506,16 +1506,18 @@ func (p *Parser) parseArrowFuncBody() (list []IStmt) { // mark undeclared vars as arguments in `function f(a=b){var b}` where the b's are different vars p.scope.MarkFuncArgs() + parentYield := p.yield + p.yield = false if p.tt == OpenBraceToken { - parentInFor := p.inFor - p.inFor = false - p.yield = false + parentInFor, parentAwait := p.inFor, p.await + p.inFor, p.await = false, false p.allowDirectivePrologue = true list = p.parseStmtList("arrow function") - p.inFor = parentInFor + p.inFor, p.await = parentInFor, parentAwait } else { list = []IStmt{&ReturnStmt{p.parseExpression(OpAssign)}} } + p.yield = parentYield return } diff --git a/js/parse_test.go b/js/parse_test.go index ccf9446..9649a19 100644 --- a/js/parse_test.go +++ b/js/parse_test.go @@ -176,6 +176,7 @@ func TestParse(t *testing.T) { {"function*a(){ (yield) }", "Decl(function* a Params() Stmt({ Stmt((yield)) }))"}, {"function*a(){ (yield a) }", "Decl(function* a Params() Stmt({ Stmt((yield a)) }))"}, {"function a(){ let\nawait }", "Decl(function a Params() Stmt({ Decl(let Binding(await)) }))"}, + {"function*a(){ b => yield%5 }", "Decl(function* a Params() Stmt({ Stmt(Params(Binding(b)) => Stmt({ Stmt(return (yield%5)) })) }))"}, {"x = {await}", "Stmt(x={await})"}, {"async function a(){ x = {await: 5} }", "Decl(async function a Params() Stmt({ Stmt(x={await: 5}) }))"}, {"async function a(){ x = await a }", "Decl(async function a Params() Stmt({ Stmt(x=(await a)) }))"}, @@ -592,7 +593,7 @@ func TestParseError(t *testing.T) { {"let\nawait", "unexpected await in binding"}, {"let {await = 5} = z", "expected : instead of = in object binding"}, {"x = await => a++", "unexpected => in expression"}, - {"function*a(){ b => yield%5 }", "unexpected % in expression"}, + {"function*a(){ b => yield 0 }", "unexpected 0 in expression"}, // specific cases {"{a, if: b, do(){}, ...d}", "unexpected if in expression"}, // block stmt