fix(javascript): retain proven property presence at reads (#12477)

* fix(javascript): retain proven property presence at reads

The read-time IR and projection boundary builds on the destructuring work in #7716. This change adds an indexed own-slot proof and uses it for ordinary member reads as well.

Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com>

* fix(javascript): retain destructuring proof in read order

---------

Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com>
This commit is contained in:
morlutoandSII-YDD authored and GitHub committed 2026-11-19 19:17:32 +00:00
1 parent 833916bb4a
commit 1dc22f18cd
8 files changed
+322 -42

No files matched your search

@@ -1,6 +1,106 @@
import { expect, it } from "vitest";
import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js";
it.each([
"const { mode: selected, fallback = 0 } = config;",
'const { mode: selected, ["fallback"]: later } = config;',
"const { mode: selected, nested: { fallback = 0 } } = config;",
"const { mode: selected, ...later } = config;",
"const { mode: selected, accessor: later } = config;",
"let selected; const target = {}; ({ mode: selected, fallback: target.value } = config);",
])(
"retains a proved read before a later unproven pattern operation",
(read) => {
const graph = graphForJavaScript(`
const config = { mode: "strict", fallback: 0, nested: { fallback: 0 }, get accessor() { return 0; } };
${read}
config.mode = "loose";
`);
const selected = graph.nodes.find(
({ kind, label }) => kind === "binding" && label === "selected",
);
expect(
graph.relations.find(
(edge) =>
edge.relation === "destructures" &&
edge.target_node_id === selected?.node_id,
),
).toMatchObject({ resolution: "resolved" });
},
);
it("leaves the next read uncertain after a source-mutating assignment target", () => {
const graph = graphForJavaScript(`
const config = { first: 1, mode: "strict" };
let selected;
const target = { set value(_) { delete config.mode; } };
({ first: target.value, mode: selected } = config);
config.mode = "loose";
`);
expect(
graph.relations.find(({ relation }) => relation === "destructures"),
).toMatchObject({ resolution: "candidate" });
});
it.each([
{ read: "const { mode: selected } = config;", relation: "destructures" },
{
read: "let selected; ({ mode: selected } = config);",
relation: "destructures",
},
{ read: "const selected = config.mode;", relation: "reads-property" },
] as const)(
"keeps earlier $relation at its read position",
({ read, relation }) => {
const source = 'const config = { mode: "strict", other: 0 };';
for (const [before, after, resolution] of [
["", 'config.mode = "loose";', "resolved"],
["", "config.other = 1;", "resolved"],
["delete config.mode;", "", "candidate"],
["mutate(config);", "", "candidate"],
]) {
const graph = graphForJavaScript(
[source, before, read, after].join("\n"),
);
expect(
graph.relations.find((edge) => edge.relation === relation),
).toMatchObject({ resolution });
}
},
);
it("keeps a direct nested data-slot dependency after a later write", () => {
const graph = graphForJavaScript(`
const config = { nested: { mode: "strict" } };
const { mode: selected } = config.nested;
config.nested.mode = "loose";
`);
expect(
graph.relations.find(({ relation }) => relation === "destructures"),
).toMatchObject({ resolution: "resolved" });
});
it.each([
`const config = { mode: "strict" }; const { first = (delete config.mode), mode: selected } = config;`,
`const config = { get first() { delete this.mode; return {}; }, mode: "strict" };
const { first: {}, mode: selected } = config;`,
])(
"leaves a dependency uncertain after an effectful earlier pattern lookup",
(source) => {
const graph = graphForJavaScript(source + 'config.mode = "loose";');
const selected = graph.nodes.find(
({ kind, label }) => kind === "binding" && label === "selected",
);
expect(
graph.relations.find(
(edge) =>
edge.relation === "destructures" &&
edge.target_node_id === selected?.node_id,
),
).toMatchObject({ resolution: "candidate" });
},
);
it("links declared and assigned destructuring bindings to their exact source slots", () => {
const cases = [
{ pattern: "{ a: selected }", source: "{ a: 1 }", path: ["a"] },
@@ -120,7 +120,7 @@ const objectTarget = (
objectNode: slot,
slot,
slotApplicability: "resolved",
presence: fact.presence,
presence: operation.sourcePresence ?? fact.presence,
receiverResolved:
receiver.presence === "present" &&
semanticContainer(receiver.value) !== null,