mirror of
https://github.com/morluto/rea.git
synced 2026-10-31 11:56:12 +00:00
refactor(javascript): group domain semantics and contracts (#907)
This commit is contained in:
1 parent
d5f28fb29c
commit
f6a58f1062
253 files changed
+432
-410
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import { fc, it } from "@fast-check/vitest";
|
||||
import { describe, expect } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import type { JavaScriptSemanticValue } from "./javascriptSemanticIr.js";
|
||||
import {
|
||||
onlyCallable,
|
||||
topLevelBinding,
|
||||
} from "./javascriptSemanticAnalysis.fixture.js";
|
||||
|
||||
describe("JavaScript semantic analysis: rejection 1", () => {
|
||||
it("retains complete finite facts and deeply nested static values", () => {
|
||||
const names = Array.from(
|
||||
{ length: 280 },
|
||||
(_, index) => `key${String(index)}`,
|
||||
);
|
||||
const object = analyzeJavaScriptSemantics(
|
||||
`const first = require("first"); const second = require("second"); first; second; const object = { ${names.map((name, index) => `${name}: ${String(index)}`).join(", ")} };`,
|
||||
);
|
||||
expect(object.moduleLinks).toHaveLength(2);
|
||||
expect(object.references).toHaveLength(4);
|
||||
expect(topLevelBinding(object, "object").value).toMatchObject({
|
||||
status: "object",
|
||||
unknownProperties: false,
|
||||
properties: expect.arrayContaining([
|
||||
expect.objectContaining({ name: "key279" }),
|
||||
]),
|
||||
});
|
||||
|
||||
const nesting = 40;
|
||||
const nestedValue =
|
||||
Array.from({ length: nesting }, () => "{ next: ").join("") +
|
||||
'"value"' +
|
||||
" }".repeat(nesting);
|
||||
const deep = analyzeJavaScriptSemantics(`const nested = ${nestedValue};`);
|
||||
let value: JavaScriptSemanticValue = topLevelBinding(deep, "nested").value;
|
||||
for (let depth = 0; depth < nesting; depth += 1) {
|
||||
expect(value.status).toBe("object");
|
||||
if (value.status !== "object")
|
||||
throw new TypeError("Expected a recovered nested object");
|
||||
const next = value.properties.find(({ name }) => name === "next");
|
||||
if (next === undefined) throw new TypeError("Missing nested property");
|
||||
value = next.value;
|
||||
}
|
||||
expect(value).toEqual({ status: "literal", value: "value" });
|
||||
});
|
||||
|
||||
it("is deterministic and returns failed coverage for an unparseable source", () => {
|
||||
const source = 'const { value: renamed } = require("fixture");';
|
||||
expect(analyzeJavaScriptSemantics(source)).toEqual(
|
||||
analyzeJavaScriptSemantics(source),
|
||||
);
|
||||
expect(analyzeJavaScriptSemantics("function {")).toMatchObject({
|
||||
scopes: [],
|
||||
bindings: [],
|
||||
callables: [],
|
||||
references: [],
|
||||
coverage: { status: "failed", omittedCount: null },
|
||||
});
|
||||
});
|
||||
|
||||
it("does not mark parser-recovered duplicate bindings complete", () => {
|
||||
const ir = analyzeJavaScriptSemantics(
|
||||
"const duplicate = 'first'; const duplicate = 'second';",
|
||||
);
|
||||
|
||||
expect(ir.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
omittedCount: 0,
|
||||
});
|
||||
expect(topLevelBinding(ir, "duplicate").value).toMatchObject({
|
||||
status: "ambiguous",
|
||||
});
|
||||
expect(ir.limitations.join(" ")).toMatch(/parser recovered/iu);
|
||||
});
|
||||
|
||||
it("keeps parser-recovered return shapes partial", () => {
|
||||
const ir = analyzeJavaScriptSemantics(
|
||||
`
|
||||
export default function recovered(value) {
|
||||
const duplicate = 1;
|
||||
const duplicate = 2;
|
||||
return { type: "item", text: render(value) };
|
||||
}
|
||||
`,
|
||||
);
|
||||
const recovered = onlyCallable(ir, "recovered");
|
||||
|
||||
expect(ir.coverage.status).toBe("partial");
|
||||
expect(recovered.returnCoverage).toMatchObject({
|
||||
status: "partial",
|
||||
retainedCount: 1,
|
||||
omittedCount: 0,
|
||||
});
|
||||
expect(recovered.returnSites[0]?.value).toMatchObject({
|
||||
status: "object",
|
||||
properties: expect.arrayContaining([
|
||||
{ name: "type", value: { status: "literal", value: "item" } },
|
||||
expect.objectContaining({
|
||||
name: "text",
|
||||
value: expect.objectContaining({ status: "unknown" }),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
it.prop([fc.string({ maxLength: 512 })])(
|
||||
"fails closed for arbitrary source text",
|
||||
(source) => {
|
||||
const ir = analyzeJavaScriptSemantics(source);
|
||||
|
||||
expect(ir.schema).toBe("JavaScriptSemanticIR");
|
||||
expect(["complete", "partial", "truncated", "failed"]).toContain(
|
||||
ir.coverage.status,
|
||||
);
|
||||
if (ir.coverage.status === "failed") {
|
||||
expect(ir.scopes).toEqual([]);
|
||||
expect(ir.bindings).toEqual([]);
|
||||
expect(ir.callables).toEqual([]);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user