Repository navigation
JS: parse the default of a destructuring arrow parameter as an expression - #137
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes tdewolff/minify#1072
A destructuring arrow parameter with a default, like
([y] = [2]) => y, is first parsed as an expression withassumeArrowFuncset. The default after=was parsed with the flag still on: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(hereais already declared, which also turns the flag off)var z=10; ([y] = [z]) => y + zboundzto the arrow scope, so minify renamed it and the output threwReferenceError.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 inparseExpressionSuffix, and restores the flag after a plain=only (([a] += 1) => xis still an error).Tests: four cases in
TestParseand four inTestParseScope, all failing before the change.go test ./...passes here, and minify'sgo test ./...passes against this branch. With it, the three commands from the issue give: