Skip to content

Commit 86d31e0

Browse files
feat(javascript): drop new arguments its constructor never reads in the minifier (#22506)
A `new` of a fixed function or class whose constructor reads neither `arguments` nor a rest parameter, nor extends another class, ignores arguments past its parameters; side-effect-free trailing ones go. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent fdc01cd commit 86d31e0

4 files changed

Lines changed: 69 additions & 7 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16980,6 +16980,51 @@ const installImprove = (modules) => {
1698016980
);
1698116981
};
1698216982

16983+
/**
16984+
* How many leading arguments of a `new` the constructor it runs can read.
16985+
* @param {Node} node a `new`
16986+
* @returns {number} the count, or -1 where the constructor is unknown or reads `arguments`
16987+
*/
16988+
const constructorArgumentsRead = (node) => {
16989+
const { callee } = node;
16990+
const target = A.isSymbolRefNode(callee) ? A.fixedValue(callee) : callee;
16991+
if (!target) return -1;
16992+
let lambda = target;
16993+
if (A.isClassNode(target)) {
16994+
// An implicit derived constructor passes every argument to `super`.
16995+
if (target.superClass) return -1;
16996+
lambda = undefined;
16997+
for (const property of target.body.body) {
16998+
if (
16999+
A.isConciseMethodNode(property) &&
17000+
!property.static &&
17001+
!property.computed &&
17002+
property.key.name === "constructor"
17003+
) {
17004+
lambda = property.value;
17005+
}
17006+
}
17007+
if (lambda === undefined) return 0;
17008+
} else if (
17009+
!A.isLambdaNode(target) ||
17010+
A.isArrowNode(target) ||
17011+
target.generator ||
17012+
target.async
17013+
) {
17014+
return -1;
17015+
}
17016+
if (
17017+
A.usesArguments(lambda) ||
17018+
A.usesEval(lambda) ||
17019+
lambda.params.some((/** @type {Node} */ parameter) =>
17020+
A.isExpansionNode(parameter)
17021+
)
17022+
) {
17023+
return -1;
17024+
}
17025+
return lambda.params.length;
17026+
};
17027+
1698317028
A.wrapOptimizer("New", (optimizeNew) => (self, compressor) => {
1698417029
const optimized = optimizeNew(self, compressor);
1698517030
if (!improvements.enabled || !A.isNewNode(optimized)) return optimized;
@@ -16996,6 +17041,16 @@ const installImprove = (modules) => {
1699617041
i--;
1699717042
}
1699817043
}
17044+
const read = constructorArgumentsRead(optimized);
17045+
if (
17046+
read >= 0 &&
17047+
args.length > read &&
17048+
!args.some((/** @type {Node} */ arg) => A.isExpansionNode(arg))
17049+
) {
17050+
let end = args.length;
17051+
while (end > read && !A.hasSideEffects(args[end - 1], compressor)) end--;
17052+
args.length = end;
17053+
}
1699917054
if (!constructsWhenCalled(optimized) || globalMayBeRebound(compressor)) {
1700017055
return optimized;
1700117056
}

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,20 +539,13 @@ const SWC_SMALLER_BY_REASON = [
539539
"swc minifier: fixture/issues/11512-exhaustive/iife-anon-arg-unused/input.js",
540540
"swc minifier: fixture/issues/11512-exhaustive/iife-anon-default-unused/input.js",
541541
"swc minifier: fixture/issues/11645/control-known-arity-drop/input.js",
542-
"swc minifier: fixture/issues/11684/bindings/input.js",
543542
"swc minifier: fixture/issues/11684/class-decl/input.js",
544543
"swc minifier: fixture/issues/11684/class-expression/input.js",
545544
"swc minifier: fixture/issues/11684/constructor-scopes/input.js",
546-
"swc minifier: fixture/issues/11684/function-decl/input.js",
547-
"swc minifier: fixture/issues/11684/function-expression/input.js",
548545
"swc minifier: fixture/issues/11684/identifier-reduce-vars-only/input.js",
549546
"swc minifier: fixture/issues/11684/identifier-side-effects/input.js",
550547
"swc minifier: fixture/issues/11684/identifier-unused-only/input.js",
551-
"swc minifier: fixture/issues/11684/input.js",
552-
"swc minifier: fixture/issues/11684/reduce-vars-only/input.js",
553-
"swc minifier: fixture/issues/11684/scopes-and-mutations/input.js",
554548
"swc minifier: fixture/issues/11684/side-effects/input.js",
555-
"swc minifier: fixture/issues/11684/unused-only/input.js",
556549
"swc minifier: fixture/issues/12212/safe-unused-rest/input.js",
557550
"swc minifier: fixture/issues/12218/leading-argument/input.js",
558551
"terser compress: collapse_vars/issue_2436_12",

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,13 @@ const IMPROVED_YET_BIGGER = {
14301430
// Sources whose shorter spelling no longer repeats text gzip matched, by the
14311431
// spelling written; an entry that stops compressing worse fails until retired.
14321432
for (const [reason, keys] of /** @type {[string, string[]][]} */ ([
1433+
[
1434+
"`new` arguments its constructor no longer reads dropped, where a twin `new` beside it keeps them",
1435+
[
1436+
"new/new_statements_3 (a module mangled at its top level)",
1437+
"new/new_statements_3 (the default minimizer's options)"
1438+
]
1439+
],
14331440
[
14341441
"`Array` written as a literal where the source writes the call again",
14351442
[

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ const IMPROVED_CASES = [
261261
`console.log(0); (() => { ${TRY} console.log(2); })(); console.log(3);`,
262262
{ compress: {}, mangle: false }
263263
],
264+
[
265+
"a `new` passing arguments past what its constructor reads",
266+
'function f() { function Z(a) { this.a = a; } class A { constructor() { this.n = 1; } } class B {} return [new Z(1, 2, 3).a, new A(1, 2).n, new B(1) instanceof B, new Z(4, g(), 5).a]; } function g() { console.log("g"); } console.log(f());',
267+
{ compress: {}, mangle: false }
268+
],
264269
[
265270
"two calls in a sequence, one keeping a `let` in a block",
266271
`(() => { let a = console.log.name; console.log(a, a); })(); (() => { ${TRY} console.log(2); })();`,
@@ -648,6 +653,8 @@ const KEPT_CASES = [
648653
["an array literal joining to a string longer than it", 'console.log([!0, !0, !0] + "");'],
649654
["`+` and `~` of what a literal does not give", "function f(a) { return [+[a], ~a !== 0 ? 1 : 2]; } console.log(f(1), f(1n));"],
650655
["a string literal indexed past its end, by no constant or as a target", 'function f(i) { var s = "abc"; "abc"[0] = 1; return ["abc"[5], "abc"[i], "abc"[1.5], "abc"[-1]]; } console.log(f(1));'],
656+
["a `new` whose constructor reads `arguments` or a rest, extends a class, or is rebound", 'function f(h) { function Z(a) { this.n = arguments.length; } function R(...a) { this.n = a.length; } class B { constructor(a) { this.n = arguments.length; } } class C extends B {} var V = function (a) { this.n = 1; }; if (h) V = function () { this.n = arguments.length; }; return [new Z(1, 2).n, new R(1, 2).n, new C(1, 2).n, new V(1, 2).n]; } console.log(f(0), f(1));'],
657+
["a `new` of what constructs nothing", 'function f() { var F = () => 1, G = function* (a) {}, H = async function (a) {}, O = { a: 1 }; try { new F(1, 2); } catch (e) { console.log(e.name); } try { new G(1, 2); } catch (e) { console.log(e.name); } try { new H(1, 2); } catch (e) { console.log(e.name); } try { new O(1, 2); } catch (e) { console.log(e.name); } } f();'],
651658
["a `Boolean` passed a spread or a second argument", "function f(a, b) { return [Boolean(...a), Boolean(a, b())]; } console.log(f([0], () => 1));"],
652659
["a conversion of no literal, which `+` and `\"\"+` would read otherwise for a BigInt or a symbol", "function f(a) { return [Number(a), String(a), BigInt(a)]; } console.log(f(1n));"],
653660
["a conversion a variable shadows", "function f(Boolean, Number) { return [Boolean(1), Number(true)]; } console.log(f(String, String));"],

0 commit comments

Comments
 (0)