Skip to content

Commit 627b6c9

Browse files
fix(minify): read a parameter as the object handed it, whatever its name, and keep Array(-0) where Array is written
A function handed a second, different object counts as patching it. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent b640357 commit 627b6c9

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25412,13 +25412,14 @@ const installImprove = (modules) => {
2541225412
* @param {Node} node a node
2541325413
* @returns {boolean} true where it does
2541425414
*/
25415-
const isGlobalObjectRef = (node) =>
25416-
A.isSymbolRefNode(node) &&
25417-
(GLOBAL_OBJECT_NAMES.has(node.name)
25418-
? isUndeclaredRef(node)
25419-
: GLOBAL_OBJECT_NAMES.has(
25420-
/** @type {string} */ (parameterAliases.get(node.definition))
25421-
));
25415+
const isGlobalObjectRef = (node) => {
25416+
if (!A.isSymbolRefNode(node)) return false;
25417+
// A parameter named `globalThis` stands for what it is handed.
25418+
const alias = parameterAliases.get(node.definition);
25419+
return alias !== undefined
25420+
? GLOBAL_OBJECT_NAMES.has(alias)
25421+
: GLOBAL_OBJECT_NAMES.has(node.name) && isUndeclaredRef(node);
25422+
};
2542225423
/**
2542325424
* The global a read names, `Array` or `globalThis.Array`, with
2542425425
* `ANY_GLOBAL` for a key through the global object that is not known.
@@ -25540,8 +25541,9 @@ const installImprove = (modules) => {
2554025541
patchedPrototypes.add(global);
2554125542
}
2554225543
for (const global of pureGlobals) patchedStatics.add(global);
25543-
} else if (BUILT_IN_PROTOTYPES_NAMES.has(name)) {
25544-
patchedPrototypes.add(name);
25544+
} else {
25545+
patchedStatics.add(name);
25546+
if (BUILT_IN_PROTOTYPES_NAMES.has(name)) patchedPrototypes.add(name);
2554525547
}
2554625548
};
2554725549
const compressBeforeProbes = compressorMethods.compress;
@@ -25670,12 +25672,15 @@ const installImprove = (modules) => {
2567025672
(parameter !== undefined && !A.isSymbolFunargNode(parameter))
2567125673
) {
2567225674
handedAway(name);
25673-
} else if (
25674-
parameter !== undefined &&
25675-
!parameterAliases.has(parameter.definition)
25676-
) {
25677-
parameterAliases.set(parameter.definition, name);
25678-
A.walkNode(lambda, walker);
25675+
} else if (parameter !== undefined) {
25676+
const alias = parameterAliases.get(parameter.definition);
25677+
if (alias === undefined) {
25678+
parameterAliases.set(parameter.definition, name);
25679+
A.walkNode(lambda, walker);
25680+
} else if (alias !== name) {
25681+
// Read for one, the function is handed another as well.
25682+
handedAway(name);
25683+
}
2567925684
}
2568025685
}
2568125686
}
@@ -25817,7 +25822,7 @@ const installImprove = (modules) => {
2581725822
self.arguments.length === 1 &&
2581825823
A.isSymbolRefNode(self.callee) &&
2581925824
self.callee.name === "Array" &&
25820-
isUndeclaredRef(self.callee) &&
25825+
readsUnassignedGlobal(self.callee, compressor) &&
2582125826
A.isUnaryPrefixNode(length) &&
2582225827
length.operator === "-" &&
2582325828
A.isNumberNode(length.argument) &&

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9105,7 +9105,11 @@ describe("syntax-printer", () => {
91059105
'(function (g) { g.Number = function () { return 7; }; })(globalThis); console.log(Number("3"));',
91069106
'function outer(g) { inner(g); } function inner(h) { h.Number = function () { return 7; }; } outer(globalThis); console.log(Number("3"));',
91079107
'function patch() { arguments[0].Number = function () { return 7; }; } patch(globalThis); console.log(Number("3"));',
9108-
'function patch({ Math: m }, g) { g.Number = function () { return 7; }; } patch(Math, globalThis); console.log(Number("3"));'
9108+
'function patch({ Math: m }, g) { g.Number = function () { return 7; }; } patch(Math, globalThis); console.log(Number("3"));',
9109+
'function patch(target) { target.Number = function () { return 7; }; } patch(Array); patch(globalThis); console.log(Number("3"));',
9110+
'function patch(target) { target.Number = function () { return 7; }; } patch(globalThis); patch(Math); console.log(Math.Number(), Number("3"));',
9111+
'function patch(globalThis) { globalThis.Number = function () { return 7; }; } patch(globalThis); console.log(Number("3"));',
9112+
"Array = function (n) { return 1 / n; }; console.log(Array(-0));"
91099113
]) {
91109114
const { code } = await minify(input, { compress: {}, mangle: false });
91119115
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));

0 commit comments

Comments
 (0)