Skip to content

Commit 5485483

Browse files
committed
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.
1 parent c916850 commit 5485483

3 files changed

Lines changed: 272 additions & 43 deletions

File tree

‎src/domain/javascript/javascriptAstFingerprint.ts‎

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import * as t from "@babel/types";
55
import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js";
66

77
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
8-
import { propertyName } from "./javascriptAstValues.js";
8+
import {
9+
propertyName,
10+
semanticStaticPropertyKey,
11+
} from "./javascriptAstValues.js";
912
import { stringValue } from "./javascriptStaticAnalysisHelpers.js";
1013

1114
/** Static CommonJS export names. */
@@ -71,35 +74,54 @@ const collectAssignmentExports = (
7174
output: Set<string>,
7275
): void => {
7376
const path = memberPath(left);
74-
if (path === "exports" || path === "module.exports") {
77+
if (path === null) return;
78+
// Rebinding the CommonJS alias does not replace module.exports.
79+
if (path.length === 1 && path[0] === "exports") return;
80+
const exportOffset =
81+
path[0] === "exports"
82+
? 1
83+
: path[0] === "module" && path[1] === "exports"
84+
? 2
85+
: null;
86+
if (exportOffset === null) return;
87+
if (path.length === exportOffset) {
7588
if (t.isObjectExpression(right)) collectObjectKeys(right, output);
7689
else addExport(output, "default");
7790
return;
7891
}
79-
if (path.startsWith("exports."))
80-
addExport(output, path.slice("exports.".length));
81-
if (path.startsWith("module.exports."))
82-
addExport(output, path.slice("module.exports.".length));
92+
// A CommonJS export assignment exposes the first property below the
93+
// exports object. Deeper writes mutate that exported value, rather than
94+
// creating an export whose name is the dotted member path.
95+
addExport(output, path[exportOffset]!);
8396
};
8497

8598
const collectCallExports = (
8699
call: t.CallExpression,
87100
output: Set<string>,
88101
): void => {
89102
const callee = memberPath(call.callee);
90-
if (callee === "Object.defineProperty") {
91-
const target = memberPath(call.arguments[0]);
103+
if (callee === null) return;
104+
const definePropertyCall =
105+
callee.length === 2 &&
106+
callee[0] === "Object" &&
107+
callee[1] === "defineProperty";
108+
const declarationCall = callee.length >= 2 && callee.at(-1) === "d";
109+
if (!definePropertyCall && !declarationCall) return;
110+
const target = memberPath(call.arguments[0]);
111+
const exportTarget =
112+
target !== null &&
113+
((target.length === 1 && target[0] === "exports") ||
114+
(target.length === 2 &&
115+
target[0] === "module" &&
116+
target[1] === "exports"));
117+
if (!exportTarget) return;
118+
if (definePropertyCall) {
92119
const name = stringValue(call.arguments[1]);
93-
if ((target === "exports" || target === "module.exports") && name)
94-
addExport(output, name);
120+
if (name !== undefined) addExport(output, name);
121+
return;
95122
}
96-
if (!callee.endsWith(".d")) return;
97-
const target = memberPath(call.arguments[0]);
98123
const declarations = call.arguments[1];
99-
if (
100-
(target === "exports" || target === "module.exports") &&
101-
t.isObjectExpression(declarations)
102-
)
124+
if (t.isObjectExpression(declarations))
103125
collectObjectKeys(declarations, output);
104126
};
105127

@@ -109,40 +131,50 @@ const collectObjectKeys = (
109131
): void => {
110132
for (const property of object.properties) {
111133
if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) continue;
112-
const name = propertyName(property.key);
113-
if (name !== "") addExport(output, name);
134+
const name = semanticStaticPropertyKey(property.key, property.computed);
135+
// In an object literal, a non-computed `__proto__: value` sets the
136+
// prototype instead of creating an own property. Shorthand, computed,
137+
// and method forms do create own properties.
138+
if (
139+
name !== null &&
140+
!(
141+
t.isObjectProperty(property) &&
142+
!property.computed &&
143+
!property.shorthand &&
144+
name === "__proto__"
145+
)
146+
)
147+
addExport(output, name);
114148
}
115149
};
116150

117151
const addExport = (output: Set<string>, name: string): void => {
118152
output.add(name);
119153
};
120154

121-
const memberPath = (node: t.Node | null | undefined): string => {
155+
const memberPath = (
156+
node: t.Node | null | undefined,
157+
): readonly string[] | null => {
122158
const properties: string[] = [];
123159
let current = node;
124160
while (
125161
current !== undefined &&
126162
current !== null &&
127163
(t.isMemberExpression(current) || t.isOptionalMemberExpression(current))
128164
) {
129-
properties.push(propertyName(current.property));
130-
current = t.isNode(current.object) ? current.object : undefined;
131-
}
132-
133-
const root =
134-
current !== undefined && t.isIdentifier(current)
135-
? current.name
136-
: current !== undefined && t.isThisExpression(current)
137-
? "this"
138-
: "";
139-
if (properties.length === 0) return root;
140-
141-
properties.reverse();
142-
const path = root === "" ? [] : [root];
143-
for (const property of properties) {
144-
if (path.length === 0 && property === "") continue;
145-
path.push(property);
165+
const property = semanticStaticPropertyKey(
166+
current.property,
167+
current.computed,
168+
);
169+
if (property === null) return null;
170+
properties.push(property);
171+
current = current.object;
146172
}
147-
return path.join(".");
173+
if (current === undefined || current === null) return null;
174+
const root = t.isIdentifier(current)
175+
? current.name
176+
: t.isThisExpression(current)
177+
? "this"
178+
: null;
179+
return root === null ? null : [root, ...properties.reverse()];
148180
};

‎src/domain/javascript/javascriptStaticAnalysis.member_path_depth.test.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,41 @@ const ordinaryMemberChain = ".next".repeat(64);
1010
it("collects a 12,000-member CommonJS export path without recursion", () => {
1111
const file = parseJavaScriptSource(`exports${memberChain}.last = 1;`);
1212
if (file === null) throw new Error("Expected valid JavaScript");
13-
expect(collectJavaScriptExports(file).values).toEqual([
14-
`${"next.".repeat(12_000)}last`,
15-
]);
13+
expect(collectJavaScriptExports(file).values).toEqual(["next"]);
1614
});
1715

18-
it("preserves empty, dynamic, this, and private member path behavior", () => {
16+
it("preserves exact empty keys without inventing dynamic, this, or private exports", () => {
1917
const file = parseJavaScriptSource(`
2018
exports[""].value = 1;
2119
exports[""] = 0;
2220
exports[key].value = 2;
21+
exports.nested = {};
22+
exports.nested.child = 3;
23+
exports["nested.child"] = 4;
2324
this.exports.ignored = 3;
2425
class Example { #private = 0; read() { this.#private = 1; } }
2526
`);
2627
if (file === null) throw new Error("Expected valid JavaScript");
2728
expect(collectJavaScriptExports(file).values).toEqual([
2829
"",
29-
".value",
30-
"key.value",
30+
"nested",
31+
"nested.child",
3132
]);
3233
});
3334

35+
it("distinguishes the object literal prototype setter from own export keys", () => {
36+
const prototypeSetter = parseJavaScriptSource(
37+
"module.exports = { __proto__: {} };",
38+
);
39+
const ownKeys = parseJavaScriptSource(
40+
'module.exports = { ["__proto__"]: 1, __proto__() {} };',
41+
);
42+
if (prototypeSetter === null || ownKeys === null)
43+
throw new Error("Expected valid JavaScript");
44+
expect(collectJavaScriptExports(prototypeSetter).values).toEqual([]);
45+
expect(collectJavaScriptExports(ownKeys).values).toEqual(["__proto__"]);
46+
});
47+
3448
it("keeps left-first and outer-member-first bundler runtime matches", () => {
3549
const source = `
3650
(globalThis.webpackChunkLeft || globalThis.webpackChunkRight).push([[1], { 1: function() {} }]);

0 commit comments

Comments
 (0)