From 89cb4b44e218d2b7be0df02306885ba914e9a599 Mon Sep 17 00:00:00 2001 From: bardrop Date: Thu, 12 Nov 2026 20:19:39 +0530 Subject: [PATCH] JS: parse the default of a destructuring arrow parameter as an expression In `([y] = [2]) => y` the default after `=` was parsed while the parser still assumed it was reading arrow parameters. A literal in the default then turned that assumption off (parse error "unexpected =>"), and identifiers in it were declared as parameters, so `([y] = [z]) => y` bound `z` to the arrow scope instead of the outer `z`. Turn the assumption off for the right-hand side of an assignment and restore it after `=` only, like the identifier case already does. Fixes tdewolff/minify#1072 --- js/parse.go | 4 ++++ js/parse_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/js/parse.go b/js/parse.go index e397649..0d261f3 100644 --- a/js/parse.go +++ b/js/parse.go @@ -1913,7 +1913,11 @@ func (p *Parser) parseExpressionSuffix(left IExpr, prec, precLeft OpPrec) IExpr return nil } p.next() + // the right-hand side is never a binding, even when the left-hand side is a binding pattern with an initializer in arrow function parameters + prevAssumeArrowFunc := p.assumeArrowFunc + p.assumeArrowFunc = false left = &BinaryExpr{tt, left, p.parseExpression(OpAssign)} + p.assumeArrowFunc = prevAssumeArrowFunc && tt == EqToken precLeft = OpAssign case LtToken, LtEqToken, GtToken, GtEqToken, InToken, InstanceofToken: if OpCompare < prec || !p.in && tt == InToken { diff --git a/js/parse_test.go b/js/parse_test.go index 3843afb..d055be4 100644 --- a/js/parse_test.go +++ b/js/parse_test.go @@ -345,6 +345,10 @@ func TestParse(t *testing.T) { {"x = (a = 5) => {a++}", "Stmt(x=(Params(Binding(a = 5)) => Stmt({ Stmt(a++) })))"}, {"x = ({a = 5}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a = 5) })) => Stmt({ Stmt(a++) })))"}, {`({a:b=1}={})=>b`, `Stmt(Params(Binding({ a: Binding(b = 1) } = {})) => Stmt({ Stmt(return b) }))`}, + {"([a] = [5]) => a", "Stmt(Params(Binding([ Binding(a) ] = [5])) => Stmt({ Stmt(return a) }))"}, + {"({a} = {a:5}) => a", "Stmt(Params(Binding({ Binding(a) } = {a: 5})) => Stmt({ Stmt(return a) }))"}, + {"([[a] = [5]]) => a", "Stmt(Params(Binding([ Binding([ Binding(a) ] = [5]) ])) => Stmt({ Stmt(return a) }))"}, + {"(a, [b] = [a]) => b", "Stmt(Params(Binding(a), Binding([ Binding(b) ] = [a])) => Stmt({ Stmt(return b) }))"}, // expression precedence {"!!a", "Stmt(!(!a))"}, @@ -1004,6 +1008,10 @@ func TestParseScope(t *testing.T) { {"({[a+b]:c}) => {}", "/c=3", "a=1,b=2/a=1,b=2"}, {"({a:b, c=d, ...e}=f) => 5", "/b=3,c=4,e=5", "d=1,f=2/d=1,f=2"}, {"([a, b=c, ...d]=e) => 5", "/a=3,b=4,d=5", "c=1,e=2/c=1,e=2"}, + {"([a]=[b]) => a+b", "/a=2", "b=1/b=1"}, + {"({a}={b}) => a+b", "/a=2", "b=1/b=1"}, + {"({a}={a:b}) => a+b", "/a=2", "b=1/b=1"}, + {"(a, [b]=[a, c]) => b", "/a=2,b=3", "c=1/c=1"}, {"(a) + ((b,c) => {var d; e = 5; return e})", "/b=3,c=4,d=5", "a=1,e=2/e=2"}, {"(a) + ((a,b) => {var c; d = 5; return d})", "/a=3,b=4,c=5", "a=1,d=2/d=2"}, {"{(a) + ((a,b) => {var c; d = 5; return d})}", "//a=3,b=4,c=5", "a=1,d=2/a=1,d=2/d=2"},