Skip to content

Commit 7435403

Browse files
committed
fix(compiler): detect stale derived-state arguments in Effect Events
Two independent gates silenced OCTANE_STRONG_STALE_STATE_UPDATE for derived-state values passed to Effect Events: 1. The follow condition only entered an Effect Event body when an argument was snapshot or state-tuple, so a derived-state argument after an await was never walked. Added derived-state to the condition. 2. freshRead returned true for every derived-state binding inside an Effect Event, including parameters that carry a stale caller value. Removed the blanket early return and instead trace through the declaration initializer (stored on the binding) so that locals derived from captured component state remain fresh while parameters and locals derived from stale parameters are correctly flagged.
1 parent 96da3a6 commit 7435403

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,14 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
26552655
// initializer check.
26562656
const states =
26572657
declaration.id?.type === 'Identifier' ? projectedStates(initial, scope) : null;
2658-
if (states !== null) bind(declaration.id, { kind: 'derived-state', states, prop });
2658+
if (states !== null)
2659+
bind(declaration.id, {
2660+
kind: 'derived-state',
2661+
states,
2662+
prop,
2663+
init: initial,
2664+
initScope: scope,
2665+
});
26592666
else if (prop) bindPropPattern(declaration.id, bind);
26602667
}
26612668
}
@@ -3263,7 +3270,11 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
32633270
// Prop-state intent belongs to the component's own initializer. A
32643271
// helper's parameter is supplied by callers, independently of spelling.
32653272
if (value?.kind === 'prop') return OTHER_BINDING;
3266-
if (value?.kind === 'derived-state' && value.prop) return { ...value, prop: false };
3273+
if (value?.kind === 'derived-state') {
3274+
if (value.prop) return { kind: 'derived-state', states: value.states, prop: false };
3275+
if (value.init != null)
3276+
return { kind: 'derived-state', states: value.states, prop: value.prop };
3277+
}
32673278
return value;
32683279
});
32693280
}
@@ -4624,7 +4635,12 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
46244635
const args = argumentValues(node.arguments, scope);
46254636
if (
46264637
(executionPhase === 'deferred' && !effectEvent) ||
4627-
args?.some((value) => value?.kind === 'snapshot' || value?.kind === 'state-tuple')
4638+
args?.some(
4639+
(value) =>
4640+
value?.kind === 'snapshot' ||
4641+
value?.kind === 'state-tuple' ||
4642+
value?.kind === 'derived-state',
4643+
)
46284644
) {
46294645
if (effectEvent) visitEffectEventCallback(callback, callee, executionPhase, args);
46304646
else visitCallable(callback, callee, executionPhase, args);

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,12 @@ export function createStrongStatePolicy(api) {
322322
};
323323

324324
// In an Effect Event's own body, state captured from the component is the
325-
// latest committed value. A parameter can still carry a stale snapshot; a
326-
// local derived inside the body is treated as current.
325+
// latest committed value. Variables defined at or inside the Effect Event
326+
// boundary are checked normally; derived locals trace through their
327+
// initializers in readsState so that captured-state derivations stay fresh
328+
// while parameter-derived locals are flagged.
327329
function freshRead(scope, name) {
328330
if (!freshCaptures) return false;
329-
if (resolve(scope, name)?.kind === 'derived-state') return true;
330331
for (let current = resolveScope(scope, name); current != null; current = current.parent) {
331332
if (current.effectEventBoundary === true) return false;
332333
}
@@ -417,9 +418,14 @@ export function createStrongStatePolicy(api) {
417418
const node = unwrap(value);
418419
if (node == null || node.type?.startsWith('TS')) return null;
419420
switch (node.type) {
420-
case 'Identifier':
421+
case 'Identifier': {
421422
if (freshRead(scope, node.name)) return null;
422-
return readsBinding(resolve(scope, node.name), state, active) ? node : null;
423+
const binding = resolve(scope, node.name);
424+
if (freshCaptures && binding?.kind === 'derived-state' && binding.init != null) {
425+
return readsState(binding.init, binding.initScope, state, active);
426+
}
427+
return readsBinding(binding, state, active) ? node : null;
428+
}
423429
case 'MemberExpression': {
424430
const root = memberRoot(node);
425431
if (

0 commit comments

Comments
 (0)