fix(javascript): retain numeric reads from projected arrays (#12624)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 01:01:22 +00:00
1 parent e9ecf900d2
commit ba2d70d71e
4 files changed
+73 -31

No files matched your search

@@ -2107,6 +2107,48 @@ it.each([
["0", "0"],
"resolved",
],
[
"a numeric own slot on a projected array RHS",
'const box={pairs:[["one"]]};const {0:[selected]}=box.pairs;box.pairs[0][0]="later";',
["pairs", "0", "0"],
"resolved",
],
[
"a quoted own slot on a projected const alias",
'const box={pairs:[["one"]]};const alias=box;let selected;({"0":[selected]}=alias.pairs);box.pairs[0][0]="later";',
["pairs", "0", "0"],
"resolved",
],
[
"a numeric own slot under an own object path",
'const box={nested:{pairs:[["one"]]}};const {0:[selected]}=box.nested.pairs;box.nested.pairs[0][0]="later";',
["nested", "pairs", "0", "0"],
"resolved",
],
[
"a numeric slot selected through an accessor",
'const box={get pairs(){return [["one"]];}};const {0:[selected]}=box.pairs;box.pairs[0][0]="later";',
["pairs", "0", "0"],
"candidate",
],
[
"an earlier replacement of the projected array receiver",
'const box={pairs:[["one"]]};box.pairs=[["earlier"]];const {0:[selected]}=box.pairs;box.pairs[0][0]="later";',
["pairs", "0", "0"],
"candidate",
],
[
"a hole in a projected array",
'const box={pairs:[,["one"]]};const {0:[selected]}=box.pairs;box.pairs[0]=["later"];',
["pairs", "0", "0"],
"candidate",
],
[
"an early inner iterator overwrite at a projected array slot",
'const box={pairs:[["one"]]};Object.getPrototypeOf([].values()).next=()=>({done:true});const {0:[selected]}=box.pairs;box.pairs[0][0]="later";',
["pairs", "0", "0"],
"candidate",
],
[
"a quoted own array slot captured through a const alias",
'const pairs=[["one"]];const alias=pairs;let selected;({"0":[selected]}=alias);pairs[0][0]="later";',
@@ -160,11 +160,10 @@ const collectDestructuring = (
const rootArray = t.isArrayPattern(root);
const sourcePresence =
initializer != null && source.path !== null
? readPresence(
source.bindingId,
initializer,
rootArray ? source.path : undefined,
)
? readPresence(source.bindingId, initializer, {
path: source.path,
selection: rootArray ? "array-iterator" : "own-data",
})
: undefined;
let ownSlot = sourcePresence?.read;
// An unsupported property still occupies its place in the pattern's
@@ -33,6 +33,11 @@ type ReadOwnData = (
beforeIterator?: t.Node,
) => t.Node | undefined;
interface DestructuringSource {
readonly path: readonly string[];
readonly selection: "array-iterator" | "own-data";
}
interface ReadPresence {
readonly read: ReadOwnData;
readonly readNestedArray: (
@@ -121,7 +126,7 @@ export const createSemanticReadPresenceQuery = (
return (
bindingId: string | null,
capture: t.Node,
iterationPath?: readonly string[],
destructuring?: DestructuringSource,
): ReadPresence | undefined => {
const binding =
bindingId === null ? undefined : state.bindingsById.get(bindingId);
@@ -212,26 +217,22 @@ export const createSemanticReadPresenceQuery = (
: selected;
};
};
// An identifier RHS in an object pattern can select numeric own data
// without starting an iterator, even when a later read closes one.
const arrayPath =
iterationPath ??
(t.isArrayExpression(value) &&
t.isIdentifier(unwrapJavaScriptExpression(capture).node)
? []
: undefined);
if (arrayPath !== undefined) {
const literal = ownDataAtPath(value, arrayPath);
if (!t.isArrayExpression(literal)) return undefined;
const read = arraySlots(
literal,
arrayPath,
[],
iterationPath === undefined ? undefined : capture,
);
return read === undefined
? undefined
: { read, readNestedArray: arraySlots };
// Object patterns read numeric own data directly, even through a
// projected receiver. Array patterns also acquire a native iterator.
if (destructuring !== undefined) {
const literal = ownDataAtPath(value, destructuring.path);
if (t.isArrayExpression(literal)) {
const read = arraySlots(
literal,
destructuring.path,
[],
destructuring.selection === "array-iterator" ? capture : undefined,
);
return read === undefined
? undefined
: { read, readNestedArray: arraySlots };
}
if (destructuring.selection === "array-iterator") return undefined;
}
if (!t.isObjectExpression(value)) return undefined;
const read: ReadOwnData = (path, beforeIterator) => {
@@ -56,7 +56,7 @@ const expectedDependencies = [
["arrayObject", ["0", "mode"]],
["nestedElement", ["items", "0"]],
["siblingObjectElement", ["sibling", "0"]],
["numericInnerElement", ["0", "0"]],
["projectedNumericInnerElement", ["pairs", "0", "0"]],
["unreachedSibling", ["sibling", "0"], "candidate"],
["innerElement", ["0", "0"]],
["siblingInnerElement", ["1", "0"]],
@@ -82,7 +82,7 @@ const tracedBindings = [
"arrayObject",
"nestedElement",
"siblingObjectElement",
"numericInnerElement",
"projectedNumericInnerElement",
"unreachedSibling",
"innerElement",
"siblingInnerElement",
@@ -139,9 +139,9 @@ cliTest(
const { items: [nestedElement], empty: {}, sibling: [siblingObjectElement] } = nestedSource;
nestedSource.items[0] = "later";
nestedSource.sibling[0] = "later";
const numericSource = [["read-time"]];
const { 0: [numericInnerElement] } = numericSource;
numericSource[0][0] = "later";
const numericSource = { pairs: [["read-time"]] };
const { 0: [projectedNumericInnerElement] } = numericSource.pairs;
numericSource.pairs[0][0] = "later";
const nestedPairs = [["read-time"], ["sibling"]];
const [[innerElement] = ["fallback"], [siblingInnerElement]] = nestedPairs;
nestedPairs[0][0] = "later";