fix(javascript): preserve loader ownership and unavailable artifact facts

Preserve missing ASAR companion occurrence identity, declared size and unknown content in graph nodes and resolution limitations. Apply URL text semantics once for ESM lookup while preserving literal CommonJS paths and original specifiers. Keep CJS wrapper var bindings and assignment history, recognizing only untouched wrapper redeclarations.

Validation: real ASAR producer workflow and CLI/MCP path/coverage cases; semantic reassignment controls. Context7 Node documentation and direct Node wrapper execution confirm wrapper behavior.

Fixes #3213
Fixes #3214
Fixes #3223
Related: #3278, #3254

Co-authored-by: Geronimo <varungore1@gmail.com>
Co-authored-by: Paolo Valletta <paovalletta@hotmail.it>
This commit is contained in:
committed 2026-10-14 16:54:08 +08:00
1 parent 1d826f304f
commit 7e5cffa2ae
15 files changed
+589 -36

No files matched your search

@@ -1,6 +1,11 @@
import { describe, expect, it } from "vitest";
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
import {
analyzeJavaScriptSemantics,
analyzeParsedJavaScriptSemantics,
} from "./javascriptSemanticAnalysis.js";
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
import { moduleResolutionModeForJavaScriptReference } from "./javascriptModuleResolutionMode.js";
import {
origin,
topLevelBinding,
@@ -56,6 +61,74 @@ describe("TypeScript import-equals links", () => {
});
});
describe("CommonJS wrapper parameter redeclarations", () => {
it("retains ambient exports and require through uninitialized top-level vars", () => {
const path = "app/main.cjs";
const parsed = parseJavaScriptSource(
'var module, exports, require; module.exports = require("./dependency.cjs");',
path,
);
expect(parsed).not.toBeNull();
const ir = analyzeParsedJavaScriptSemantics(
parsed!,
moduleResolutionModeForJavaScriptReference(path, "static-import"),
);
expect(ir.moduleLinks).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "commonjs-export",
specifier: "./dependency.cjs",
localName: null,
exportedName: "default",
}),
]),
);
});
it("retains the Node loader call on an uninitialized wrapper var assignment RHS", () => {
const path = "app/main.cjs";
const parsed = parseJavaScriptSource(
'var require; require = require("./bootstrap.cjs");',
path,
);
expect(parsed).not.toBeNull();
const ir = analyzeParsedJavaScriptSemantics(
parsed!,
moduleResolutionModeForJavaScriptReference(path, "static-import"),
);
expect(ir.moduleLinks).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "require",
specifier: "./bootstrap.cjs",
}),
]),
);
});
it.each([
'var require = local; require("./dependency.cjs");',
'var require; require = replacement; require("./dependency.cjs");',
"var module; module = replacement; module.exports = 1;",
"var exports; exports = replacement; exports.value = 1;",
'let require; require("./dependency.cjs");',
'function nested() { var require; require("./dependency.cjs"); }',
])("keeps non-wrapper declarations as shadows: %s", (source) => {
const path = "app/main.cjs";
const parsed = parseJavaScriptSource(source, path);
expect(parsed).not.toBeNull();
const ir = analyzeParsedJavaScriptSemantics(
parsed!,
moduleResolutionModeForJavaScriptReference(path, "static-import"),
);
expect(
ir.moduleLinks.filter(
({ kind }) => kind === "require" || kind === "commonjs-export",
),
).toEqual([]);
});
});
describe("TypeScript type-only module links", () => {
it("retains declarations without runtime bindings or callable claims", () => {
const ir = analyzeJavaScriptSemantics(`