Compare import and require outcomes for static TypeScript references when compiler options are unknown. Preserve branch facts through worker IR and both graph projections; retain declaration fallbacks as candidates without changing explicitly selected paths. Validation: 210 focused resolver and semantic tests; 21 production-boundary and source-extension tests. A bounded 2,000-reference probe adds about 16–17 ms wall time for dual resolution; full task token costs and real compiled application behavior remain unmeasured.
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
/** Emitted NodeNext suffixes and the TypeScript source counterparts they name. */
|
|
const EMITTED_SUFFIXES: ReadonlyArray<
|
|
readonly [suffix: string, sourceSuffixes: readonly string[]]
|
|
> = [
|
|
[".js", [".ts", ".tsx", ".d.ts"]],
|
|
[".mjs", [".mts", ".d.mts"]],
|
|
[".cjs", [".cts", ".d.cts"]],
|
|
[".jsx", [".tsx", ".d.ts"]],
|
|
];
|
|
|
|
export interface TypeScriptEmittedSourceCandidate {
|
|
readonly path: string;
|
|
readonly kind: "implementation" | "declaration";
|
|
}
|
|
|
|
/**
|
|
* Return TypeScript source counterparts for an explicit emitted filename.
|
|
* Callers retain exact runtime-file precedence and must not use these for
|
|
* directory/index, URL, HTML, or package target resolution.
|
|
*/
|
|
export const typescriptEmittedSourceCandidates = (
|
|
sourcePath: string,
|
|
candidate: string,
|
|
): readonly TypeScriptEmittedSourceCandidate[] => {
|
|
if (!/\.(?:ts|tsx|mts|cts)$/u.test(sourcePath)) return [];
|
|
const emitted = EMITTED_SUFFIXES.find(([suffix]) =>
|
|
candidate.endsWith(suffix),
|
|
);
|
|
if (emitted === undefined) return [];
|
|
// Strip the matched suffix by length. posix.extname would miss dotfile
|
|
// names such as ".js", whose whole basename is the emitted suffix itself.
|
|
const stem = candidate.slice(0, candidate.length - emitted[0].length);
|
|
return emitted[1].map((suffix) => ({
|
|
path: `${stem}${suffix}`,
|
|
kind: suffix.startsWith(".d.") ? "declaration" : "implementation",
|
|
}));
|
|
};
|