Files
reverse-engineering-anything/src/domain/javascript/javascriptModuleResolutionMode.ts
T
morlutoandShuaixinHuang b46f4fcebd fix(javascript): preserve loader authority for exact suffixes (#9477)
* fix(javascript): require exact loader suffix case

* fix(javascript): share exact loader mode across import resolution

* test(process): align symlink fixture with filesystem checkpoints

* test(javascript): normalize the module resolution oracle path separators

* test(frida): assert the CLI spawn Evidence contract

* test(mcp): use an unsupported analysis-view parent

* test(browser): separate source-location line bounds from parity

* fix(javascript): preserve uncertain loader provenance

* test(javascript): cover uncertain require resolution

* fix(javascript): retain conditional Electron evidence

* fix(javascript): preserve timed semantic provenance

* fix(javascript): retain conditional builtin effects

* test(javascript): distinguish bundled host authority

* fix(javascript): preserve nested loader invocation identity

* test(javascript): qualify Electron fixture authority

* fix(javascript): preserve loader invocation authority

* fix(javascript): respect source order for resolver escapes

* fix(javascript): check nested loader callable mutations

* fix(javascript): retain conditional constructor requests

* fix(reference): retain constructor loader relationships

* fix(reference): follow nested loader callable paths

* refactor(javascript): share callable mutation interpretation

* fix(javascript): require exact package loader suffixes

* refactor(javascript): unify conditional relation evidence

* refactor(javascript): unify conditional node evidence

* fix(javascript): retain unknown authority for mixed case sources

* fix(javascript): preserve loader uncertainty for dotfiles

* fix(reference): classify javascript suffix dotfiles

* test(reference): cover suffix dotfiles through cli

* fix(javascript): retain equivalent conditional built-in origins

* fix(javascript): preserve wrapped resolver operation

* fix(javascript): retain nested conditional built-in receivers

* test(javascript): declare CommonJS in native addon fixtures

* fix(javascript): retain wrapped resolver candidates

* style: format merged native command schema

* fix(javascript): preserve uncertain loader authority

* test(javascript): require candidate preload evidence

* test(javascript): cover reference loader member writes

---------

Co-authored-by: ShuaixinHuang <1836656514@qq.com>
2026-11-09 13:21:24 +08:00

65 lines
2.3 KiB
TypeScript

import { posix } from "node:path";
import type { JavaScriptArtifactFileKind } from "./javascriptArtifactFiles.js";
/** JSON dispatch uses the exact loader filename, not the inventory's folded kind. */
export const isJsonModuleTarget = (
path: string,
kind: JavaScriptArtifactFileKind | undefined,
): boolean =>
posix.extname(path) === ".json" &&
(kind === "json" || kind === "package-json");
/** Native-addon dispatch likewise needs the exact `.node` loader suffix. */
export const isNativeAddonModuleTarget = (
path: string,
kind: JavaScriptArtifactFileKind | undefined,
): boolean => posix.extname(path) === ".node" && kind === "native-addon";
/** Exact loader suffixes are separate from case-folded artifact grammar. */
export const explicitJavaScriptLoaderMode = (
path?: string,
): "module" | "commonjs" | undefined => {
switch (path === undefined ? undefined : posix.extname(path)) {
case ".mjs":
case ".mts":
return "module";
case ".cjs":
case ".cts":
return "commonjs";
default:
return undefined;
}
};
/** Case variants are artifact syntax hints, not recognized loader suffixes. */
export const hasCaseVariantJavaScriptLoaderSuffix = (
path?: string,
): boolean => {
if (path === undefined) return false;
const suffix = /\.(?:js|ts|mjs|cjs|mts|cts)$/iu.exec(path)?.[0];
return suffix !== undefined && suffix !== suffix.toLowerCase();
};
/** Loader evidence retained for a source-level module reference. */
export type JavaScriptModuleResolutionMode =
| "import"
| "require"
| "compiler-dependent";
/** Derive source mode only when syntax or an explicit source format owns it. */
export const moduleResolutionModeForJavaScriptReference = (
sourcePath: string,
referenceKind: "static-import" | "dynamic-import" | "require",
explicitMode?: JavaScriptModuleResolutionMode,
): JavaScriptModuleResolutionMode => {
if (explicitMode !== undefined) return explicitMode;
if (referenceKind === "require") return "require";
if (referenceKind === "dynamic-import") return "import";
const loaderMode = explicitJavaScriptLoaderMode(sourcePath);
if (loaderMode !== undefined)
return loaderMode === "commonjs" ? "require" : "import";
const extension = posix.extname(sourcePath).toLowerCase();
if (extension === ".ts" || extension === ".tsx") return "compiler-dependent";
return "import";
};