Skip to content

Commit f743029

Browse files
committed
fix(compiler): follow state-tuple and effect-event helpers in deferred snapshot mutation check
The outside-render helper walk only followed a call when an argument had kind === 'snapshot'. A whole useState/useReducer tuple (kind === 'state-tuple') was skipped, so passing the tuple into a local helper from an event or timer never re-bound the parameter and mutations through pair[0] escaped the OCTANE_STRONG_SNAPSHOT_MUTATION diagnostic. Similarly, useEffectEvent results (kind === 'effect-event') were not considered by the deferred-phase callback kind check. An event-time call that passes a state value into an Effect Event never re-entered the body with those arguments. Fix: unwrap effect-event to its inner callback before the kind check, and include state-tuple alongside snapshot in the argument filter. Add regression tests for both patterns.
1 parent 3f7b937 commit f743029

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,10 +4477,13 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
44774477
}
44784478
} else if (hook === null) {
44794479
// Any helper can write to a state value passed to it.
4480-
const callback = callableValue(callee, scope);
4480+
let callback = callableValue(callee, scope);
4481+
if (callback?.kind === 'effect-event') callback = callback.callback;
44814482
if (callback?.kind === 'callback' || callback?.kind === 'callback-choice') {
44824483
const args = argumentValues(node.arguments, scope);
4483-
if (args?.some((value) => value?.kind === 'snapshot')) {
4484+
if (
4485+
args?.some((value) => value?.kind === 'snapshot' || value?.kind === 'state-tuple')
4486+
) {
44844487
visitCallable(callback, callee, executionPhase, args);
44854488
}
44864489
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ describe('Strong state mutation outside render', () => {
162162
'Object.assign before a spread',
163163
`export function A() { const [s, setS] = useState({ list: [] }); return <b onClick={() => { Object.assign(s, { list: [...s.list, 1] }); setS({ ...s }); }}>{s.list.length}</b>; }`,
164164
],
165+
[
166+
'a helper receiving the full state tuple',
167+
`function mutate(pair) { pair[0].push(1); pair[1]([...pair[0]]); } export function A() { const tuple = useState([]); return <b onClick={() => mutate(tuple)}>{tuple[0].length}</b>; }`,
168+
],
169+
[
170+
'a useEffectEvent receiving a snapshot parameter',
171+
`export function A() { const [items, setItems] = useState([]); const handler = useEffectEvent((list) => { list.push(1); }); return <b onClick={() => handler(items)}>{items.length}</b>; }`,
172+
],
165173
])('rejects the rewrite through %s', (_label, body) => {
166174
expect(rejected(body)).toBe(SNAPSHOT_MUTATION);
167175
});

0 commit comments

Comments
 (0)