fix(javascript): preserve object reads inside outer array patterns (#12523)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 20:56:33 +00:00
1 parent 3dfd7c2f0d
commit 77ffbe3da5
4 files changed
+169 -24

No files matched your search

@@ -995,3 +995,125 @@ it.each([
).toEqual(propertyPath);
},
);
it.each([
[
"inline object",
'const box=[{mode:"initial"}];const [{mode:selected}]=box;box[0].mode="later";',
["0", "mode"],
"resolved",
],
[
"assigned object",
'const box=[{mode:"initial"}];let selected;[{mode:selected}]=box;box[0].mode="later";',
["0", "mode"],
"resolved",
],
[
"nested object",
'const box=[{nest:{mode:"initial"}}];const alias=box;const [{nest:{mode:selected}}]=alias;box[0].nest.mode="later";',
["0", "nest", "mode"],
"resolved",
],
[
"projected object",
'const box={items:[{mode:"initial"}]};const alias=box;const [{mode:selected}]=alias.items;box.items[0].mode="later";',
["items", "0", "mode"],
"resolved",
],
[
"an earlier inline object",
'const box=[{}, "initial"];const [{},selected]=box;box[1]="later";',
["1"],
"resolved",
],
[
"an earlier null object pattern input",
'const box=[null, "initial"];const [{},selected]=box;box[1]="later";',
["1"],
"candidate",
],
[
"referenced object",
'const item={mode:"initial"};const box=[item];const [{mode:selected}]=box;item.mode="later";',
["0", "mode"],
"candidate",
],
[
"earlier slot write",
'const box=[{mode:"initial"}];box[0].mode="other";const [{mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"custom iterator",
'const box=[{mode:"initial"}];box[Symbol.iterator]=function*(){yield {mode:"other"};};const [{mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"custom next",
'const box=[{mode:"initial"}];Object.getPrototypeOf([].values()).next=function(){return {value:{mode:"other"},done:false}};const [{mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"earlier object getter",
'const box=[{mode:"initial",get key(){box[0].mode="other";return 0;}}];const [{key:before,mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"earlier nested default",
'const box=[{mode:"initial"}];const [{key:before=(box[0].mode="other"),mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"earlier computed key",
'const box=[{key:0,mode:"initial"}];const [{[void(box[0].mode="other")]:before,mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"earlier member assignment target",
'const box=[{key:0,mode:"initial"}];const target={set key(_){box[0].mode="other";}};let selected;[{key:target.key,mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"earlier computed assignment target",
'const box=[{key:0,mode:"initial"}];const target={};let selected;[{key:target[(box[0].mode="other","key")],mode:selected}]=box;',
["0", "mode"],
"candidate",
],
[
"nested array",
'const box=[{sub:["initial"]}];const [{sub:[selected]}]=box;box[0].sub[0]="later";',
["0", "sub", "0"],
"candidate",
],
[
"outer close after the selection",
'const box=[{mode:"initial"}];Object.getPrototypeOf([].values()).return=function(){box[0].mode="closed";return {};};const [{mode:selected}]=box;',
["0", "mode"],
"candidate",
],
] as const)(
"composes the outer iterator with read-time own-data paths for %s",
(_name, source, path, resolution) => {
const graph = graphForJavaScript(source);
const binding = graph.nodes.find(
({ kind, label }) => kind === "binding" && label === "selected",
);
const relation = graph.relations.find(
({ relation, target_node_id }) =>
relation === "destructures" && target_node_id === binding?.node_id,
);
expect(relation).toMatchObject({ resolution });
expect(
graph.nodes.find(({ node_id }) => node_id === relation?.source_node_id)
?.properties.property_path,
).toEqual(path);
},
);
@@ -126,8 +126,8 @@ const collectMember = (
provenOwnSlot:
!write &&
identity.path !== null &&
context.readPresence(identity.bindingId, node)?.(identity.path) ===
true,
context.readPresence(identity.bindingId, node)?.(identity.path) !==
undefined,
ownerCallableId,
objectBindingId: identity.bindingId,
targetBindingId: null,
@@ -171,12 +171,13 @@ const collectDestructuring = (
const { path } = next;
const propertyPath =
source.path === null ? null : [...source.path, ...path];
const selected =
next.lookup === "own" && propertyPath !== null
? ownSlot?.(propertyPath)
: undefined;
if (
next.lookup === "unproven" ||
(next.lookup === "own" &&
ownSlot !== undefined &&
propertyPath !== null &&
!ownSlot(propertyPath))
(next.lookup === "own" && selected === undefined)
)
ownSlot = undefined;
// Elision still advances the iterator and can read a property. It
@@ -188,7 +189,8 @@ const collectDestructuring = (
? unwrapJavaScriptExpression(occurrence.left).node
: occurrence;
if (t.isObjectPattern(pattern)) {
if (rootArray) ownSlot = undefined;
// A nested pattern needs a proved object even if it has no children.
if (rootArray && !t.isObjectExpression(selected)) ownSlot = undefined;
for (const property of pattern.properties.toReversed()) {
if (!t.isObjectProperty(property)) {
pending.push(null);
@@ -82,9 +82,10 @@ export const createSemanticReadPresenceQuery = (
return origin ?? undefined;
};
const ownDataAtPath = (value: t.Node, path: readonly string[]) => {
const ownDataAtPath = (value: t.Node, path: readonly string[], from = 0) => {
let receiver = value;
for (const key of path) {
for (let index = from; index < path.length; index += 1) {
const key = path[index]!;
if (!t.isObjectExpression(receiver)) return undefined;
let read = readers.get(receiver);
if (read === undefined) {
@@ -136,8 +137,8 @@ export const createSemanticReadPresenceQuery = (
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 literal = ownDataAtPath(value, iterationPath);
if (!t.isArrayExpression(literal)) return undefined;
const array = evaluateSemanticExpression(
capture,
state,
@@ -149,28 +150,43 @@ export const createSemanticReadPresenceQuery = (
array.unknownItems
)
return undefined;
return (path: readonly string[]): boolean => {
if (path.length !== iterationPath.length + 1) return false;
return (path: readonly string[]): t.Node | undefined => {
const key = path[iterationPath.length];
if (key === undefined) return false;
if (key === undefined) return undefined;
const index = semanticArrayIndex(key);
const slot = index === null ? undefined : array.items[index];
return (
slot?.name === key &&
slot.presence === "present" &&
!capturedBindingPathMayHaveChanged(
if (index === null) return undefined;
const slot = array.items[index];
const element = literal.elements[index];
if (
slot?.name !== key ||
slot.presence !== "present" ||
element == null
)
return undefined;
const selected = ownDataAtPath(
unwrapJavaScriptExpression(element).node,
path,
iterationPath.length + 1,
);
return selected === undefined ||
capturedBindingPathMayHaveChanged(
origin.binding,
path,
capture,
state,
)
);
? undefined
: selected;
};
}
if (!t.isObjectExpression(value)) return undefined;
return (path: readonly string[]): boolean =>
path.length > 0 &&
ownDataAtPath(value, path) !== undefined &&
!capturedBindingPathMayHaveChanged(origin.binding, path, capture, state);
return (path: readonly string[]): t.Node | undefined => {
if (path.length === 0) return undefined;
const selected = ownDataAtPath(value, path);
return selected === undefined ||
capturedBindingPathMayHaveChanged(origin.binding, path, capture, state)
? undefined
: selected;
};
};
};
@@ -45,6 +45,9 @@ cliTest(
const projectedAlias = projectedSource;
const [projected] = projectedAlias.items;
projectedSource.items[0] = "later";
const nestedArray = [{ mode: "read-time" }];
const [{ mode: arrayObject }] = nestedArray;
nestedArray[0].mode = "later";
const box = { items: [] };
const iterator = box.items.values();
const prototype = Reflect.getPrototypeOf(iterator);
@@ -74,6 +77,7 @@ cliTest(
["arrayAssigned", ["0"]],
["elided", ["1"]],
["projected", ["items", "0"]],
["arrayObject", ["0", "mode"]],
["selected", ["0"], "candidate"],
] as const) {
const binding = graph.nodes.find(
@@ -110,6 +114,7 @@ cliTest(
"arrayAssigned",
"elided",
"projected",
"arrayObject",
"selected",
]) {
const binding = graph.nodes.find(