fix(javascript): honor outer lexical order through standalone blocks (#12701)

fix(javascript): carry lexical timing through standalone blocks
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 21:24:20 -05:00
1 parent 2245d25aaa
commit c0ee832a30
3 files changed
+76 -26

No files matched your search

@@ -2759,6 +2759,30 @@ it.each([
["1"],
"candidate",
],
[
"let target in a standalone block before an outer declaration",
'let selected;const xs=[1,"one"];{[fixed,selected]=xs;}let fixed;',
["1"],
"candidate",
],
[
"let target in multiple standalone blocks before a function-local declaration",
'function run(){let selected;const xs=[1,"one"];{{[fixed,selected]=xs;}}let fixed;}',
["1"],
"candidate",
],
[
"let target in a standalone block after the outer declaration",
'let selected;let fixed;const xs=[1,"one"];{[fixed,selected]=xs;}',
["1"],
"resolved",
],
[
"deferred callable declared inside an earlier standalone block",
'let read;{read=()=>{let selected;const xs=[1,"one"];[fixed,selected]=xs;};}let fixed;read();',
["1"],
"resolved",
],
[
"read before an uninitialized target",
"let selected;let later;const xs=[1,2,3];[selected,fixed,later]=xs;let fixed;",
@@ -41,7 +41,7 @@ import {
} from "./javascriptSemanticState.js";
interface StatementPosition {
readonly body: readonly t.Statement[];
readonly owner: t.Program | t.BlockStatement;
readonly index: number;
readonly declaratorIndex: number;
}
@@ -158,7 +158,7 @@ const statementPosition = (
return index < 0
? null
: {
body: parent.body,
owner: parent,
index,
declaratorIndex:
t.isVariableDeclaration(statement) && declarator !== undefined
@@ -171,7 +171,7 @@ const positionPrecedes = (
left: StatementPosition,
right: StatementPosition,
): boolean =>
left.body === right.body &&
left.owner === right.owner &&
(left.index < right.index ||
(left.index === right.index &&
left.declaratorIndex >= 0 &&
@@ -181,7 +181,7 @@ const sameStatementPosition = (
left: StatementPosition,
right: StatementPosition,
): boolean =>
left.body === right.body &&
left.owner === right.owner &&
left.index === right.index &&
left.declaratorIndex === right.declaratorIndex;
@@ -777,7 +777,7 @@ const computeReferenceInitializerPosition = (
return directMutationPosition(parent, parents);
if (t.isFunction(parent))
return t.isBlockStatement(parent.body)
? { body: parent.body.body, index: -1, declaratorIndex: -1 }
? { owner: parent.body, index: -1, declaratorIndex: -1 }
: null;
if (
parent === undefined ||
@@ -814,7 +814,7 @@ const initializerPosition = (
reference = false,
): StatementPosition | null => {
if (initializer.entryBody !== undefined)
return { body: initializer.entryBody.body, index: -1, declaratorIndex: -1 };
return { owner: initializer.entryBody, index: -1, declaratorIndex: -1 };
return reference
? referenceInitializerPosition(initializer.node, parents)
: directInitializerPosition(initializer.node, parents);
@@ -986,7 +986,16 @@ export const semanticBindingMayRejectAssignmentAt = (
if (position === null || end === null) return false;
// The RHS runs before any binding is initialized. Defaults inside the
// binding pattern itself can run after earlier targets are initialized.
return initializer === declarator || positionPrecedes(position, end);
if (initializer === declarator) return true;
// A standalone block executes at its own position in the parent list.
// Stop at scopes that are not themselves statements in an ordered list.
let before: StatementPosition | null = position;
while (before !== null && before.owner !== end.owner) {
before = t.isBlockStatement(before.owner)
? statementPosition(before.owner, parentsByNode)
: null;
}
return before !== null && positionPrecedes(before, end);
};
/** Repeated member reads share unknown ordering, while retaining their source sites. */
@@ -1257,7 +1266,7 @@ const moduleFunctionFirstRead = (
if (
effect.node === undefined ||
position === null ||
position.body !== body
position.owner.body !== body
) {
bound = null;
break;
@@ -1320,7 +1329,7 @@ const moduleFunctionFirstRead = (
for (const effect of state?.prototypeMutationSites ?? []) {
if (effect.owner !== owner && effect.owner !== "Object") continue;
const position = directMutationPosition(effect.node, parents);
if (position === null || position.body !== body) {
if (position === null || position.owner.body !== body) {
bound = null;
break;
}
@@ -1346,7 +1355,7 @@ const moduleFunctionFirstRead = (
if (
position.position === null ||
position.repeatingLoop ||
position.position.body !== body
position.position.owner.body !== body
)
return null;
if (position.position.index > bound.index) return null;
@@ -1373,7 +1382,7 @@ const moduleFunctionFirstRead = (
if (
position.position === null ||
position.repeatingLoop ||
position.position.body !== body
position.position.owner.body !== body
)
return null;
if (position.position.index < bound.index) return identity;
@@ -1757,11 +1766,11 @@ export const semanticBindingAtRead = (
moduleConstBinding &&
sourcePosition !== null &&
functionPosition !== null &&
sourcePosition.body === functionPosition.body &&
sourcePosition.owner === functionPosition.owner &&
t.isFunctionDeclaration(enclosingFunction)
? moduleFunctionFirstRead(
enclosingFunction,
sourcePosition.body,
sourcePosition.owner.body,
parents,
state,
)
@@ -1770,7 +1779,7 @@ export const semanticBindingAtRead = (
moduleConstBinding &&
sourcePosition !== null &&
functionPosition !== null &&
sourcePosition.body === functionPosition.body &&
sourcePosition.owner === functionPosition.owner &&
(t.isFunctionDeclaration(enclosingFunction)
? firstFunctionRead !== null && sourcePosition.index < firstFunctionRead
: positionPrecedes(sourcePosition, functionPosition));
@@ -1778,7 +1787,7 @@ export const semanticBindingAtRead = (
repeatingLoop ||
sourcePosition === null ||
position === null ||
sourcePosition.body !== position.body;
sourcePosition.owner !== position.owner;
if (uncertainOrder && moduleConstInitializedBeforeActivation)
// Initialization coverage does not order a capture in another activation.
return { status: "unresolved", binding, hasDefiniteInitializer: true };
@@ -1837,7 +1846,7 @@ const selectInitializers = (
const preceding: typeof origins = [];
for (const origin of origins) {
const source = origin.position;
if (source === null || source.body !== position.body) return undefined;
if (source === null || source.owner !== position.owner) return undefined;
if (positionPrecedes(source, position)) preceding.push(origin);
else if (!positionPrecedes(position, source)) {
if (
@@ -88,6 +88,26 @@ const declaratorOrderSource = `
let readLater = ([later, uninitializedLater] = cells), later;
`;
const outerDeclarationSource = `
let ready, afterReady, beforeOuterLet, afterOuterLet;
const cells = ["before", 1, "unreached"];
{ [ready, afterReady] = cells; }
{ [beforeOuterLet, pending, afterOuterLet] = cells; }
let pending;
`;
const additionalFixtures = [
["iterator-close.js", iteratorCloseSource],
["array-prototype-alias.js", arrayPrototypeAliasSource],
["abrupt-object-pattern.js", abruptObjectPatternSource],
["first-default.js", firstDefaultSource],
["immutable-target.js", immutableTargetSource],
["class-name-target.js", classNameTargetSource],
["uninitialized-target.js", uninitializedTargetSource],
["declarator-order.js", declaratorOrderSource],
["outer-let.js", outerDeclarationSource],
] as const;
const expectedDependencies = [
["declared", ["a"]],
["a", ["a"]],
@@ -104,6 +124,9 @@ const expectedDependencies = [
["afterUninitialized", ["2"], "candidate"],
["initializedEarlier", ["1"]],
["uninitializedLater", ["1"], "candidate"],
["afterReady", ["1"]],
["beforeOuterLet", ["0"]],
["afterOuterLet", ["2"], "candidate"],
["arrayAssigned", ["0"]],
["elided", ["1"]],
["projected", ["items", "0"]],
@@ -141,6 +164,9 @@ const tracedBindings = [
"afterUninitialized",
"initializedEarlier",
"uninitializedLater",
"afterReady",
"beforeOuterLet",
"afterOuterLet",
"arrayAssigned",
"elided",
"projected",
@@ -227,16 +253,7 @@ cliTest(
[selected] = after;
`,
);
for (const [path, source] of [
["iterator-close.js", iteratorCloseSource],
["array-prototype-alias.js", arrayPrototypeAliasSource],
["abrupt-object-pattern.js", abruptObjectPatternSource],
["first-default.js", firstDefaultSource],
["immutable-target.js", immutableTargetSource],
["class-name-target.js", classNameTargetSource],
["uninitialized-target.js", uninitializedTargetSource],
["declarator-order.js", declaratorOrderSource],
] as const)
for (const [path, source] of additionalFixtures)
await writeFile(join(root, path), source);
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],