Skip to content

Commit

Permalink
JS: don't allow yield expression in arrow function body, see #108
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 26, 2023
1 parent 821e4c7 commit 2389b87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }))"},
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2389b87

Please sign in to comment.