Files
Onyeka ObiandGitHub 81641342aa fix(es/parser): Reject object-rest assignment to array/object literal (#11875)
**Description**

  Closes #11543.

  Per ECMA-262 ยง13.15.5.1, `AssignmentRestProperty`'s
`DestructuringAssignmentTarget` must not be an `ArrayLiteral` or
`ObjectLiteral`.
Until this PR the parser silently accepted ({...{}} = {}), ({...({})} =
{}),
({...[]} = {}), and ({...([])} = {}), all of which V8 and SpiderMonkey
reject.

The fix adds a parser check in reparse_expr_as_pat's rest-element path:
in
AssignElement context, if the rest target is an `ArrayLit` or
`ObjectLit`
(possibly wrapped in parentheses), emit `SyntaxError::NotSimpleAssign`
and
recover with `Pat::Invalid`. Other shapes that are syntactically allowed
per
spec but throw at runtime (optional chain, member access, calls)
continue
  through `reparse_expr_as_pat` unchanged.

  **Why this is narrower than the two prior attempts**

- #11555 used is_valid_simple_assignment_target, which also rejects
optional
chain rest targets ({...obj?.a}). Per spec those are syntactically valid
  (they throw at runtime when the chain short-circuits), so #11555 was
  over-rejecting.
- #11579 caught direct Array/Object but not parenthesized forms; CI
failed on
  tsc/restPropertyWithBindingPattern.ts which covers both shapes.

  **Test changes**

  - New error fixtures tests/errors/issue-11543 and
tests/errors/issue-11543-array cover ({...{}} = {}) and ({...[]} = {}).
- tests/tsc/restPropertyWithBindingPattern.ts excluded from the tsc_spec
harness with a pointer to this issue; its .ts.json snapshots the
parse-OK AST
for the four invalid forms, which is no longer what we produce. The new
error
   fixtures provide the replacement coverage.

  **Verification**

```
  - cargo fmt --all --check
  - cargo clippy -p swc_ecma_parser --all-targets -- -D warnings
  - cargo test -p swc_ecma_parser (4954 pass; only pre-existing master-side
  invalid_type_only failure remains, confirmed unrelated by running on pristine
  main)
```

---------

Signed-off-by: Onyeka Obi <[email protected]>
2026-05-21 11:08:40 +09:00

6 lines
125 B
Plaintext

x Cannot assign to this
,-[$DIR/tests/errors/issue-11543-array/input.js:1:1]
1 | ({...[]} = {});
: ^^^^^
`----