Skip to content

Commit 9fa4963

Browse files
committed
fix(compiler): track const/let function-expression hooks in Strong setup graph
autoMemoModuleFunctionDeclarations only collected FunctionDeclaration nodes, so a same-module `const useTheme = () => useContext(...)` was never entered into the setup-hook dependency graph. autoMemoCallExecutesSetupHook then returned false for calls to such hooks, letting Strong mode cache/project the result and freeze the underlying context, state, or effect value. Extend the collection to VariableDeclaration statements whose declarators have ArrowFunctionExpression or FunctionExpression initializers, storing the function node so the existing walk logic correctly descends into its body while still skipping nested functions.
1 parent 7a8ab83 commit 9fa4963

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎packages/octane/src/compiler/compile.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4280,6 +4280,16 @@ function autoMemoModuleFunctionDeclarations(ctx) {
42804280
if (declaration?.type === 'FunctionDeclaration' && declaration.id?.type === 'Identifier') {
42814281
declarations.set(declaration.id.name, declaration);
42824282
}
4283+
if (declaration?.type === 'VariableDeclaration') {
4284+
for (const declarator of declaration.declarations) {
4285+
if (declarator.id?.type === 'Identifier') {
4286+
const init = unwrapTsExpr(declarator.init);
4287+
if (init?.type === 'ArrowFunctionExpression' || init?.type === 'FunctionExpression') {
4288+
declarations.set(declarator.id.name, init);
4289+
}
4290+
}
4291+
}
4292+
}
42834293
}
42844294
ctx.autoMemoModuleFunctionDeclarations = declarations;
42854295
return declarations;

‎packages/octane/tests/_fixtures/for-strong.tsrx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,24 @@ export function StrongCyclicContextReaders() @{
264264
</div>
265265
}
266266

267+
const useConstTheme = (context: typeof StrongTheme): string => useContext(context);
268+
269+
export function StrongConstHookContextRows() @{
270+
const [theme, setTheme] = useState('first');
271+
const [items] = useState([{ id: 1 }, { id: 2 }]);
272+
<div>
273+
<button id="strong-const-hook-context-update" onClick={() => setTheme('second')}>
274+
update
275+
</button>
276+
<StrongTheme.Provider value={theme}>
277+
@for (const item of items.slice(); key item.id) {
278+
const value = useConstTheme(StrongTheme);
279+
<span class="strong-const-hook-context-row">{value as string}</span>
280+
}
281+
</StrongTheme.Provider>
282+
</div>
283+
}
284+
267285
function useShadowedTheme(context: typeof StrongTheme): string {
268286
return useContext(context);
269287
}

‎packages/octane/tests/for.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
CapturedSnapshotList,
4949
StrongAliasedContextRows,
5050
StrongCallableDependencies,
51+
StrongConstHookContextRows,
5152
StrongConstructedResultProp,
5253
StrongCustomHookContext,
5354
StrongCyclicContextReaders,
@@ -687,6 +688,20 @@ describe('Strong memoization preserves dependency and setup semantics', () => {
687688
r.unmount();
688689
});
689690

691+
it('tracks const-declared function-expression hooks as setup-bearing in keyed rows', () => {
692+
const r = mount(StrongConstHookContextRows);
693+
expect(r.findAll('.strong-const-hook-context-row').map((row) => row.textContent)).toEqual([
694+
'first',
695+
'first',
696+
]);
697+
r.click('#strong-const-hook-context-update');
698+
expect(r.findAll('.strong-const-hook-context-row').map((row) => row.textContent)).toEqual([
699+
'second',
700+
'second',
701+
]);
702+
r.unmount();
703+
});
704+
690705
it('uses SameValue semantics for newly cached constructor inputs', () => {
691706
const Projector = class {
692707
result: string;

0 commit comments

Comments
 (0)