Skip to content

Commit e308d7f

Browse files
fix(minify): count loop, destructuring and call writes through the global object
Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 618873d commit e308d7f

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24054,8 +24054,8 @@ const installImprove = (modules) => {
2405424054
};
2405524055

2405624056
/**
24057-
* Whether a member read off the global `Object` is a place written: an
24058-
* assignment's or update's target, or one inside a destructuring.
24057+
* Whether a member access is a place written: an assignment's, update's or
24058+
* loop head's target, or one inside a destructuring.
2405924059
* @param {Node} access the member access
2406024060
* @param {{ parent: (n?: number) => Node }} info its ancestors, `access`'s parent first
2406124061
* @returns {boolean} true when it is written
@@ -25418,14 +25418,10 @@ const installImprove = (modules) => {
2541825418
const writesThrough = (node, walker) => {
2541925419
const parent = walker.parent(0);
2542025420
if (A.isCallNode(parent)) return parent.callee !== node;
25421-
if (!A.isPropAccessNode(parent) || parent.object !== node) return false;
25422-
const grandparent = walker.parent(1);
2542325421
return (
25424-
(A.isAssignNode(grandparent) && grandparent.left === parent) ||
25425-
(A.isUnaryNode(grandparent) &&
25426-
(grandparent.operator === "delete" ||
25427-
grandparent.operator === "++" ||
25428-
grandparent.operator === "--"))
25422+
A.isPropAccessNode(parent) &&
25423+
parent.object === node &&
25424+
writesObjectMember(parent, walker)
2542925425
);
2543025426
};
2543125427
const compressBeforeProbes = compressorMethods.compress;
@@ -25464,14 +25460,17 @@ const installImprove = (modules) => {
2546425460
}
2546525461
}
2546625462
}
25463+
// `Object.defineProperty(globalThis, …)` may rebind any global.
2546725464
if (
2546825465
A.isSymbolRefNode(node) &&
2546925466
GLOBAL_OBJECT_NAMES.has(node.name) &&
25470-
A.isPropAccessNode(walker.parent(0)) &&
2547125467
writesThrough(node, walker)
2547225468
) {
25469+
const access = walker.parent(0);
2547325470
reboundGlobals.add(
25474-
/** @type {string} */ (globalNameOf(walker.parent(0)))
25471+
A.isPropAccessNode(access)
25472+
? /** @type {string} */ (globalNameOf(access))
25473+
: ANY_GLOBAL
2547525474
);
2547625475
}
2547725476
if (depth > 0 && A.isSymbolRefNode(node)) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8976,7 +8976,12 @@ describe("syntax-printer", () => {
89768976
'var k = "Number"; globalThis[k] = { POSITIVE_INFINITY: 7 }; console.log(Number.POSITIVE_INFINITY);',
89778977
'globalThis.Array.prototype.slice = function () { return "patched"; }; console.log([1, 2].slice());',
89788978
'var k = "Array"; globalThis[k].prototype.slice = function () { return "patched"; }; console.log([1, 2].slice());',
8979-
"globalThis.Math.max = function () { return 0; }; console.log(Math.max(1, 2));"
8979+
"globalThis.Math.max = function () { return 0; }; console.log(Math.max(1, 2));",
8980+
'for (globalThis.Number of [function () { return 7; }]); console.log(Number("3"));',
8981+
'[globalThis.Number] = [function () { return 7; }]; console.log(Number("3"));',
8982+
'({ n: globalThis.Number } = { n: function () { return 7; } }); console.log(Number("3"));',
8983+
'Object.defineProperty(globalThis, "Number", { value: function () { return 7; } }); console.log(Number("3"));',
8984+
'[globalThis.Array.prototype.slice] = [function () { return "patched"; }]; console.log([1, 2].slice());'
89808985
]) {
89818986
const { code } = await minify(input, { compress: {}, mangle: false });
89828987
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));

0 commit comments

Comments
 (0)