Skip to content

JS: parse the default of a destructuring arrow parameter as an expression - #137

Merged
tdewolff merged 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-arrow-pattern-default
Nov 12, 2026
Merged

tdewolff merged 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-arrow-pattern-default

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Fixes tdewolff/minify#1072

A destructuring arrow parameter with a default, like ([y] = [2]) => y, is first parsed as an expression with assumeArrowFunc set. The default after = was parsed with the flag still on:

  • a literal in the default (2, x:1) turned the flag off, so the => that follows failed with "unexpected => in expression":
    • ([y] = [2]) => y
    • ({x} = {x:1}) => x
    • (a, [b] = [a]) => b (here a is already declared, which also turns the flag off)
  • identifiers in the default were declared as arrow parameters. var z=10; ([y] = [z]) => y + z bound z to the arrow scope, so minify renamed it and the output threw ReferenceError.

The identifier case ((a = b) => a) already parses its initializer with the flag off. This does the same for the right-hand side of any assignment in parseExpressionSuffix, and restores the flag after a plain = only (([a] += 1) => x is still an error).

Tests: four cases in TestParse and four in TestParseScope, all failing before the change. go test ./... passes here, and minify's go test ./... passes against this branch. With it, the three commands from the issue give:

var g=([e]=[2])=>e;console.log(g())        // 2
var h=({x:e}={x:1})=>e;console.log(h())    // 1
var z=10,k=([e]=[z])=>e+z;console.log(k()) // 20

…sion

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
@tdewolff
tdewolff merged commit d8c3779 into tdewolff:master Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JS: arrow parameter ([y] = [2]) => y fails to parse; ([y] = [z]) => y renames the outer z

2 participants