Files
oxc-project__oxc/crates/oxc_react_compiler/tests/snapshots/destructuring-assignment.snap
Boshen cb2a5051af fix(react_compiler): codegen destructuring reassignment targets (#24131)
## Root cause

The reactive-function backend (`codegen_reactive_function.rs`) only knew
how to turn a plain `BindingIdentifier` lvalue into an
`AssignmentTarget`. Any **destructuring reassignment** to an existing
binding — `[x] = arr`, `({a} = obj)`, `({a} = {a:{}}), ([x] = [[]])` —
hit an unported `invariant_err("Destructuring reassignment targets are
not yet ported to oxc")`.

`codegen_function` deliberately swallows any emission error and falls
back to an **empty body** (a shim from the de-Babel batches). So every
function containing such an assignment silently compiled to `function
f() {}` — params and body both dropped — with **no diagnostic**. This is
a hard miscompile, not a whitespace diff.

## Fix

Port `ox_binding_pattern_to_assignment_target` to recursively reshape
`BindingPattern` → `AssignmentTarget`, since oxc (unlike Babel) models
declaration patterns and assignment targets as distinct node families.
Adds handling for:

- array patterns → `ArrayAssignmentTarget` (elements + holes + rest)
- object patterns → `ObjectAssignmentTarget`, distinguishing shorthand
(`{a}` / `{a = 1}` → `AssignmentTargetPropertyIdentifier`) from keyed
(`{a: b}` → `AssignmentTargetPropertyProperty`)
- rest elements (`...x`)
- nested patterns and defaults (`AssignmentTargetWithDefault`)

Mirrors Babel, which reuses the same pattern nodes as the assignment
LHS.

## Impact

23 corpus snapshots changed — **all move toward the fork golden, none
regress**; 14 now match the fork exactly. Verified old-vs-new token
distance to each fork golden.

Within this batch (`reduce-reactive-deps` /
`propagate-scope-deps-hir-fork`):

- `ssa-renaming-via-destructuring-with-mutation` — was empty function,
now matches the fork exactly.
- `ssa-renaming-ternary-destruction` /
`ssa-renaming-ternary-destruction-with-mutation` — were empty functions,
now compile with **matching dependency sets, cache keys, scope
boundaries, and dead-code elimination**; the only residual is
codegen-only (sequence-expression parenthesisation `([x]=[[]], …)` vs
`(([x]=[[]]), …)` and object-literal whitespace).

Other improved fixtures across the corpus (also previously
empty-bodied): `destructuring`, `destructuring-assignment`,
`destructuring-assignment-array-default`,
`destructure-direct-reassignment`, `destructure-in-branch-ssa`,
`destructuring-object-pattern-within-rest`,
`escape-analysis-destructured-rest-element`,
`call-args-destructuring-assignment`,
`array-pattern-spread-creates-array`, the `nonmutated-spread-*` set,
`repro-reassign-props`, `switch-with-only-default`,
`new-mutability__ssa-renaming-ternary-destruction`.

## Remaining work (out of scope here)

The rest of the batch's diffs are pure codegen and live in
`oxc_codegen`, not the dependency passes:
- sequence-expression parenthesisation of assignment operands (`(a=[],
b)` vs `((a=[]), b)`)
- JSX multi-line wrapping, TS type printing, object/array-literal
whitespace

The two `try-catch-*` fixtures (stray `;`) belong to a sibling
`fix/rc-try-catch` branch and are left untouched.
2026-07-04 10:43:43 +08:00

45 lines
739 B
Plaintext

---
source: crates/oxc_react_compiler/tests/snapshot.rs
input_file: crates/oxc_react_compiler/fixtures/destructuring-assignment.js
---
import { c as _c } from "react/compiler-runtime";
function foo(a, b, c) {
const $ = _c(5);
let d;
let g;
let n;
let o;
const [t0, t1] = a;
d = t0;
const [t2] = t1;
const { e: t3 } = t2;
({f: g} = t3);
const { l: t4, o: t5 } = b;
const { m: t6 } = t4;
const [t7] = t6;
[n] = t7;
o = t5;
let t8;
if ($[0] !== d || $[1] !== g || $[2] !== n || $[3] !== o) {
t8 = {
d,
g,
n,
o
};
$[0] = d;
$[1] = g;
$[2] = n;
$[3] = o;
$[4] = t8;
} else {
t8 = $[4];
}
return t8;
}
export const FIXTURE_ENTRYPOINT = {
fn: foo,
params: ["TodoAdd"],
isComponent: "TodoAdd"
};