Skip to content

Commit

Permalink
JS: fix assumeArrowFunc inside arrow function body, fixes tdewolff/mi…
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Aug 29, 2020
1 parent b074b5a commit 09d9f68
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions js/parse.go
Expand Up @@ -1999,9 +1999,10 @@ func (p *Parser) parseParenthesizedExpressionOrArrowFunc(prec OpPrec) IExpr {
return nil
}
p.next()
p.inFor = parentInFor
isArrowFunc := p.tt == ArrowToken && p.assumeArrowFunc
p.assumeArrowFunc, p.inFor = parentAssumeArrowFunc, parentInFor

if p.tt == ArrowToken && p.assumeArrowFunc {
if isArrowFunc {
parentAsync, parentGenerator := p.async, p.generator
p.async, p.generator = false, false

Expand All @@ -2013,7 +2014,7 @@ func (p *Parser) parseParenthesizedExpressionOrArrowFunc(prec OpPrec) IExpr {
arrowFunc.Params.Rest = p.exprToBinding(rest)
arrowFunc.Body.List = p.parseArrowFuncBody()

p.async, p.generator, p.assumeArrowFunc = parentAsync, parentGenerator, parentAssumeArrowFunc
p.async, p.generator = parentAsync, parentGenerator
p.exitScope(parent)

left = &arrowFunc
Expand All @@ -2022,7 +2023,6 @@ func (p *Parser) parseParenthesizedExpressionOrArrowFunc(prec OpPrec) IExpr {
p.fail("arrow function", ArrowToken)
return nil
} else {
p.assumeArrowFunc = parentAssumeArrowFunc
p.exitScope(parent)

// for any nested FuncExpr/ArrowFunc scope, Parent will point to the temporary scope created in case this was an arrow function instead of a parenthesized expression. This is not a problem as Parent is only used for defining new variables, and we already parsed all the nested scopes so that Parent (not Func) are not relevant anymore. Anyways, the Parent will just point to an empty scope, whose Parent/Func will point to valid scopes. This should not be a big deal.
Expand Down
2 changes: 2 additions & 0 deletions js/parse_test.go
Expand Up @@ -363,6 +363,8 @@ func TestParse(t *testing.T) {
{"return /*com\nment*/ a", "Stmt(return) Stmt(a)"},
{"return //comment\n a", "Stmt(return) Stmt(a)"},
{"a?.b\n`c`", "Stmt((a?.b)`c`)"},

{"() => { const v=6; x={v} }", "Stmt(Params() => Stmt({ Decl(const Binding(v = 6)) Stmt(x={v}) }))"},
}
for _, tt := range tests {
t.Run(tt.js, func(t *testing.T) {
Expand Down

0 comments on commit 09d9f68

Please sign in to comment.