Skip to content

Commit e654527

Browse files
committed
fix(compiler): close local-callee bypass in plainCalleeIsMemoizable and add moduleFunctionBindings to autoMemoCallsitesSafe
Bug 1: plainCalleeIsMemoizable admitted any non-hook Identifier callee, but locally-declared names (const read = () => item.read(); read()) can wrap the member-callee hazard the veto intends to catch. Add collectSurfaceDeclaredNames to collect names declared at the walked surface (matching containsRenderCall's function-body boundary) and exclude them from the plain-callee pass-through. Bug 2: The autoMemoCallsitesSafe free-identifier loop checked importedNames, defaultMemoBindings, and componentInfo but not moduleFunctionBindings, causing components that call same-module function helpers to keep currentAutoMemoCallsitesSafe false and block child callsite memoization. Add the missing moduleFunctionBindings check.
1 parent 72b5bbb commit e654527

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,7 @@ function containsAutoMemoContextRead(root, ctx) {
26332633
* other than memo staleness (SSR item identity) must leave it off.
26342634
*/
26352635
function containsRenderCall(stmts, allowPlainCallee = false) {
2636+
const localNames = allowPlainCallee ? collectSurfaceDeclaredNames(stmts) : null;
26362637
let found = false;
26372638
const seen = new WeakSet();
26382639
function walk(n) {
@@ -2659,7 +2660,7 @@ function containsRenderCall(stmts, allowPlainCallee = false) {
26592660
t === 'NewExpression' ||
26602661
t === 'TaggedTemplateExpression'
26612662
) {
2662-
if (allowPlainCallee && plainCalleeIsMemoizable(n)) {
2663+
if (allowPlainCallee && plainCalleeIsMemoizable(n, localNames)) {
26632664
// The callee itself is a witnessed binding; its ARGUMENTS still carry
26642665
// the render-value contract, so `fmt(row.get())` stays disqualified.
26652666
walk(n.arguments);
@@ -2715,11 +2716,48 @@ function isHookCalleeName(name) {
27152716
* and React Compiler key on, and it is what the blanket call veto was
27162717
* incidentally covering here.
27172718
*/
2718-
function plainCalleeIsMemoizable(node) {
2719+
function plainCalleeIsMemoizable(node, localNames) {
27192720
if (node.type !== 'CallExpression' && node.type !== 'OptionalCallExpression') return false;
27202721
const callee = node.callee;
27212722
if (callee?.type !== 'Identifier') return false;
2722-
return !isHookCalleeName(callee.name);
2723+
if (isHookCalleeName(callee.name)) return false;
2724+
if (localNames?.has(callee.name)) return false;
2725+
return true;
2726+
}
2727+
2728+
/**
2729+
* Collect names declared at the surface level of `stmts`, matching the walk
2730+
* boundary of `containsRenderCall` (function bodies are NOT entered).
2731+
* Locally-declared callees can wrap opaque member calls through closures
2732+
* (`const read = () => item.read(); read()`) and must not be treated as the
2733+
* free module-level or imported identifiers `plainCalleeIsMemoizable` admits.
2734+
*/
2735+
function collectSurfaceDeclaredNames(stmts) {
2736+
const names = new Set();
2737+
const seen = new WeakSet();
2738+
function walk(n) {
2739+
if (!n || typeof n !== 'object') return;
2740+
if (Array.isArray(n)) {
2741+
for (const x of n) walk(x);
2742+
return;
2743+
}
2744+
const t = n.type;
2745+
if (!t) return;
2746+
if (seen.has(n)) return;
2747+
seen.add(n);
2748+
if (t === 'ArrowFunctionExpression' || t === 'FunctionExpression') return;
2749+
if (t === 'FunctionDeclaration') {
2750+
if (n.id?.type === 'Identifier') names.add(n.id.name);
2751+
return;
2752+
}
2753+
if (t === 'VariableDeclarator') collectPatternNames(n.id, names);
2754+
for (const key in n) {
2755+
if (AST_WALK_SKIP_KEYS.has(key)) continue;
2756+
walk(n[key]);
2757+
}
2758+
}
2759+
for (const s of stmts) walk(s);
2760+
return names;
27232761
}
27242762

27252763
/**
@@ -5291,7 +5329,8 @@ function compileInternal(source, filename, options, analyzedAst, mode, bundlerMe
52915329
ctx.importNamespaceNames.has(name) ||
52925330
(!ctx.importedNames.has(name) &&
52935331
!ctx.defaultMemoBindings.has(name) &&
5294-
!ctx.componentInfo.has(name))
5332+
!ctx.componentInfo.has(name) &&
5333+
!ctx.moduleFunctionBindings?.has(name))
52955334
) {
52965335
autoMemoCallsitesSafe = false;
52975336
break;

0 commit comments

Comments
 (0)