test(javascript): consolidate semantic graph coverage

This commit is contained in:
morluto committed 2026-11-17 21:02:58 -05:00
1 parent e2e3881c0e
commit c77d4261a1
17 files changed
+684 -1098

No files matched your search

@@ -1,105 +1,48 @@
import { expect, it } from "vitest";
import { graphForJavaScript as graphFor } from "../../../tests/fixtures/javascriptSemanticGraph.js";
import { analyzeJavaScriptSemantics } from "../../domain/javascript/javascriptSemanticAnalysis.js";
import { topLevelBinding } from "../../domain/javascript/javascriptSemanticAnalysis.fixture.js";
import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js";
const dynamicScopeUnknowns = (source: string) =>
graphFor(source).unknowns.filter(({ reason }) => reason === "dynamic-scope");
it("exposes direct eval and with as dynamic-scope unknowns in the public graph", () => {
const unknowns = dynamicScopeUnknowns(`
function inspect(host, source) {
with (host) { consume(target); }
eval(source);
}
`);
it("records dynamic scope only at with and unshadowed direct eval", () => {
const graph = graphForJavaScript(
[
"function inspect(host, source) {",
" with (host) { consume(target); }",
" eval(source);",
"}",
"function shadowed(eval, source) { eval(source); }",
"function optional(source) { eval?.(source); }",
"function indirect(source) { (0, eval)(source); }",
].join("\n"),
);
const inspect = graph.nodes.find(
({ label, kind }) => kind === "function" && label === "inspect",
);
if (inspect === undefined)
throw new Error("Expected the containing function");
const unknowns = graph.unknowns.filter(
({ reason }) => reason === "dynamic-scope",
);
expect(unknowns).toHaveLength(2);
expect(unknowns.map(({ detail }) => detail).join("\n")).toEqual(
expect.stringContaining("with"),
);
expect(unknowns.map(({ detail }) => detail).join("\n")).toEqual(
expect.stringContaining("eval"),
);
});
it("does not mark shadowed, optional, or indirect eval as dynamic scope", () => {
const unknowns = dynamicScopeUnknowns(`
function shadowed(eval, source) { eval(source); }
function optional(source) { eval?.(source); }
function indirect(source) { (0, eval)(source); }
`);
expect(unknowns).toEqual([]);
});
it("keeps switch discriminants outside the shared case lexical scope", () => {
const ir = analyzeJavaScriptSemantics(`
const selected = choose();
switch (selected) {
case 1: let selected = 1; let local = 1; break;
case 2: local = 2; break;
}
`);
const selectedBinding = topLevelBinding(ir, "selected");
const localBindings = ir.bindings.filter(({ name }) => name === "local");
expect(ir.references.find(({ name }) => name === "selected")?.bindingId).toBe(
selectedBinding.bindingId,
);
expect(localBindings).toHaveLength(1);
expect(localBindings[0]?.scopeId).not.toBe(selectedBinding?.scopeId);
expect(
ir.references.filter(
({ name, role }) => name === "selected" && role === "read",
),
).toHaveLength(1);
expect(
ir.references.filter(
({ name, role }) => name === "local" && role === "write",
),
).toHaveLength(1);
});
it("leaves with-object resolution intact and marks only body name resolution unknown", () => {
const ir = analyzeJavaScriptSemantics(`
const host = getHost();
const outer = 3;
function inspect() {
with (host) {
const local = 1;
consume(local, outer);
}
}
`);
const host = topLevelBinding(ir, "host");
const hostRead = ir.references.find(
({ name, role }) => name === "host" && role === "read",
);
const outerRead = ir.references.find(
({ name, role }) => name === "outer" && role === "read",
);
const localRead = ir.references.find(
({ name, role }) => name === "local" && role === "read",
);
expect(hostRead?.bindingId).toBe(host.bindingId);
expect(hostRead?.resolution).toBe("resolved");
expect(outerRead?.resolution).toBe("unknown");
expect(localRead?.resolution).toBe("resolved");
expect(outerRead?.bindingId).toBeNull();
});
it("keeps var declarations in each static block from leaking to another block", () => {
const ir = analyzeJavaScriptSemantics(`
class Example {
static { var first = 1; }
static { var second = 2; consume(first); }
}
`);
const first = ir.bindings.filter(({ name }) => name === "first");
const second = ir.bindings.filter(({ name }) => name === "second");
expect(ir.references.find(({ name }) => name === "first")?.resolution).toBe(
"unbound",
);
expect(first).toHaveLength(1);
expect(second).toHaveLength(1);
expect(first[0]?.scopeId).not.toBe(second[0]?.scopeId);
for (const [statement, line] of [
["with", 2],
["eval", 3],
] as const) {
expect(unknowns).toContainEqual(
expect.objectContaining({
node_id: inspect.node_id,
family: "data-flow",
relation_kinds: ["reads", "writes"],
detail: expect.stringContaining(`\`${statement}\``),
evidence: expect.objectContaining({
location: expect.objectContaining({
available: true,
value: expect.objectContaining({
kind: "source-range",
start: { line, column: 2 },
}),
}),
}),
}),
);
}
});