fix(reference): retain proven loader calls and unresolved targets (#9920)

* fix(reference): retain proven loader calls and unresolved targets

* refactor(reference): remove unused ancestor lookup

* fix(reference): drop loader calls that return a reassigned member

An immediate call could still use a member's original require initializer
because mutation facts were not built for a call-valued callee. A definite
assignment that precedes the call now replaces that initializer.

* fix(reference): select returned loaders from indexed read facts

* fix(reference): refresh loader facts for wrapped returned members

A sequence such as (0, box.load) still used the original require
initializer. Follow sequence, conditional, and logical returns to the
member before deciding whether a prior assignment replaced it.

* test(reference): record unresolved CommonJS targets in source evidence

unknown.cjs calls require with a non-literal target. The inventory now
keeps that coverage limitation on the file entry, and the boundary
fixture asserts it beside the resolved member relationships.

* fix(reference): cover opaque apply lists and assigned returned members

require.apply(null, args) has no normalized argument list, so the
inventory now records an unresolved target instead of complete coverage.
An assignment return such as f = box.load is followed to the member
before a prior replacement is trusted.

* fix(reference): keep self-defaulted module delegates loadable

Timing a member read at a later call made `box.load = box.load || null` observe its own write and drop the native delegate. Reads that already fed an earlier assignment stay at the member.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
morlutoandCursor Agent authored and GitHub committed 2026-11-11 18:23:15 +08:00
1 parent d9537fc75c
commit cb17828dd1
9 files changed
+317 -41

No files matched your search

@@ -3,7 +3,10 @@ import { createHash } from "node:crypto";
import { expect, it } from "vitest";
import { parseReferenceSourceEntries } from "./ReferenceSourceImportEntries.js";
import { historicalSourceLanguages } from "../domain/referenceSourceGraph.js";
import {
historicalSourceInventoryHasPartialFacts,
historicalSourceLanguages,
} from "../domain/referenceSourceGraph.js";
import type { ReferenceSourceRead } from "../reference/ReferenceSourceReaderTypes.js";
it("projects an unsupported non-regular entry as unknown kind instead of file", () => {
@@ -98,6 +101,101 @@ it("keeps source entries when a module has an empty specifier", () => {
expect(parsed.limitations[0]).toMatch(/not recorded.*relationship/iu);
});
it("resolves immediately invoked loaders from their proven source bases", () => {
const files = [
[
"main.cjs",
'(() => require)()("./dep.cjs"); (() => () => {})()("./local.cjs");',
],
[
"main.mjs",
[
'import {createRequire} from "node:module";',
'createRequire(new URL("./sub/base.cjs", import.meta.url))("./dep.cjs");',
'createRequire(new URL("./sub/base.cjs", import.meta.url)).call(null, "./member.cjs");',
].join("\n"),
],
["dep.cjs", ""],
["sub/dep.cjs", ""],
["member.cjs", ""],
["sub/member.cjs", ""],
] as const;
const parsed = parseReferenceSourceEntries(
{
root: "/selected/source",
entries: files.map(([path, content]) => ({
status: "read",
kind: "file",
path,
bytes: Buffer.from(content),
size: Buffer.byteLength(content),
})),
bytesRead: files.reduce(
(sum, [, content]) => sum + Buffer.byteLength(content),
0,
),
limitations: [],
},
[],
);
expect(
parsed.relationships.filter(({ kind }) => kind === "requires"),
).toEqual([
{
from_path: "main.cjs",
to: "dep.cjs",
kind: "requires",
resolution: "internal",
parse_state: "parsed",
},
{
from_path: "main.mjs",
to: "sub/dep.cjs",
kind: "requires",
resolution: "internal",
parse_state: "parsed",
},
]);
});
it.each([
["function load(name) { require(name); }", true],
["function load(args) { require.apply(null, args); }", true],
["function load(require, name) { require(name); }", false],
["require(7); require();", false],
] as const)(
"reports unresolved CommonJS target coverage for %s",
(source, partial) => {
const bytes = Buffer.from(source);
const parsed = parseReferenceSourceEntries(
{
root: "/selected/source",
entries: [
{
status: "read",
kind: "file",
path: "main.cjs",
bytes,
size: bytes.length,
},
],
bytesRead: bytes.length,
limitations: [],
},
[],
);
expect(parsed.relationships).toEqual([]);
expect(
historicalSourceInventoryHasPartialFacts({
...parsed,
parse_failures: parsed.parseFailures,
exclusions: [],
}),
).toBe(partial);
expect(parsed.entries[0]?.limitations).toEqual(parsed.limitations);
},
);
it("retains Kotlin source identity and language across the inventory projection", () => {
const files = [
[