fix(javascript): preserve dependencies for projected array sources (#12519)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 20:48:47 +00:00
1 parent 1ec32721af
commit 16e4eec122
5 files changed
+224 -31

No files matched your search

@@ -821,3 +821,177 @@ it.each([
).toBe(pointer);
},
);
it.each([
[
"later write",
"const box = { items: [1] };",
"const [selected] = box.items; box.items[0] = 2;",
["items", "0"],
"resolved",
],
[
"later replacement",
"const box = { items: [1] };",
"const [selected] = box.items; box.items = [2];",
["items", "0"],
"resolved",
],
[
"later replacement after assignment",
"const box = { items: [1] };",
"let selected; [selected] = box.items; box.items = [2];",
["items", "0"],
"resolved",
],
[
"later write through the original",
"const box = { items: [1] }; const alias = box;",
"const [selected] = alias.items; box.items[0] = 2;",
["items", "0"],
"resolved",
],
[
"later write through the alias",
"const box = { items: [1] }; const alias = box;",
"const [selected] = box.items; alias.items[0] = 2;",
["items", "0"],
"resolved",
],
[
"nested own-data receiver",
"const box = { nested: { items: [1] } }; const alias = box;",
'const [selected] = alias.nested["items"]; box.nested.items[0] = 2;',
["nested", "items", "0"],
"resolved",
],
[
"skipping an own slot of the projected array",
"const box = { items: [0, 1] }; const alias = box;",
"const [,selected] = alias.items; box.items[1] = 2;",
["items", "1"],
"resolved",
],
[
"earlier write",
"const box = { items: [1] };",
"box.items[0] = 2; const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier write through the original",
"const box = { items: [1] }; const alias = box;",
"box.items[0] = 2; const [selected] = alias.items;",
["items", "0"],
"candidate",
],
[
"earlier write through the alias",
"const box = { items: [1] }; const alias = box;",
"alias.items[0] = 2; const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier shortening",
"const box = { items: [1] }; const alias = box;",
"alias.items.length = 0; const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier receiver replacement",
"const box = { items: [1] }; const alias = box;",
"box.items = unknown; const [selected] = alias.items;",
["items", "0"],
"candidate",
],
[
"rebound source",
"let box = { items: [1] }; const alias = box; box = { items: [2] };",
"const [selected] = alias.items; alias.items[0] = 2;",
["items", "0"],
"candidate",
],
[
"getter",
"const box = { get items() { return [1]; } };",
"const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"inherited data",
"const box = { __proto__: { items: [1] } };",
"const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier getter replacement",
"const box = { items: [1] };",
'Object.defineProperty(box, "items", { get() { return [2]; } }); const [selected] = box.items;',
["items", "0"],
"candidate",
],
[
"earlier own iterator replacement",
"const box = { items: [1] }; const alias = box;",
"alias.items[Symbol.iterator] = function* () { yield 2; }; const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier custom array prototype",
"const box = { items: [1] };",
"Object.setPrototypeOf(box.items, { *[Symbol.iterator]() { yield 2; } }); const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier shared next replacement",
"const box = { items: [1] };",
"Object.getPrototypeOf([].values()).next = () => ({ done: true }); const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"opaque receiver escape",
"const box = { items: [1] };",
"consume(box.items); const [selected] = box.items;",
["items", "0"],
"candidate",
],
[
"earlier target setter",
"const box = { items: [1, 2] }; const target = { set value(_) { box.items[1] = 3; } };",
"let selected; [target.value, selected] = box.items;",
["items", "1"],
"candidate",
],
[
"effectful key",
"const box = { undefined: [1] };",
"const [selected] = box[void (box.undefined.length = 0)];",
["undefined", "0"],
"candidate",
],
] as const)(
"uses the projected array's own data and iteration for %s",
(_name, setup, read, propertyPath, resolution) => {
const graph = graphForJavaScript(`${setup} ${read}`);
const selected = graph.nodes.find(
({ kind, label }) => kind === "binding" && label === "selected",
);
const edge = graph.relations.find(
({ relation, target_node_id }) =>
relation === "destructures" && target_node_id === selected?.node_id,
);
expect(edge).toMatchObject({ resolution });
expect(
graph.nodes.find(({ node_id }) => node_id === edge?.source_node_id)
?.properties.property_path,
).toEqual(propertyPath);
},
);
@@ -2074,7 +2074,19 @@ export function* collectSemanticMemberMutationsSteps(
continue;
}
}
const slot = semanticSelectedReferenceSlot(reference, state);
const { mutation, node, projection } = reference;
// A root array pattern acquires its iterator from the completed RHS.
// A nested iterator can run after earlier pattern reads and defaults.
const capturesRootPattern =
reference.iterationOnly &&
projection.length === 0 &&
((t.isVariableDeclarator(mutation) && mutation.init === node) ||
(t.isAssignmentExpression(mutation) && mutation.right === node));
const slot = semanticSelectedReferenceSlot(
reference,
state,
capturesRootPattern ? node : undefined,
);
// Custom iteration can yield the iterable itself as well as any child.
// Treat its root as escaped, including writes through yielded references.
const knownArray = semanticKnownArrayIteration(
@@ -147,13 +147,11 @@ const collectDestructuring = (
const source = expressionObjectIdentity(initializer, state);
const rootArray = t.isArrayPattern(root);
let ownSlot =
initializer != null &&
source.path !== null &&
(!rootArray || source.path.length === 0)
initializer != null && source.path !== null
? readPresence(
source.bindingId,
initializer,
rootArray ? "array-iteration" : "property",
rootArray ? source.path : undefined,
)
: undefined;
// An unsupported property still occupies its place in the pattern's
@@ -82,10 +82,26 @@ export const createSemanticReadPresenceQuery = (
return origin ?? undefined;
};
const ownDataAtPath = (value: t.Node, path: readonly string[]) => {
let receiver = value;
for (const key of path) {
if (!t.isObjectExpression(receiver)) return undefined;
let read = readers.get(receiver);
if (read === undefined) {
read = staticObjectPropertyReader(receiver);
readers.set(receiver, read);
}
const property = read(key);
if (property.status !== "explicit") return undefined;
receiver = unwrapJavaScriptExpression(property.value).node;
}
return receiver;
};
return (
bindingId: string | null,
capture: t.Node,
source: "property" | "array-iteration" = "property",
iterationPath?: readonly string[],
) => {
const binding =
bindingId === null ? undefined : state.bindingsById.get(bindingId);
@@ -117,8 +133,11 @@ export const createSemanticReadPresenceQuery = (
)
return undefined;
const { value } = origin;
if (source === "array-iteration") {
if (!t.isArrayExpression(value)) return undefined;
if (iterationPath !== undefined) {
// The RHS must select an own data value before numeric array slots can
// explain the binding. Temporal checks still use the full origin path.
if (!t.isArrayExpression(ownDataAtPath(value, iterationPath)))
return undefined;
const array = evaluateSemanticExpression(
capture,
state,
@@ -131,8 +150,9 @@ export const createSemanticReadPresenceQuery = (
)
return undefined;
return (path: readonly string[]): boolean => {
const [key] = path;
if (key === undefined || path.length !== 1) return false;
if (path.length !== iterationPath.length + 1) return false;
const key = path[iterationPath.length];
if (key === undefined) return false;
const index = semanticArrayIndex(key);
const slot = index === null ? undefined : array.items[index];
return (
@@ -148,26 +168,9 @@ export const createSemanticReadPresenceQuery = (
};
}
if (!t.isObjectExpression(value)) return undefined;
return (path: readonly string[]): boolean => {
if (path.length === 0) return false;
let receiver: t.Node = value;
for (const key of path) {
if (!t.isObjectExpression(receiver)) return false;
let read = readers.get(receiver);
if (read === undefined) {
read = staticObjectPropertyReader(receiver);
readers.set(receiver, read);
}
const property = read(key);
if (property.status !== "explicit") return false;
receiver = unwrapJavaScriptExpression(property.value).node;
}
return !capturedBindingPathMayHaveChanged(
origin.binding,
path,
capture,
state,
);
};
return (path: readonly string[]): boolean =>
path.length > 0 &&
ownDataAtPath(value, path) !== undefined &&
!capturedBindingPathMayHaveChanged(origin.binding, path, capture, state);
};
};
@@ -41,6 +41,10 @@ cliTest(
const skipped = ["skip", "first"];
const [,elided] = skipped;
skipped[1] = "later";
const projectedSource = { items: ["projected"] };
const projectedAlias = projectedSource;
const [projected] = projectedAlias.items;
projectedSource.items[0] = "later";
const box = { items: [] };
const iterator = box.items.values();
const prototype = Reflect.getPrototypeOf(iterator);
@@ -69,6 +73,7 @@ cliTest(
["ordinary", ["0"]],
["arrayAssigned", ["0"]],
["elided", ["1"]],
["projected", ["items", "0"]],
["selected", ["0"], "candidate"],
] as const) {
const binding = graph.nodes.find(
@@ -104,6 +109,7 @@ cliTest(
"ordinary",
"arrayAssigned",
"elided",
"projected",
"selected",
]) {
const binding = graph.nodes.find(