From ad515e3531f244ea6c35532430950b23887cfb39 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:40:37 -0500 Subject: [PATCH] fix(javascript): preserve proved dependencies across array elisions (#12514) --- ...vaScriptSemanticGraphDestructuring.test.ts | 79 +++++++++++++++++++ .../javascript/javascriptSemanticObjects.ts | 10 ++- .../javascriptDestructuringEvidence.test.ts | 12 ++- 3 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts index 65e5d351df..353ccb42ad 100644 --- a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts @@ -742,3 +742,82 @@ it.each([ ).toMatchObject({ resolution }); }, ); + +it.each([ + [ + "declaration", + "[0, 1]", + "const [,selected] = config; config[1] = 2;", + "/1", + "resolved", + ], + [ + "assignment", + "[0, 1]", + "let selected; [,selected] = config; config[1] = 2;", + "/1", + "resolved", + ], + [ + "const alias", + "[0, 1]", + "const alias = config; const [,selected] = alias; config[1] = 2;", + "/1", + "resolved", + ], + [ + "two elisions", + "[0, 1, 2]", + "const [,,selected] = config; config[2] = 3;", + "/2", + "resolved", + ], + [ + "array hole", + "[, 1]", + "const [,selected] = config; config[1] = 2;", + "/1", + "candidate", + ], + [ + "previous deletion", + "[0, 1]", + "delete config[0]; const [,selected] = config; config[1] = 2;", + "/1", + "candidate", + ], + [ + "skipped getter", + "[0, 1]", + 'Object.defineProperty(config, "0", {get(){delete this[1]; return 0;}}); const [,selected] = config;', + "/1", + "candidate", + ], + [ + "replaced iterator", + "[0, 1]", + "Object.getPrototypeOf([].values()).next = () => ({value: 3, done: false}); const [,selected] = config; config[1] = 2;", + "/1", + "candidate", + ], +] as const)( + "preserves exactly the proved array elision dependencies for %s", + (_name, initial, source, pointer, resolution) => { + const graph = graphForJavaScript(`const config = ${initial}; ${source}`); + const selected = graph.nodes.find( + ({ kind, label }) => kind === "binding" && label === "selected", + ); + const edges = graph.relations.filter( + ({ relation }) => relation === "destructures", + ); + expect(edges).toHaveLength(1); + expect(edges[0]).toMatchObject({ + target_node_id: selected?.node_id, + resolution, + }); + expect( + graph.nodes.find(({ node_id }) => node_id === edges[0]?.source_node_id) + ?.properties.property_pointer, + ).toBe(pointer); + }, +); diff --git a/src/domain/javascript/javascriptSemanticObjects.ts b/src/domain/javascript/javascriptSemanticObjects.ts index ca3e2847a9..ccccf8e42d 100644 --- a/src/domain/javascript/javascriptSemanticObjects.ts +++ b/src/domain/javascript/javascriptSemanticObjects.ts @@ -159,7 +159,7 @@ const collectDestructuring = ( // An unsupported property still occupies its place in the pattern's // evaluation order, even when it cannot produce a graph operation. const pending: ({ - readonly pattern: t.Node; + readonly pattern: t.Node | undefined; readonly path: readonly string[]; readonly lookup?: "own" | "unproven"; } | null)[] = [{ pattern: root, path: [] }]; @@ -181,6 +181,9 @@ const collectDestructuring = ( !ownSlot(propertyPath)) ) ownSlot = undefined; + // Elision still advances the iterator and can read a property. It + // contributes no binding once the shared slot check has run. + if (next.pattern === undefined) continue; const occurrence = unwrapJavaScriptExpression(next.pattern).node; if (t.isAssignmentPattern(occurrence)) ownSlot = undefined; const pattern = t.isAssignmentPattern(occurrence) @@ -210,10 +213,11 @@ const collectDestructuring = ( if (pattern !== root) ownSlot = undefined; for (let index = pattern.elements.length - 1; index >= 0; index -= 1) { const element = pattern.elements[index]; - if (element == null || t.isRestElement(element)) pending.push(null); + if (element === undefined || t.isRestElement(element)) + pending.push(null); else pending.push({ - pattern: element, + pattern: element ?? undefined, path: [...path, String(index)], lookup: "own", }); diff --git a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts index d0791bf2a1..df323c8c80 100644 --- a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts +++ b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts @@ -38,6 +38,9 @@ cliTest( let arrayAssigned; [arrayAssigned] = before; before[0] = "later"; + const skipped = ["skip", "first"]; + const [,elided] = skipped; + skipped[1] = "later"; const box = { items: [] }; const iterator = box.items.values(); const prototype = Reflect.getPrototypeOf(iterator); @@ -65,6 +68,7 @@ cliTest( ["scalar", ["b"]], ["ordinary", ["0"]], ["arrayAssigned", ["0"]], + ["elided", ["1"]], ["selected", ["0"], "candidate"], ] as const) { const binding = graph.nodes.find( @@ -95,7 +99,13 @@ cliTest( ), ).toMatchObject({ resolution: "resolved" }); const queryPath = join(root, "query.json"); - for (const name of ["declared", "ordinary", "arrayAssigned", "selected"]) { + for (const name of [ + "declared", + "ordinary", + "arrayAssigned", + "elided", + "selected", + ]) { const binding = graph.nodes.find( ({ kind, label }) => kind === "binding" && label === name, );