Skip to content

Commit 2040f9c

Browse files
feat(javascript): move a value past a read of a built-in in the minifier
On the last pass, `collapse_vars` no longer stops at a read of `console`, a built-in global or one of its standard methods, which no value moved past it can change, where the declaration it moves goes away entirely. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent a067e66 commit 2040f9c

5 files changed

Lines changed: 99 additions & 31 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16350,6 +16350,33 @@ const installImprove = (modules) => {
1635016350
unusedHooks.dropsCatchBinding = (compressor) =>
1635116351
improvements.enabled && compressor.option("ecma") >= 2019;
1635216352

16353+
/**
16354+
* A global `collapse_vars` may move a value past: a built-in, or `console`,
16355+
* which every host defines and nothing in a bundle is expected to replace.
16356+
* @param {Node} node a name
16357+
* @param {CompressorShape} compressor the compressor
16358+
* @returns {boolean} true for such a global
16359+
*/
16360+
const isBuiltInRead = (node, compressor) =>
16361+
A.isSymbolRefNode(node) &&
16362+
(pureGlobals.has(node.name) || node.name === "console") &&
16363+
isUndeclaredRef(node) &&
16364+
!globalMayBeRebound(compressor);
16365+
16366+
/** @type {CollapseHooks} */
16367+
const collapseHooks = modules.webpackCollapse;
16368+
collapseHooks.readsBuiltIn = (node, compressor) => {
16369+
if (!improvements.enabled) return false;
16370+
if (A.isSymbolRefNode(node)) return isBuiltInRead(node, compressor);
16371+
if (!A.isDotNode(node) || node.optional) return false;
16372+
const { object } = node;
16373+
if (!isBuiltInRead(object, compressor)) return false;
16374+
// A console's members are its methods; a built-in's, the standard ones.
16375+
if (object.name === "console") return true;
16376+
const statics = callableStatics.get(object.name);
16377+
return statics !== undefined && statics.has(node.property.name);
16378+
};
16379+
1635316380
const constructedWhenCalled = new Set(foldedTables.constructedWhenCalled);
1635416381

1635516382
/**
@@ -21042,6 +21069,12 @@ const installDropSideEffectFree = ({
2104221069
};
2104321070
};
2104421071

21072+
/**
21073+
* @typedef {object} CollapseHooks what later phases add to `collapse_vars`
21074+
* @property {((node: Node, compressor: CompressorShape) => boolean) | null} readsBuiltIn whether a node only reads a built-in global or one of its members, which no value moved past it can change
21075+
* @property {boolean} finalPass whether the pass running is the last one asked for
21076+
*/
21077+
2104521078
/**
2104621079
* Creates terser's `tighten_body`, which joins, reorders and drops the
2104721080
* statements of a block and folds assignments into their first use.
@@ -21051,6 +21084,10 @@ const installDropSideEffectFree = ({
2105121084
const createTightenBody = (modules) => {
2105221085
const { transformNode, walkNode } = modules.ast;
2105321086
const { ast, common, flags, inference, nativeObjects, utils } = modules;
21087+
// What a later phase lets `collapse_vars` move a value past.
21088+
/** @type {CollapseHooks} */
21089+
const collapseHooks = { readsBuiltIn: null, finalPass: true };
21090+
modules.webpackCollapse = collapseHooks;
2105421091
const {
2105521092
ArrayNode,
2105621093
isArrowNode,
@@ -21829,7 +21866,8 @@ const createTightenBody = (modules) => {
2182921866
isSymbolRefNode(node) &&
2183021867
!ast.isDeclared(node, compressor) &&
2183121868
// Likely a terser bug: a set of names asked for a node is never true.
21832-
!purePropertyAccessGlobals.has(node)) ||
21869+
!purePropertyAccessGlobals.has(node) &&
21870+
!readsBuiltIn(node)) ||
2183321871
(isSymbolRefNode(node) &&
2183421872
isCallNode(parent) &&
2183521873
hasAnnotation(parent, _NOINLINE)) ||
@@ -21918,13 +21956,14 @@ const createTightenBody = (modules) => {
2191821956
(isExitNode(node) &&
2191921957
(sideEffects || isPropAccessNode(lhs) || mayModify(lhs))) ||
2192021958
(isPropAccessNode(node) &&
21921-
(sideEffects || ast.mayThrowOnAccess(node.object, compressor))) ||
21959+
(sideEffects || ast.mayThrowOnAccess(node.object, compressor)) &&
21960+
!readsBuiltIn(node)) ||
2192221961
(isSymbolRefNode(node) &&
2192321962
((hasLeftValue(node.name) &&
2192421963
/** @type {{ modified: boolean }} */ (
2192521964
leftValues().get(node.name)
2192621965
).modified) ||
21927-
(sideEffects && mayModify(node)))) ||
21966+
(sideEffects && mayModify(node) && !readsBuiltIn(node)))) ||
2192821967
(isVarDefNode(node) &&
2192921968
node.init &&
2193021969
(hasLeftValue(node.id.name) ||
@@ -22524,6 +22563,45 @@ const createTightenBody = (modules) => {
2252422563
return false;
2252522564
}
2252622565

22566+
/**
22567+
* Whether a node only reads a built-in global or one of its members.
22568+
* @param {Node} node a node
22569+
* @returns {boolean} true where a later phase reads it so
22570+
*/
22571+
function readsBuiltIn(node) {
22572+
// Moved earlier, a value would hide from later passes what they fold.
22573+
return (
22574+
collapseHooks.finalPass &&
22575+
collapseHooks.readsBuiltIn !== null &&
22576+
dropsCandidate() &&
22577+
collapseHooks.readsBuiltIn(node, compressor)
22578+
);
22579+
}
22580+
22581+
/**
22582+
* Whether collapsing the candidate leaves nothing of it behind.
22583+
* @returns {boolean} true for a declaration of a name read once and written nowhere else
22584+
*/
22585+
function dropsCandidate() {
22586+
if (!isVarDefNode(candidate) || isSymbolFunargNode(candidate.id)) {
22587+
return false;
22588+
}
22589+
const candidateDefinition = candidate.id.definition;
22590+
return (
22591+
candidateDefinition.references.length -
22592+
candidateDefinition.replaced ===
22593+
1 &&
22594+
candidateDefinition.identifiers.length === 1 &&
22595+
!candidateDefinition.assignments &&
22596+
!compressor.exposed(candidateDefinition) &&
22597+
!(
22598+
candidateDefinition.global &&
22599+
compressor.top_retain &&
22600+
compressor.top_retain(candidateDefinition)
22601+
)
22602+
);
22603+
}
22604+
2252722605
/**
2252822606
* terser's `may_modify`.
2252922607
* @param {Node} symbol a symbol or a destructuring
@@ -24265,7 +24343,10 @@ const installStatementOptimizers = (modules, helpers) => {
2426524343
let minimumCount = Infinity;
2426624344
let stopping = false;
2426724345
const mangle = this.mangle_options();
24346+
/** @type {CollapseHooks} */
24347+
const collapseHooks = modules.webpackCollapse;
2426824348
for (let pass = 0; pass < passes; pass++) {
24349+
collapseHooks.finalPass = pass + 1 === passes;
2426924350
ast.figureOutScope(this._toplevel, mangle);
2427024351
if (pass === 0 && this.option("drop_console")) {
2427124352
// Before `reduce_vars` and the pass itself.
@@ -24304,6 +24385,7 @@ const installStatementOptimizers = (modules, helpers) => {
2430424385
}
2430524386
}
2430624387
}
24388+
collapseHooks.finalPass = true;
2430724389
if (this.option("expression")) {
2430824390
ast.processExpression(this._toplevel, false);
2430924391
}

‎test/helpers/oxcSmaller.js‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ const OXC_SMALLER_BY_REASON = [
187187
"swc minifier: fixture/issues/vercel/002/input.js",
188188
"swc minifier: fixture/issues/vercel/004/input.js",
189189
"swc minifier: fixture/next/46887-2/input.js",
190-
"swc minifier: fixture/next/46887/input.js",
191190
"swc minifier: fixture/pr/11381/input.js",
192191
"swc minifier: fixture/pr/6272/input.js",
193192
"swc minifier: fixture/projects/backbone/16/input.js",
@@ -704,7 +703,6 @@ const OXC_SMALLER_BY_REASON = [
704703
"swc minifier: fixture/issues/emotion/react/2/input.js",
705704
"swc minifier: fixture/issues/firebase-core/1/input.js",
706705
"swc minifier: fixture/issues/firebase/dist/1/input.js",
707-
"swc minifier: fixture/issues/react-autosuggest/1/input.js",
708706
"swc minifier: fixture/issues/react-instancesearch/004/input.js",
709707
"swc minifier: fixture/issues/react/hooks/3/input.js",
710708
"swc minifier: fixture/issues/vercel/005/input.js",
@@ -755,7 +753,6 @@ const OXC_SMALLER_BY_REASON = [
755753
"swc minifier: fixture/issues/11684/bindings/input.js",
756754
"swc minifier: fixture/issues/5684/input.js",
757755
"swc minifier: fixture/issues/6407/1/input.js",
758-
"swc minifier: fixture/issues/7004/input.js",
759756
"swc minifier: fixture/issues/8324/input.js",
760757
"swc minifier: fixture/issues/9460/strict-mode/input.js",
761758
"swc minifier: fixture/issues/framer-motion/1/input.js",
@@ -828,7 +825,6 @@ const OXC_SMALLER_BY_REASON = [
828825
[
829826
"oxc minifier: peephole/dead_code_elimination.rs:241",
830827
"oxc minifier: peephole/dead_code_elimination.rs:346",
831-
"oxc minifier: peephole/dead_code_elimination.rs:393",
832828
"oxc minifier: peephole/dead_code_elimination.rs:600",
833829
"oxc minifier: peephole/dead_code_elimination.rs:605",
834830
"oxc minifier: peephole/dead_code_elimination.rs:610",
@@ -1056,14 +1052,11 @@ const OXC_SMALLER_BY_REASON = [
10561052
"swc exec: terser_issue_1466_same_variable_in_multiple_for_of",
10571053
"swc exec: terser_issue_1466_same_variable_in_multiple_for_of_sequences_const",
10581054
"swc exec: terser_issue_1466_same_variable_in_multiple_for_of_sequences_let",
1059-
"swc exec: terser_reduce_vars_issue_3110_3",
1060-
"swc minifier: fixture/issues/5280/input.js",
10611055
"swc minifier: fixture/issues/5846/input.js",
10621056
"swc minifier: fixture/issues/7228/1/input.js",
10631057
"swc minifier: fixture/issues/7770/2/input.js",
10641058
"swc minifier: fixture/issues/next-97517/input.js",
10651059
"swc minifier: fixture/member_expr/callee/input.js",
1066-
"swc minifier: mangle/issue-5766/2/input.js",
10671060
"swc minifier: mangle/issue-5766/for-in/2/input.js",
10681061
"terser compress: block-scope/issue_508",
10691062
"terser compress: collapse_vars/collapse_vars_object",
@@ -1083,9 +1076,6 @@ const OXC_SMALLER_BY_REASON = [
10831076
"terser compress: issue-1466/same_variable_in_multiple_forOf_sequences_const",
10841077
"terser compress: issue-1466/same_variable_in_multiple_forOf_sequences_let",
10851078
"terser compress: issue-1466/same_variable_in_multiple_for_loop",
1086-
"terser compress: reduce_vars/issue_1107",
1087-
"terser compress: reduce_vars/issue_3110_2",
1088-
"terser compress: reduce_vars/issue_3110_3",
10891079
"terser compress: reduce_vars/unsafe_evaluate_side_effect_free_2",
10901080
"terser compress: try-catch/issue_452"
10911081
]
@@ -1192,7 +1182,6 @@ const OXC_SMALLER_BY_REASON = [
11921182
"terser compress: conditionals/issue_ternary_and_nullish_coalescing_operator",
11931183
"terser compress: evaluate/issue_2535_3",
11941184
"terser compress: inline/dont_inline_funcs_into_default_param_2",
1195-
"terser compress: inline/inline_func_with_name_existing_in_block_scope",
11961185
"terser compress: issue-1043/issue_1043",
11971186
"terser compress: issue-1446/typeof_eq_undefined",
11981187
"terser compress: issue-1446/typeof_eq_undefined_ie8",
@@ -1259,7 +1248,6 @@ const OXC_SMALLER_BY_REASON = [
12591248
"oxc minifier: peephole/inline_single_use_variable.rs:47",
12601249
"oxc minifier: peephole/inline_single_use_variable.rs:533",
12611250
"oxc minifier: peephole/inline_single_use_variable.rs:556",
1262-
"oxc minifier: peephole/inline_single_use_variable.rs:573",
12631251
"oxc minifier: peephole/inline_single_use_variable.rs:80"
12641252
]
12651253
],

‎test/helpers/swcSmaller.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,6 @@ const SWC_SMALLER_BY_REASON = [
201201
"swc minifier: fixture/issues/10041/input.js",
202202
"swc minifier: fixture/issues/12118/input.js",
203203
"swc minifier: fixture/issues/12188/input.js",
204-
"swc minifier: fixture/issues/12380/input.js",
205-
"swc minifier: fixture/issues/2779/1/input.js",
206-
"swc minifier: fixture/issues/2779/2/input.js",
207204
"swc minifier: fixture/issues/3629/1/input.js",
208205
"swc minifier: fixture/issues/5846/input.js",
209206
"swc minifier: fixture/issues/5910/1/input.js",
@@ -214,8 +211,6 @@ const SWC_SMALLER_BY_REASON = [
214211
"swc minifier: fixture/issues/7402/input.js",
215212
"swc minifier: fixture/issues/7500/input.js",
216213
"swc minifier: fixture/issues/7568/1/input.js",
217-
"swc minifier: fixture/issues/7739/1/input.js",
218-
"swc minifier: fixture/issues/7847/input.js",
219214
"swc minifier: fixture/issues/8173/input.js",
220215
"swc minifier: fixture/issues/8718/3/input.js",
221216
"swc minifier: fixture/issues/8718/4/input.js",
@@ -239,7 +234,6 @@ const SWC_SMALLER_BY_REASON = [
239234
"terser compress: collapse_vars/collapse_vars_side_effects_2",
240235
"terser compress: collapse_vars/collapse_vars_while",
241236
"terser compress: collapse_vars/issue_1537_destructuring_1",
242-
"terser compress: collapse_vars/issue_2187_1",
243237
"terser compress: collapse_vars/issue_2436_9",
244238
"terser compress: collapse_vars/var_defs",
245239
"terser compress: collapse_vars/var_side_effects_2",
@@ -259,7 +253,6 @@ const SWC_SMALLER_BY_REASON = [
259253
"terser compress: functions/issue_2663_1",
260254
"terser compress: functions/issue_2663_3",
261255
"terser compress: harmony/issue_2874_3",
262-
"terser compress: harmony/object_spread_unsafe",
263256
"terser compress: hoist_props/issue_2473_4",
264257
"terser compress: hoist_props/issue_851_hoist_to_conflicting_name",
265258
"terser compress: hoist_props/name_collision_1",
@@ -446,7 +439,6 @@ const SWC_SMALLER_BY_REASON = [
446439
"swc minifier: fixture/issues/6957/1/input.js",
447440
"swc minifier: fixture/issues/6957/2/input.js",
448441
"swc minifier: fixture/issues/7714/1/input.js",
449-
"swc minifier: fixture/issues/9741_multiple_methods/input.js",
450442
"swc minifier: fixture/issues/non-finite-number-method-call/input.js",
451443
"swc minifier: fixture/next/swc-4559/input.js",
452444
"swc minifier: pass-1/compute/1/input.js",
@@ -611,9 +603,7 @@ const SWC_SMALLER_BY_REASON = [
611603
"swc minifier: fixture/member_expr/callee/input.js",
612604
"swc minifier: fixture/projects/wmr/archive-1/chunks/index.bf24abaa/input.js",
613605
"swc minifier: fixture/projects/wmr/archive-1/chunks/index.ddc4110d/input.js",
614-
"swc minifier: pass-1/issue-6788/1/input.js",
615-
"terser compress: hoist_props/direct_access_2",
616-
"terser compress: template-string/tagged_call_with_invalid_escape_2"
606+
"terser compress: hoist_props/direct_access_2"
617607
]
618608
],
619609
[

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,6 @@ const IMPROVED_YET_BIGGER = {
12811281
"40 bytes fewer, 3 more gzipped: `this.p=c?x:y` breaks the `c?this.p=x:this.p=y` runs gzip reused",
12821282
"projects/files/jquery.mobile-1.4.2.js (the default minimizer's options)":
12831283
"30 bytes fewer, 2 more gzipped: `this.p=c?x:y` breaks the `c?this.p=x:this.p=y` runs gzip reused",
1284-
"benches-full/vue.js (the default minimizer's options)":
1285-
"9 bytes fewer, 6 more gzipped: the shorter `typeof x<\"u\"` and constructor calls break runs gzip reused",
12861284
"fixture/projects/next/.archive-4/framework-054ead69ea8124b4cb27/input.js (its own options)":
12871285
"83 bytes fewer, 8 more gzipped: the shorter `typeof x<\"u\"` breaks the `\"…\"!=typeof` runs gzip reused",
12881286
"fixture/projects/next/.archive-4/framework-054ead69ea8124b4cb27/input.js (the default minimizer's options)":
@@ -1311,8 +1309,6 @@ const IMPROVED_YET_BIGGER = {
13111309
"10 bytes fewer, 7 more gzipped: the folded `[3,2,1].join(\"-\")` matched the one left after it",
13121310
"harmony/issue_2345 (reminify 8 {\"compress\":{\"reduce_funcs\":false}})":
13131311
"10 bytes fewer, 7 more gzipped: the folded `[3,2,1].join(\"-\")` matched the one left after it",
1314-
"fixture/issues/12215/input.js (its own options)":
1315-
"20 bytes fewer, 5 more gzipped: the folded global call matched a shadowed one beside it",
13161312
"issue_8864_1 (its own options)":
13171313
"19 bytes fewer, 1 more gzipped: the folded `toUpperCase()` matched the one left in the template",
13181314
"S15.1.3.3_A4_T2.js (the default minimizer's options)":
@@ -1586,6 +1582,13 @@ for (const [reason, keys] of /** @type {[string, string[]][]} */ ([
15861582
"fixture/issues/string-index-utf16/input.js (the default minimizer's options)"
15871583
]
15881584
],
1585+
[
1586+
"a value read where a built-in read stood between, where the source repeats the declaration gzip matched",
1587+
[
1588+
"regress-1383630.js (the default minimizer's options)",
1589+
"reviver-forward-modifies-object.js (the default minimizer's options)"
1590+
]
1591+
],
15891592
[
15901593
"a pattern's unused names dropped where the source repeats the pattern gzip matched",
15911594
[

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ const IMPROVED_CASES = [
522522
"an array of strings split beside long arrays holding a number or a hole, left alone",
523523
`var a = ${stringArray(100)}, b = ${stringArray(100).slice(0, -1)}, 1], c = ${stringArray(100).slice(0, -1)}, , "z"]; console.log(a.length, typeof b[100], 100 in c, c.length);`
524524
],
525+
[
526+
"a value moved past a read of `console`, a built-in or one of its methods",
527+
"function f(x) { var r = x + 1, a = r * r - r; console.log(a + 7); var v = g(); console.log(Math.max(v, 1)); var c; c = 2; console.log(c); } function g() { return 3; } f(1);"
528+
],
525529
[
526530
"an array of empty strings",
527531
`var a = ${stringArray(100, () => "")}; console.log(a.length, a.every((s) => s === ""));`
@@ -607,6 +611,7 @@ const KEPT_CASES = [
607611
["`new.target`", `!function () { ${TRY} console.log(new.target); }();`],
608612
["an arrow reading `this`", `!function () { ${TRY} [1].map(() => this); }();`],
609613
["an arrow calling `eval`", `!function () { ${TRY} [1].map(() => eval("this")); }();`],
614+
["a value kept before a global no built-in names, a member a built-in computes, a shadowed built-in, or a built-in the value writes", 'function f(Math) { var v = g(); h(v); var w = g(); console.log(RegExp.$1, w); var x = g(); console.log(Math.max(x)); } function k() { var o = console; var u = (console = { log: function () { o.log("new"); } }, 1); console.log(u); console = o; } var h = console.log; function g() { return 2; } f({ max: String }); k();'],
610615
["a parameter given an argument", `!function (a) { ${TRY} console.log(a); }(Math.random());`],
611616
["a parameter given nothing", `!function (a) { ${TRY} console.log(a, a = Math.random()); }();`],
612617
["a name", `!function f() { ${TRY} console.log(f); }();`],
@@ -5047,7 +5052,7 @@ describe("syntax-printer", () => {
50475052
["function f(c) { return null == c ? void 0 : c?.a; } console.log(f(null), f({ a: 1 }));", target(2021)],
50485053
["var o = { p: 0, q: 1 }; function f(k) { o[k + 1] || (o[k + 1] = 2); return o; } console.log(f(\"p\"), f(\"q\"));", target(2021)],
50495054
["var g = function f() { \"use strict\"; try { f = f || 1; } catch (e) { return e.name; } return typeof f; }; console.log(g());", target(2021)],
5050-
["x = x || 1; var x; console.log(x);", target(2021)],
5055+
["x = x || 1; var x; print(x); function print(v) { console.log(v); }", target(2021)],
50515056
["console.log(function () { var other = {}, o = { get p() { o = other; return 0; }, set p(v) {} }, first = o; o.p || (o.p = 1); return [first === o, other.p]; }());", target(2021)],
50525057
["var n = 0; Object.defineProperty(globalThis, \"a\", { get: function () { return n++ ? null : { p: 1 }; }, configurable: true }); function f() { return null == a ? void 0 : a.p; } try { console.log(f()); } catch (e) { console.log(e.name); }", target(2021)],
50535058
["function f(o, c) { with (o) { return null == c ? void 0 : c.p; } } console.log(f({}, null), f({ c: { p: 2 } }, { p: 1 }));", target(2021)],

0 commit comments

Comments
 (0)