mirror of
https://github.com/morluto/rea
synced 2026-10-27 14:42:18 -04:00
fix(javascript): bound static getter reference expansion
Replace recursive getter histories with a query-owned worklist and memo. Bound precise expansion at 512 states and widen growing receiver cycles; retain escaped roots and constructed children rather than silently dropping suffixes. Carry the resource limit through semantic coverage, worker transport and graph Evidence while preserving direct writes, imports and unrelated literal facts. The small source probe reduced 65/326 paths to one widened reference for five and six getters. The original large Claude artifact was not rerun. Validate receiver/alias/mutation controls and compiled CLI/MCP consumers. Refs #3839, #3848 Co-authored-by: Yi Han <yhan606@aucklanduni.ac.nz>
This commit is contained in:
@@ -2,9 +2,13 @@ import * as t from "@babel/types";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { indexJavaScriptBindingsSteps } from "./javascriptSemanticAnalysis.js";
|
||||
import { semanticObjectPropertyReferences } from "./javascriptSemanticObjectReferences.js";
|
||||
import {
|
||||
semanticClassPropertyReferences,
|
||||
semanticObjectPropertyReferences,
|
||||
} from "./javascriptSemanticObjectReferences.js";
|
||||
import { completeSemanticSteps } from "./javascriptSemanticTraversal.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
import { getterExpansionSource } from "../../../tests/fixtures/javascriptGetterExpansion.js";
|
||||
|
||||
const objectAndState = (source: string) => {
|
||||
const parsed = parseJavaScriptSource(source, "object.js");
|
||||
@@ -71,3 +75,67 @@ it("resolves exact keys with source-order overrides, spreads and accessors", ()
|
||||
spelled(objectAndState(`const o = { 1: "one", 2: "two" };`), 1),
|
||||
).toEqual([['"one"', "[]"]]);
|
||||
});
|
||||
|
||||
it("preserves references through a small static getter permutation", () => {
|
||||
const getters = Array.from(
|
||||
{ length: 5 },
|
||||
(_, index) =>
|
||||
`static get field${index}() { return this[key].field${index}; }`,
|
||||
).join("\n");
|
||||
const source = `const key = getKey(); const shared = {}; class Request {
|
||||
${getters}
|
||||
static get state() { return shared; }
|
||||
}`;
|
||||
const parsed = parseJavaScriptSource(source, "getters.js");
|
||||
if (parsed === null) throw new Error("Expected a parsed fixture");
|
||||
const klass = parsed.program.body.find((node): node is t.ClassDeclaration =>
|
||||
t.isClassDeclaration(node),
|
||||
);
|
||||
if (klass === undefined) throw new Error("Expected a class declaration");
|
||||
const state = completeSemanticSteps(
|
||||
indexJavaScriptBindingsSteps(parsed.program),
|
||||
);
|
||||
const references = semanticClassPropertyReferences(
|
||||
{ owner: klass, kind: "static" },
|
||||
["field0"],
|
||||
state,
|
||||
);
|
||||
|
||||
expect(
|
||||
references.some(
|
||||
({ node }) => source.slice(node.start ?? 0, node.end ?? 0) === "shared",
|
||||
),
|
||||
).toBe(true);
|
||||
const distinctReferences = new Set(
|
||||
references.map(({ node, path, escapes }) =>
|
||||
JSON.stringify([node.start, node.end, path, escapes]),
|
||||
),
|
||||
);
|
||||
expect(references).toHaveLength(distinctReferences.size);
|
||||
});
|
||||
|
||||
it("widens a large static getter permutation to one escaped reference", () => {
|
||||
const source = getterExpansionSource(30).replaceAll("get ", "static get ");
|
||||
const parsed = parseJavaScriptSource(source, "getters.js");
|
||||
if (parsed === null) throw new Error("Expected a parsed fixture");
|
||||
const klass = parsed.program.body.find((node): node is t.ClassDeclaration =>
|
||||
t.isClassDeclaration(node),
|
||||
);
|
||||
if (klass === undefined) throw new Error("Expected a class declaration");
|
||||
const state = completeSemanticSteps(
|
||||
indexJavaScriptBindingsSteps(parsed.program),
|
||||
);
|
||||
const references = semanticClassPropertyReferences(
|
||||
{ owner: klass, kind: "static" },
|
||||
["field0"],
|
||||
state,
|
||||
);
|
||||
|
||||
expect(references).toHaveLength(1);
|
||||
expect(references[0]).toMatchObject({
|
||||
node: { type: "Identifier", name: "shared" },
|
||||
path: [],
|
||||
escapes: true,
|
||||
});
|
||||
expect([...state.getterResourceLimits]).toEqual(["getter-expansion"]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user