Skip to content

Commit dbdb36e

Browse files
fix(minify): count a prototype patched through a constructor or global object handed to a call
Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 7b402f3 commit dbdb36e

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25460,6 +25460,13 @@ const installImprove = (modules) => {
2546025460
) {
2546125461
for (const global of name === ANY_GLOBAL ? pureGlobals : [name]) {
2546225462
patchedStatics.add(global);
25463+
// `f(Array)` hands `f` the constructor, whose `prototype` it may write.
25464+
if (
25465+
BUILT_IN_PROTOTYPES_NAMES.has(global) &&
25466+
!A.isPropAccessNode(walker.parent(0))
25467+
) {
25468+
patchedPrototypes.add(global);
25469+
}
2546325470
}
2546425471
}
2546525472
}
@@ -25470,11 +25477,15 @@ const installImprove = (modules) => {
2547025477
writesThrough(node, walker)
2547125478
) {
2547225479
const access = walker.parent(0);
25473-
reboundGlobals.add(
25474-
A.isPropAccessNode(access)
25475-
? /** @type {string} */ (globalNameOf(access))
25476-
: ANY_GLOBAL
25477-
);
25480+
if (A.isPropAccessNode(access)) {
25481+
reboundGlobals.add(/** @type {string} */ (globalNameOf(access)));
25482+
} else {
25483+
reboundGlobals.add(ANY_GLOBAL);
25484+
for (const name of BUILT_IN_PROTOTYPES_NAMES) {
25485+
patchedPrototypes.add(name);
25486+
}
25487+
for (const name of pureGlobals) patchedStatics.add(name);
25488+
}
2547825489
}
2547925490
if (depth > 0 && A.isSymbolRefNode(node)) {
2548025491
probedReads.add(node);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8983,7 +8983,10 @@ describe("syntax-printer", () => {
89838983
'Object.defineProperty(globalThis, "Number", { value: function () { return 7; } }); console.log(Number("3"));',
89848984
'[globalThis.Array.prototype.slice] = [function () { return "patched"; }]; console.log([1, 2].slice());',
89858985
'Object.defineProperty(globalThis?.Array.prototype, "slice", { value: function () { return "S"; } }); console.log([1, 2].slice());',
8986-
'Object.defineProperty(Array?.prototype, "slice", { value: function () { return "S"; } }); console.log([1, 2].slice());'
8986+
'Object.defineProperty(Array?.prototype, "slice", { value: function () { return "S"; } }); console.log([1, 2].slice());',
8987+
'function patch(C) { C.prototype.slice = function () { return "S"; }; } patch(globalThis?.Array); console.log([1, 2].slice());',
8988+
'function patch(C) { C.prototype.slice = function () { return "S"; }; } patch(Array); console.log([1, 2].slice());',
8989+
'function patch(g) { g.Array.prototype.slice = function () { return "S"; }; } patch(globalThis); console.log([1, 2].slice());'
89878990
]) {
89888991
const { code } = await minify(input, { compress: {}, mangle: false });
89898992
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));

0 commit comments

Comments
 (0)