Skip to content

Commit 72eb028

Browse files
committed
fix(compiler): decompose state-tuple bindings for destructured parameters in Strong mode
When a state-tuple was passed to a helper or Effect Event and the callee destructured it in a parameter (e.g. function add([list]) { list.push(1); }), visitParameters only called bindSnapshotPattern for kind === 'snapshot'. The destructured elements stayed as OTHER_BINDING, so mutations on the snapshot were not reported. Add ArrayPattern and ObjectPattern decomposition for state-tuple parameters, mirroring the existing logic in bindDeclarationValue. Index 0 is bound as the snapshot, index 1 as the setter (index 2 / getter was already handled by bindStateGetterPattern). Add a regression test that passes a whole tuple to a helper with a destructured array parameter.
1 parent 0c1e251 commit 72eb028

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎packages/octane/src/compiler/strong-mode.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,42 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
23362336
if (value?.kind === 'snapshot' && parameter.type !== 'Identifier') {
23372337
bindSnapshotPattern(parameter, value, bindGetter, snapshotPatternProperty(parameterScope));
23382338
}
2339+
if (value?.kind === 'state-tuple' && parameter.type !== 'Identifier') {
2340+
if (parameter.type === 'ArrayPattern') {
2341+
bindSnapshotPattern(
2342+
parameter.elements?.[0],
2343+
value.snapshot,
2344+
bindGetter,
2345+
snapshotPatternProperty(parameterScope),
2346+
);
2347+
const setterElement = parameter.elements?.[1];
2348+
bindGetter(
2349+
setterElement?.type === 'AssignmentPattern' ? setterElement.left : setterElement,
2350+
value.setter,
2351+
);
2352+
} else if (parameter.type === 'ObjectPattern') {
2353+
for (const property of parameter.properties ?? []) {
2354+
if (property.type !== 'Property') continue;
2355+
const key = property.computed
2356+
? staticPrimitiveValue(property.key, parameterScope)
2357+
: property.key?.type === 'Identifier'
2358+
? property.key.name
2359+
: property.key?.value;
2360+
const pv = property.value;
2361+
const setter = pv?.type === 'AssignmentPattern' ? pv.left : pv;
2362+
if ((key === 1 || key === '1') && setter?.type === 'Identifier') {
2363+
bindGetter(setter, value.setter);
2364+
} else if (key === 0 || key === '0') {
2365+
bindSnapshotPattern(
2366+
pv,
2367+
value.snapshot,
2368+
bindGetter,
2369+
snapshotPatternProperty(parameterScope),
2370+
);
2371+
}
2372+
}
2373+
}
2374+
}
23392375
if (parameter.type === 'Identifier' && !isReassigned(parameter)) {
23402376
parameterScope.bindings.set(parameter.name, value);
23412377
}

‎packages/octane/tests/compiler/strong-state.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ describe('Strong state mutation outside render', () => {
366366
'a tuple passed to a helper',
367367
`function add(pair) { pair[0].push(1); } export function A() { const tuple = useState([]); return <b onClick={() => { add(tuple); tuple[1]([...tuple[0]]); }}>{tuple[0].length}</b>; }`,
368368
],
369+
[
370+
'a destructured tuple parameter in a helper',
371+
`function add([list]) { list.push(1); } export function A() { const tuple = useState([]); return <b onClick={() => { add(tuple); tuple[1]([...tuple[0]]); }}>{tuple[0].length}</b>; }`,
372+
],
369373
[
370374
'an Effect Event called with the state',
371375
`export function A() { const [items, setItems] = useState([]); const add = useEffectEvent((list) => { list.push(1); }); return <b onClick={() => { add(items); setItems([...items]); }}>{items.length}</b>; }`,

0 commit comments

Comments
 (0)