Skip to content

Commit da68334

Browse files
fix(minify): follow a name set to a local function when it is handed a global
`const alias = patch; alias(Array)` no longer lets a prototype patch go unseen. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 742fca5 commit da68334

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25509,6 +25509,9 @@ const installImprove = (modules) => {
2550925509
const localFunctions = new Map();
2551025510
/** @type {Set<Node>} */
2551125511
const writtenNames = new Set();
25512+
// The name a name declared once was set to, by declaring symbol.
25513+
/** @type {Map<Node, Node>} */
25514+
const functionAliases = new Map();
2551225515
// An `eval`, direct or not, may write any binding it can see, unseen.
2551325516
let programReadsEval = false;
2551425517
/**
@@ -25533,6 +25536,36 @@ const installImprove = (modules) => {
2553325536
? undefined
2553425537
: localFunctions.get(declaration);
2553525538
};
25539+
/**
25540+
* The name a callee stands for, following names declared once, never
25541+
* written again, and set to another name; a cycle stops where it closes.
25542+
* @param {Node} callee the call's callee
25543+
* @returns {Node} the last name followed, or the callee itself
25544+
*/
25545+
const aliasedFunction = (callee) => {
25546+
/** @type {Set<Node>} */
25547+
const seen = new Set();
25548+
let current = callee;
25549+
if (programReadsEval) return current;
25550+
while (
25551+
A.isSymbolRefNode(current) &&
25552+
current.definition &&
25553+
current.definition.identifiers.length === 1
25554+
) {
25555+
const [declaration] = current.definition.identifiers;
25556+
const target = functionAliases.get(declaration);
25557+
if (
25558+
target === undefined ||
25559+
writtenNames.has(declaration) ||
25560+
seen.has(declaration)
25561+
) {
25562+
break;
25563+
}
25564+
seen.add(declaration);
25565+
current = target;
25566+
}
25567+
return current;
25568+
};
2553625569
/**
2553725570
* Records what a function the program defines but cannot be read may do
2553825571
* with the global object or a built-in handed to it.
@@ -25569,13 +25602,15 @@ const installImprove = (modules) => {
2556925602
parameterAliases.clear();
2557025603
localFunctions.clear();
2557125604
writtenNames.clear();
25605+
functionAliases.clear();
2557225606
}
2557325607
};
2557425608
// Runs once scope is known, so a local named `self` is told from the global.
2557525609
collapseHooks.probeProgram = (toplevel) => {
2557625610
parameterAliases.clear();
2557725611
localFunctions.clear();
2557825612
writtenNames.clear();
25613+
functionAliases.clear();
2557925614
programReadsEval = false;
2558025615
if (improvements.enabled) {
2558125616
let depth = 0;
@@ -25650,6 +25685,13 @@ const installImprove = (modules) => {
2565025685
A.isLambdaNode(node.init)
2565125686
) {
2565225687
localFunctions.set(node.id.definition.identifiers[0], node.init);
25688+
} else if (
25689+
A.isVarDefNode(node) &&
25690+
A.isSymbolDeclarationNode(node.id) &&
25691+
node.init &&
25692+
A.isSymbolRefNode(node.init)
25693+
) {
25694+
functionAliases.set(node.id.definition.identifiers[0], node.init);
2565325695
} else if (
2565425696
A.isSymbolRefNode(node) &&
2565525697
(isWrittenName(node, walker) ||
@@ -25680,7 +25722,7 @@ const installImprove = (modules) => {
2568025722
// it, until no call hands one on.
2568125723
for (let i = 0; i < handOffs.length; i++) {
2568225724
const { call, argument, name } = handOffs[i];
25683-
const lambda = calledFunction(call.callee);
25725+
const lambda = calledFunction(aliasedFunction(call.callee));
2568425726
if (lambda === undefined || !A.isLambdaNode(lambda)) continue;
2568525727
const index = call.arguments.indexOf(argument);
2568625728
const parameter = lambda.params[index];

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9102,6 +9102,8 @@ describe("syntax-printer", () => {
91029102
'Object.assign(globalThis, { Number: function () { return 7; } }); console.log(Number("3"));',
91039103
'function patch(C) { C.prototype.slice = function () { return "S"; }; } patch(globalThis?.Array); console.log([1, 2].slice());',
91049104
'function patch(C) { C.prototype.slice = function () { return "S"; }; } patch(Array); console.log([1, 2].slice());',
9105+
'function patch(C) { C.prototype.slice = function () { return "S"; }; } const alias = patch; alias(Array); console.log([1, 2].slice());',
9106+
'function patch(t) { t.Number = function () { return 7; }; } var alias = patch, again = alias; again(globalThis); console.log(Number("3"));',
91059107
'var patch = function (g) { g.Array.prototype.slice = function () { return "S"; }; }; patch(globalThis); console.log([1, 2].slice());',
91069108
'(function (g) { g.Number = function () { return 7; }; })(globalThis); console.log(Number("3"));',
91079109
'function outer(g) { inner(g); } function inner(h) { h.Number = function () { return 7; }; } outer(globalThis); console.log(Number("3"));',
@@ -9121,7 +9123,8 @@ describe("syntax-printer", () => {
91219123
'function f() { var self = { a: 1 }; g(self); } function g(o) { o.Number = 1; } f(); console.log(Number("3"));',
91229124
'function isGlobal(g) { return g && g.Math === Math; } console.log(isGlobal(globalThis), Number("3"));',
91239125
'function name(C) { return C.name; } console.log(name(Array), [1, 2].slice(), Number("3"));',
9124-
'console.log(["1", "2"].map(Number), Number("3"));'
9126+
'console.log(["1", "2"].map(Number), Number("3"));',
9127+
'var a = b, b = a; try { a(globalThis); } catch (e) {} console.log(Number("3"));'
91259128
]) {
91269129
const { code } = await minify(input, { compress: {}, mangle: false });
91279130
expect(code).not.toContain('Number("3")');

0 commit comments

Comments
 (0)