fix(javascript): preserve exact CommonJS export keys

Retain literal property keys as member segments, omit unresolved computed keys, and distinguish nested writes from dotted export names. Preserve empty keys and omit object-literal prototype setters. Verify bundler observations through Node, CLI, and MCP consumers.
This commit is contained in:
morluto
2026-10-11 07:59:40 +08:00
parent c916850852
commit 5485483f7d
3 changed files with 273 additions and 44 deletions
@@ -10,27 +10,41 @@ const ordinaryMemberChain = ".next".repeat(64);
it("collects a 12,000-member CommonJS export path without recursion", () => {
const file = parseJavaScriptSource(`exports${memberChain}.last = 1;`);
if (file === null) throw new Error("Expected valid JavaScript");
expect(collectJavaScriptExports(file).values).toEqual([
`${"next.".repeat(12_000)}last`,
]);
expect(collectJavaScriptExports(file).values).toEqual(["next"]);
});
it("preserves empty, dynamic, this, and private member path behavior", () => {
it("preserves exact empty keys without inventing dynamic, this, or private exports", () => {
const file = parseJavaScriptSource(`
exports[""].value = 1;
exports[""] = 0;
exports[key].value = 2;
exports.nested = {};
exports.nested.child = 3;
exports["nested.child"] = 4;
this.exports.ignored = 3;
class Example { #private = 0; read() { this.#private = 1; } }
`);
if (file === null) throw new Error("Expected valid JavaScript");
expect(collectJavaScriptExports(file).values).toEqual([
"",
".value",
"key.value",
"nested",
"nested.child",
]);
});
it("distinguishes the object literal prototype setter from own export keys", () => {
const prototypeSetter = parseJavaScriptSource(
"module.exports = { __proto__: {} };",
);
const ownKeys = parseJavaScriptSource(
'module.exports = { ["__proto__"]: 1, __proto__() {} };',
);
if (prototypeSetter === null || ownKeys === null)
throw new Error("Expected valid JavaScript");
expect(collectJavaScriptExports(prototypeSetter).values).toEqual([]);
expect(collectJavaScriptExports(ownKeys).values).toEqual(["__proto__"]);
});
it("keeps left-first and outer-member-first bundler runtime matches", () => {
const source = `
(globalThis.webpackChunkLeft || globalThis.webpackChunkRight).push([[1], { 1: function() {} }]);