Skip to content

Commit 7b5812b

Browse files
committed
fix(compiler): use top-level module bindings in ambient-read classifier
isUnwitnessedAmbientRead used _moduleBoundNames, which collects every binding from every scope in the module (including nested function parameters and locals). A sibling function parameter named 'location' would incorrectly shadow the host global, causing @for rows reading location.pathname to keep PURE and stay stale after navigation. Replace with _moduleTopLevelBoundNames that collects only top-level module-scope bindings (declarations, function/class names, imports), so nested parameters in sibling functions no longer mask globals. The existing _moduleBoundNames set is retained for setupCanScheduleSelfUpdate, where the conservative all-scopes check is intentionally correct.
1 parent 4004610 commit 7b5812b

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5218,6 +5218,38 @@ const SETUP_PURE_NAMESPACE_MEMBERS = new Map([
52185218
['JSON', new Set(['parse', 'stringify'])],
52195219
]);
52205220

5221+
// Top-level module-scope bindings only: declarations, function/class names,
5222+
// and imports visible at the module's own scope. Parameters and locals of
5223+
// nested functions are excluded — they do not shadow globals for siblings.
5224+
function collectModuleTopLevelBoundNames(body) {
5225+
const names = new Set();
5226+
for (const statement of body) {
5227+
let declaration = statement;
5228+
if (
5229+
statement.type === 'ExportNamedDeclaration' ||
5230+
statement.type === 'ExportDefaultDeclaration'
5231+
) {
5232+
declaration = statement.declaration;
5233+
if (!declaration) continue;
5234+
}
5235+
switch (declaration.type) {
5236+
case 'VariableDeclaration':
5237+
for (const d of declaration.declarations || []) collectBindings(d.id, names);
5238+
break;
5239+
case 'FunctionDeclaration':
5240+
if (declaration.id) names.add(declaration.id.name);
5241+
break;
5242+
case 'ClassDeclaration':
5243+
if (declaration.id) names.add(declaration.id.name);
5244+
break;
5245+
case 'ImportDeclaration':
5246+
for (const spec of declaration.specifiers || []) if (spec.local) names.add(spec.local.name);
5247+
break;
5248+
}
5249+
}
5250+
return names;
5251+
}
5252+
52215253
// Every binding name the module declares anywhere. A global namespace shadowed
52225254
// by any local, parameter, or import is treated as an unknown callee.
52235255
function collectModuleBoundNames(ast) {
@@ -5635,7 +5667,7 @@ function isUnwitnessedAmbientRead(name, ctx) {
56355667
// `_$…` names are runtime helpers the compiler itself imports.
56365668
if (name.startsWith('_$')) return false;
56375669
if (ctx.mutableModuleBindings?.has(name)) return true;
5638-
const bound = ctx._moduleBoundNames;
5670+
const bound = ctx._moduleTopLevelBoundNames;
56395671
return bound !== undefined && !bound.has(name) && !IMMUTABLE_AMBIENT_GLOBALS.has(name);
56405672
}
56415673

@@ -10477,6 +10509,9 @@ function compileInternal(
1047710509
ast.body,
1047810510
ctx.moduleFunctionDeclarations,
1047910511
);
10512+
// Top-level module bindings for the ambient-read classifier: only names
10513+
// declared at the module's own scope can shadow a global for a @for body.
10514+
ctx._moduleTopLevelBoundNames = collectModuleTopLevelBoundNames(ast.body);
1048010515
// Global namespaces are only trusted by the setup checkpoint analysis when
1048110516
// no module binding shadows them (see setupCanScheduleSelfUpdate).
1048210517
ctx._moduleBoundNames = collectModuleBoundNames(ast);

‎packages/octane/tests/compiler/for-item-purity.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ describe('@for item-body purity with module and global reads', () => {
213213
'<li data-pick={pick} />',
214214
],
215215
['location in an @if test', '', '<li>@if (location.hash === item.href) {<b />}</li>'],
216+
[
217+
'location when a sibling function shadows the name',
218+
'function match(location, href) { return location.hash === href; }',
219+
'<li>@if (location.hash === item.href) {<b />}</li>',
220+
],
216221
['a globalThis property', '', '<li>@if ((globalThis as any).flag) {<b />}</li>'],
217222
])('declines a body reading %s', (_name, prelude, body) => {
218223
const flags = compileList(body, prelude);

0 commit comments

Comments
 (0)