Skip to content

Commit 25acfb0

Browse files
committed
fix(compiler): detect stale state updates through Effect Event arguments
When an Effect Event receives state as arguments from deferred code, setter calls inside its body that use those argument-bound snapshots should trigger OCTANE_STRONG_STALE_STATE_UPDATE. The 'event' phase override correctly suppresses the diagnostic for captures (which read the latest committed value), but it was also suppressing it for argument-bound state that was evaluated at the deferred call site. Add a boundary-aware stale check that fires during the Effect Event body visit. The boundary is set to the callback's parent scope (component scope), so identifiers resolving from that scope or above (captures) are excluded while identifiers resolving from the function scope (parameters bound to args) are checked.
1 parent 533950f commit 25acfb0

3 files changed

Lines changed: 51 additions & 12 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
15361536
// Updaters and reducers run while their owner renders, and deferred code
15371537
// reads a render snapshot that later updates may already have replaced.
15381538
let insidePureCallback = false;
1539+
let effectEventStaleScope = null;
15391540
const statePolicy = createStrongStatePolicy({
15401541
report,
15411542
resolve,
@@ -4473,6 +4474,8 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
44734474
if (hook === null) {
44744475
if (executionPhase === 'deferred') {
44754476
statePolicy.checkStaleUpdate(callee, node.arguments, scope);
4477+
} else if (effectEventStaleScope !== null) {
4478+
statePolicy.checkStaleUpdate(callee, node.arguments, scope, effectEventStaleScope);
44764479
}
44774480
if (insidePureCallback && executionPhase === 'render') {
44784481
statePolicy.checkPureCall(callee, scope);
@@ -4625,7 +4628,12 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
46254628
(executionPhase === 'deferred' && !effectEvent) ||
46264629
args?.some((value) => value?.kind === 'snapshot' || value?.kind === 'state-tuple')
46274630
) {
4631+
const prevEEScope = effectEventStaleScope;
4632+
if (effectEvent && executionPhase === 'deferred' && callback?.kind === 'callback') {
4633+
effectEventStaleScope = callback.scope;
4634+
}
46284635
visitCallable(callback, callee, effectEvent ? 'event' : executionPhase, args);
4636+
effectEventStaleScope = prevEEScope;
46294637
}
46304638
}
46314639
}

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ export function createStrongStatePolicy(api) {
130130
hookOf,
131131
} = api;
132132
const asynchronousCallbacks = new WeakSet();
133+
let activeStaleCheckBoundary = null;
134+
135+
function resolvesBelowBoundary(scope, name) {
136+
for (let s = scope; s; s = s.parent) {
137+
if (s === activeStaleCheckBoundary) return false;
138+
if (s.bindings.has(name)) return true;
139+
}
140+
return false;
141+
}
133142

134143
function unshadowed(node, scope, names) {
135144
const value = unwrap(node);
@@ -297,18 +306,24 @@ export function createStrongStatePolicy(api) {
297306
},
298307

299308
/** Report a deferred setter or dispatch computed from its own render snapshot. */
300-
checkStaleUpdate(callee, args, scope) {
301-
for (const setter of setterValues(callableValue(callee, scope))) {
302-
for (const argument of args ?? []) {
303-
const read = readsState(argument, scope, setter.state, new Set());
304-
if (read === null) continue;
305-
report(
306-
STRONG_STALE_STATE_UPDATE,
307-
read,
308-
hookOf(setter.state) === 'useReducer' ? STALE_REDUCER_MESSAGE : STALE_MESSAGE,
309-
);
310-
break;
309+
checkStaleUpdate(callee, args, scope, boundary = null) {
310+
const prev = activeStaleCheckBoundary;
311+
activeStaleCheckBoundary = boundary;
312+
try {
313+
for (const setter of setterValues(callableValue(callee, scope))) {
314+
for (const argument of args ?? []) {
315+
const read = readsState(argument, scope, setter.state, new Set());
316+
if (read === null) continue;
317+
report(
318+
STRONG_STALE_STATE_UPDATE,
319+
read,
320+
hookOf(setter.state) === 'useReducer' ? STALE_REDUCER_MESSAGE : STALE_MESSAGE,
321+
);
322+
break;
323+
}
311324
}
325+
} finally {
326+
activeStaleCheckBoundary = prev;
312327
}
313328
},
314329
};
@@ -392,9 +407,21 @@ export function createStrongStatePolicy(api) {
392407
if (node == null || node.type?.startsWith('TS')) return null;
393408
switch (node.type) {
394409
case 'Identifier':
410+
if (activeStaleCheckBoundary !== null && !resolvesBelowBoundary(scope, node.name)) {
411+
return null;
412+
}
395413
return readsBinding(resolve(scope, node.name), state, active) ? node : null;
396414
case 'MemberExpression':
397-
if (snapshotBinding(node, scope)?.state === state) return node;
415+
if (snapshotBinding(node, scope)?.state === state) {
416+
if (activeStaleCheckBoundary !== null) {
417+
let root = node;
418+
while (root?.type === 'MemberExpression') root = unwrap(root.object);
419+
if (root?.type === 'Identifier' && !resolvesBelowBoundary(scope, root.name)) {
420+
break;
421+
}
422+
}
423+
return node;
424+
}
398425
return (
399426
readsState(node.object, scope, state, active) ??
400427
(node.computed ? readsState(node.property, scope, state, active) : null)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ describe('Strong stale deferred state updates', () => {
630630
'a tuple index',
631631
`export function A({ save }) { const tuple = useState(0); return <b onClick={async () => { await save(); tuple[1](tuple[0] + 1); }}>{tuple[0]}</b>; }`,
632632
],
633+
[
634+
'an Effect Event that writes its argument back to the same state',
635+
`export function A({ save }) { const [items, setItems] = useState([]); const add = useEffectEvent((list) => { setItems([...list, 1]); }); return <b onClick={async () => { await save(); add(items); }}>{items.length}</b>; }`,
636+
],
633637
])('rejects %s', (_label, body) => {
634638
expect(rejected(body)).toBe(STALE_STATE_UPDATE);
635639
});

0 commit comments

Comments
 (0)