Skip to content

Commit c26522c

Browse files
committed
fix(compiler): fall back to rootScope for missing nodeScopes entries and unwrap TS expressions in namespace hook recovery
Three sites in the optional-call hook recovery treated a missing nodeScopes entry as evidence the callee was locally scoped, mapping it to a null binding and failing the root-scope check. This caused rebuilt optional calls to proven setup hooks (readContext?.(), useTheme?.()) to be misclassified as pure projections by Strong mode, freezing context, state, and effects. Fall back to lexical.rootScope — matching the existing namespace path — so the conservative direction is to treat the call as module-scoped. The namespace optional-call branch also read callee.object and callee.property without unwrapTsExpr, so parenthesized or assertion-wrapped receivers like (Octane as any).useContext?.() never matched. Unwrap both before the type check so TS syntax does not prevent hook identification.
1 parent 44e1d29 commit c26522c

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,8 +4229,9 @@ function autoMemoBuiltinHookName(call, ctx) {
42294229
ctx.activityLexical ??= lexical;
42304230
const callee = unwrapTsExpr(call.callee);
42314231
if (callee?.type === 'Identifier') {
4232-
const scope = lexical.nodeScopes.get(callee) ?? lexical.nodeScopes.get(call);
4233-
const binding = scope === undefined ? null : lexical.resolveBinding(scope, callee.name);
4232+
const scope =
4233+
lexical.nodeScopes.get(callee) ?? lexical.nodeScopes.get(call) ?? lexical.rootScope;
4234+
const binding = lexical.resolveBinding(scope, callee.name);
42344235
const importedName = ctx.octaneImportLocals?.get(callee.name);
42354236
if (
42364237
importedName !== undefined &&
@@ -4242,21 +4243,19 @@ function autoMemoBuiltinHookName(call, ctx) {
42424243
if (binding === null && AUTO_MEMO_SETUP_HOOK_NAMES.has(callee.name)) return callee.name;
42434244
return null;
42444245
}
4245-
if (
4246-
(callee?.type === 'MemberExpression' || callee?.type === 'OptionalMemberExpression') &&
4247-
callee.object?.type === 'Identifier'
4248-
) {
4249-
const property = callee.computed ? callee.property?.value : callee.property?.name;
4246+
if (callee?.type === 'MemberExpression' || callee?.type === 'OptionalMemberExpression') {
4247+
const object = unwrapTsExpr(callee.object);
4248+
if (object?.type !== 'Identifier') return null;
4249+
const prop = unwrapTsExpr(callee.property);
4250+
const property = callee.computed ? prop?.value : prop?.name;
42504251
if (
42514252
typeof property === 'string' &&
42524253
AUTO_MEMO_SETUP_HOOK_NAMES.has(property) &&
4253-
ctx.octaneImportNamespaces?.has(callee.object.name)
4254+
ctx.octaneImportNamespaces?.has(object.name)
42544255
) {
42554256
const scope =
4256-
lexical.nodeScopes.get(callee.object) ??
4257-
lexical.nodeScopes.get(callee) ??
4258-
lexical.rootScope;
4259-
if (lexical.resolveBinding(scope, callee.object.name)?.scope === lexical.rootScope) {
4257+
lexical.nodeScopes.get(object) ?? lexical.nodeScopes.get(callee) ?? lexical.rootScope;
4258+
if (lexical.resolveBinding(scope, object.name)?.scope === lexical.rootScope) {
42604259
return property;
42614260
}
42624261
}
@@ -4380,11 +4379,9 @@ function autoMemoSetupHookFunctions(ctx) {
43804379
} else {
43814380
const callee = unwrapTsExpr(node.callee);
43824381
if (callee?.type === 'Identifier' && declarations.has(callee.name)) {
4383-
const scope = lexical.nodeScopes.get(callee) ?? lexical.nodeScopes.get(node);
4384-
if (
4385-
scope !== undefined &&
4386-
lexical.resolveBinding(scope, callee.name)?.scope === lexical.rootScope
4387-
) {
4382+
const scope =
4383+
lexical.nodeScopes.get(callee) ?? lexical.nodeScopes.get(node) ?? lexical.rootScope;
4384+
if (lexical.resolveBinding(scope, callee.name)?.scope === lexical.rootScope) {
43884385
outgoing.add(callee.name);
43894386
}
43904387
}
@@ -4440,8 +4437,8 @@ function autoMemoCallExecutesSetupHook(call, ctx) {
44404437
// the same name. The summary graph uses this same binding resolution for its
44414438
// transitive edges, so cycles reach a fixed point independent of source order.
44424439
const lexical = ctx.autoMemoHookLexical;
4443-
const scope = lexical.nodeScopes.get(callee) ?? lexical.nodeScopes.get(call);
4444-
const binding = scope === undefined ? null : lexical.resolveBinding(scope, name);
4440+
const scope = lexical.nodeScopes.get(callee) ?? lexical.nodeScopes.get(call) ?? lexical.rootScope;
4441+
const binding = lexical.resolveBinding(scope, name);
44454442
return binding?.scope === lexical.rootScope;
44464443
}
44474444

0 commit comments

Comments
 (0)