Files
reverse-engineering-anything/src/domain/referenceSourcePathIdentity.ts
T
morlutoandCursor Agent cb17828dd1 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>
2026-11-11 18:23:15 +08:00

74 lines
2.6 KiB
TypeScript

import { posix } from "node:path";
import { err, ok, type Result } from "./result.js";
export interface ReferenceSourcePathLookup {
/** Whether an exact or canonical identity exists, including ambiguous ones. */
readonly has: (path: string) => boolean;
readonly resolve: (path: string) => string | undefined;
readonly resolveCandidates: (paths: readonly string[]) => string | undefined;
}
/**
* Reference inventories retain filesystem spelling. Prefer that exact identity;
* canonical Unicode equivalence is a fallback only when it identifies one entry.
*/
export const createReferenceSourcePathLookup = (
paths: Iterable<string>,
): ReferenceSourcePathLookup => {
const exact = new Set(paths);
const canonical = new Map<string, string | null>();
for (const path of exact) {
const key = path.normalize("NFC");
canonical.set(key, canonical.has(key) ? null : path);
}
const equivalent = (path: string): string | undefined =>
canonical.get(path.normalize("NFC")) ?? undefined;
const has = (path: string): boolean =>
exact.has(path) || canonical.has(path.normalize("NFC"));
const resolve = (path: string): string | undefined =>
exact.has(path) ? path : equivalent(path);
return {
has,
resolve,
resolveCandidates: (candidates) =>
candidates.find((path) => exact.has(path)) ??
candidates.map(equivalent).find((path) => path !== undefined),
};
};
/** Symlink evidence can originate on a different OS from the graph reader. */
export const isPortableAbsoluteReferenceTarget = (target: string): boolean =>
target.startsWith("/") ||
/^[A-Za-z]:[\\/]/u.test(target) ||
/^\\\\[^\\/]+[\\/][^\\/]+/u.test(target);
/** Decode one historical inventory URL path; backslash remains a filename byte. */
export const decodeReferenceSourceModuleUrlPath = (
value: string,
): Result<string, string> => {
if (/%2f/iu.test(value))
return err(
"Historical module URL paths cannot contain percent-encoded slash bytes.",
);
try {
const decoded = decodeURIComponent(value);
return decoded.includes("\0")
? err("The decoded historical module URL path contains NUL.")
: ok(decoded);
} catch (cause: unknown) {
if (!(cause instanceof URIError)) throw cause;
return err(
"The historical module URL path contains malformed percent encoding.",
);
}
};
/** Rebase a portable target without discarding its directory-required suffix. */
export const rebaseReferenceSourceTarget = (
linkPath: string,
target: string,
): string => {
const relative = posix.relative(posix.dirname(linkPath), target) || ".";
return target.endsWith("/") ? `${relative}/` : relative;
};