Skip to content

Commit 391b5fe

Browse files
committed
fix(compiler): scan param defaults and destructuring keys in free-variable walk, and gate callback hoisting on dev/profile
Two bugs in the capture-free hook callback hoisting pass: 1. collectFreeIdentifiers walked only function bodies after collecting param bindings, skipping default initializers and computed destructuring keys. A callback like (s, n = props.x) => ... whose only component capture sits in a default would appear capture-free, get lifted to module scope, and throw ReferenceError when the default evaluated. Fix: walk each param with the binding-augmented scope after collectBindings. Similarly, walk destructuring VariableDeclarator id patterns (with a local scope copy that includes the binding names) so defaults and computed keys are scanned. 2. hoistCaptureFreeHookCallbacks was gated on !hmrEnabled alone, but the neighboring autoMemo and inlineHookMemo tiers require !hmrEnabled && !devEnabled && !profileEnabled. The call also ran before devEnabled/profileEnabled were computed. Fix: move the call after those variables and extend the gate to match the sibling tiers.
1 parent 0f8eb9b commit 391b5fe

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,9 @@ function collectFreeIdentifiers(root, initiallyBound, ignoreNodes = null) {
26912691
for (const p of n.params || []) collectBindings(p, newScope);
26922692
// `function name(){}` introduces its own name into the body scope too.
26932693
if (n.id) collectBindings(n.id, newScope);
2694+
// Walk params so default initializers and computed destructuring keys
2695+
// are scanned for free variables (collectBindings only extracts names).
2696+
for (const p of n.params || []) walk(p, newScope);
26942697
walk(n.body, newScope);
26952698
return;
26962699
}
@@ -2710,8 +2713,17 @@ function collectFreeIdentifiers(root, initiallyBound, ignoreNodes = null) {
27102713
return;
27112714
}
27122715

2713-
// VariableDeclarator's `id` is a binding, only walk the init.
2716+
// VariableDeclarator — the `id` pattern is a binding site, but
2717+
// default initializers and computed destructuring keys inside it are
2718+
// expressions that may reference free variables. For destructuring
2719+
// patterns, collect the binding names into a local scope copy first
2720+
// so they are not themselves reported as free.
27142721
if (t === 'VariableDeclarator') {
2722+
if (n.id && n.id.type !== 'Identifier') {
2723+
const patternScope = new Set(scope);
2724+
collectBindings(n.id, patternScope);
2725+
walk(n.id, patternScope);
2726+
}
27152727
walk(n.init, scope);
27162728
return;
27172729
}
@@ -5593,10 +5605,6 @@ function compileInternal(source, filename, options, analyzedAst, mode, bundlerMe
55935605
);
55945606
}
55955607
const hmrEnabled = hmrDialect !== false;
5596-
// Runs AFTER dependency inference so materialised dep arrays are already in
5597-
// place: a lifted callback is module scope, which inference reads as
5598-
// non-reactive, and re-ordering the two would change what it infers.
5599-
ast = hoistCaptureFreeHookCallbacks(ast, { enabled: !hmrEnabled });
56005608
// Opt-in template-origin recording (`inspect: true`, client mode only): for
56015609
// every span baked into a hoisted template's HTML string (tag names, static
56025610
// attributes, static text), capture the authored source range that produced
@@ -5614,6 +5622,14 @@ function compileInternal(source, filename, options, analyzedAst, mode, bundlerMe
56145622
// emits component/hook identity metadata but no hydration-warning expandos.
56155623
// Server compilation returns above, so profile metadata is client-only.
56165624
const profileEnabled = !!(options && options.profile);
5625+
// Runs AFTER dependency inference so materialised dep arrays are already in
5626+
// place: a lifted callback is module scope, which inference reads as
5627+
// non-reactive, and re-ordering the two would change what it infers.
5628+
// Production-only like the inline hook-memo and autoMemo tiers: dev/profile
5629+
// builds observe every entry at its authored site.
5630+
ast = hoistCaptureFreeHookCallbacks(ast, {
5631+
enabled: !hmrEnabled && !devEnabled && !profileEnabled,
5632+
});
56175633
// React-Compiler-style component-region and keyed-list caching. Always on for
56185634
// production client compilation — HMR can replace component contracts in
56195635
// place, and dev/profiling observe every entry, so those modes decline it.

0 commit comments

Comments
 (0)