Skip to content

Commit 52f91fd

Browse files
committed
fix(compiler): reject var in unbraced branch body in Strong localInit
The scope-based check for whether a var is declared directly in a function body incorrectly accepted vars inside unbraced if/while/do bodies, because those control-flow statements do not create a new scope. This meant `if (cond) var ready = await load();` was treated as always-initialized, allowing a zero-delay effect-update bypass. Replace the scope check with an AST-structural check that verifies the VariableDeclarator belongs to a VariableDeclaration that is a direct child statement of a function/module body.
1 parent eac9426 commit 52f91fd

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

‎packages/octane/src/compiler/strong-effects.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ function functionScopeOf(scope) {
5353
export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
5454
const { nodeScopes, declarators } = analysis;
5555
let declaratorInfo = null;
56+
let directBodyVars = null;
57+
function isDirectBodyVar(decl) {
58+
if (directBodyVars === null) {
59+
directBodyVars = new WeakSet();
60+
const mark = (statements) => {
61+
for (const stmt of statements) {
62+
if (stmt.type === 'VariableDeclaration' && stmt.kind === 'var') {
63+
for (const d of stmt.declarations ?? []) directBodyVars.add(d);
64+
}
65+
}
66+
};
67+
mark(ast.body ?? []);
68+
for (const { node } of analysis.functions ?? []) mark(node.body?.body ?? []);
69+
}
70+
return directBodyVars.has(decl);
71+
}
5672
// undefined: not an analyzed reference; null: an unshadowed global.
5773
function bindingOf(identifier) {
5874
const scope = nodeScopes.get(identifier);
@@ -300,10 +316,7 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
300316
const fn = functionScopeOf(scope);
301317
if (info === null || functionScopeOf(binding.scope) !== fn) return null;
302318
if (info.kind === 'var') {
303-
// The function body is a block scope directly inside the function scope.
304-
const declScope = nodeScopes.get(info.decl);
305-
const direct = declScope === fn || (declScope?.kind === 'block' && declScope.parent === fn);
306-
if (!direct || node.start < info.decl.end) return null;
319+
if (!isDirectBodyVar(info.decl) || node.start < info.decl.end) return null;
307320
} else if (info.kind !== 'const' && info.kind !== 'let') {
308321
return null;
309322
}

0 commit comments

Comments
 (0)