Repository navigation
fix(ast): accept every simple target in destructuring assignment patterns - #5596
Closed
andreasrosdal wants to merge 1 commit into
Closed
andreasrosdal wants to merge 1 commit into
andreasrosdal wants to merge 1 commit into
Conversation
…erns
A destructuring assignment target that is not an object or array literal
must be a simple assignment target, and a parenthesized identifier or
property access is one. `ArrayLiteral::to_pattern` and
`ObjectLiteral::to_pattern` only knew bare identifiers and property
accesses, so `[(a), b] = list`, `({ a: (b) } = obj)`, `[...(x)] = list`
and `({...(o.p)} = obj)` were rejected. Any other target is now refined
through `AssignTarget::from_expression_simple`, which also rejects `eval`
and `arguments` in strict code (`[eval] = list`, `({ arguments } = obj)`
were accepted). A parenthesized pattern such as `[({a})] = list` stays an
error.
Object patterns also accept nested patterns and initializers under
computed and numeric property names (`({ [k]: [x] } = obj)`,
`({ 0: x = 1 } = list)`). An initializer is named only when its target is
an unparenthesized identifier (IsIdentifierRef), as the assignment parser
already does, so `[(x) = function () {}] = []` no longer names the
function. A parenthesized object or array literal is not a pattern in a
for-in/of head: `for (([x]) of list)` is a syntax error.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
It changes the following:
ArrayLiteral::to_pattern/ObjectLiteral::to_patternaccept any simple assignment target (viaAssignTarget::from_expression_simple), including parenthesized ones:[(a), b] = list,({ a: (b) } = obj),[...(x)] = list,({...(o.p)} = obj).evalandargumentsare rejected as destructuring targets in strict code ([eval] = list,({ a: eval } = obj),({ arguments } = obj),[...eval] = listwere accepted).({ [k]: [x] } = obj),({ 0: x = 1 } = list).to_pattern; it was already named by the assignment parser only when the target is an unparenthesized identifier (IsIdentifierRef), so[(x) = function () {}] = []no longer names the functionx.for (([x]) of list);is a SyntaxError.Spec: AssignmentTargetType and the destructuring assignment early errors. Fixes test262
staging/sm/expressions/destructuring-pattern-parenthesized.js,staging/sm/destructuring/order.jsandstaging/sm/destructuring/order-super.js. Found while embedding Boa in the Southstar browser.Tested with a new parser unit test (
destructuring_assignment_simple_targets).🤖 Generated with Claude Code