Skip to content

Commit cf23f8f

Browse files
feat(javascript): write x ? y : x as x && y in the minifier
What terser does for `x ? x : y` and `a ? b : (c, b)`, mirrored: `x ? y : x` is written `x && y` and `a ? (c, b) : b` as `(a && c), b`. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent a067e66 commit cf23f8f

5 files changed

Lines changed: 63 additions & 14 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16929,6 +16929,54 @@ const installImprove = (modules) => {
1692916929
: loose;
1693016930
};
1693116931

16932+
// What terser does for `x ? x : y` and `a ? b : (c, b)`, mirrored.
16933+
A.wrapOptimizer(
16934+
"Conditional",
16935+
(optimizeConditional) => (self, compressor) => {
16936+
const optimized = optimizeConditional(self, compressor);
16937+
if (!improvements.enabled || !A.isConditionalNode(optimized)) {
16938+
return optimized;
16939+
}
16940+
const { test, consequent, alternate } = optimized;
16941+
// x ? y : x -> x && y
16942+
if (
16943+
A.isSymbolRefNode(test) &&
16944+
A.isSymbolRefNode(alternate) &&
16945+
test.definition === alternate.definition
16946+
) {
16947+
return makeNode(A.BinaryNode, optimized, {
16948+
operator: "&&",
16949+
left: test,
16950+
right: consequent
16951+
});
16952+
}
16953+
// a ? (c, b) : b -> (a && c), b
16954+
if (
16955+
A.isSequenceNode(consequent) &&
16956+
A.isEquivalent(
16957+
alternate,
16958+
consequent.expressions[consequent.expressions.length - 1]
16959+
)
16960+
) {
16961+
return A.optimize(
16962+
makeSequence(optimized, [
16963+
makeNode(A.BinaryNode, optimized, {
16964+
operator: "&&",
16965+
left: test,
16966+
right: makeSequence(
16967+
optimized,
16968+
consequent.expressions.slice(0, -1)
16969+
)
16970+
}),
16971+
alternate
16972+
]),
16973+
compressor
16974+
);
16975+
}
16976+
return optimized;
16977+
}
16978+
);
16979+
1693216980
A.wrapOptimizer(
1693316981
"Conditional",
1693416982
(optimizeConditional) => (self, compressor) => {

‎test/helpers/oxcSmaller.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ const OXC_SMALLER_BY_REASON = [
6161
"oxc minifier: peephole/esbuild.rs:287",
6262
"oxc minifier: peephole/esbuild.rs:288",
6363
"oxc minifier: peephole/esbuild.rs:494",
64-
"oxc minifier: peephole/esbuild.rs:520",
6564
"oxc minifier: peephole/esbuild.rs:596",
66-
"oxc minifier: peephole/esbuild.rs:598",
6765
"oxc minifier: peephole/esbuild.rs:599",
6866
"oxc minifier: peephole/esbuild.rs:600",
69-
"oxc minifier: peephole/esbuild.rs:617",
7067
"oxc minifier: peephole/esbuild.rs:628",
7168
"oxc minifier: peephole/esbuild.rs:629",
7269
"oxc minifier: peephole/esbuild.rs:632",
@@ -815,7 +812,6 @@ const OXC_SMALLER_BY_REASON = [
815812
"oxc minifier: peephole/remove_unused_expression.rs:531",
816813
"oxc minifier: peephole/remove_unused_expression.rs:535",
817814
"oxc minifier: peephole/remove_unused_expression.rs:539",
818-
"oxc minifier: peephole/remove_unused_expression.rs:545",
819815
"oxc minifier: peephole/remove_unused_expression.rs:560",
820816
"oxc minifier: peephole/remove_unused_expression.rs:587",
821817
"oxc minifier: peephole/remove_unused_expression.rs:589",
@@ -940,7 +936,6 @@ const OXC_SMALLER_BY_REASON = [
940936
"swc minifier: fixture/projects/backbone/7/input.js",
941937
"swc minifier: fixture/projects/jquery/18/input.js",
942938
"swc minifier: fixture/projects/jquery/7/input.js",
943-
"swc minifier: fixture/projects/mootools/10/input.js",
944939
"swc minifier: fixture/projects/mootools/6/input.js",
945940
"swc minifier: fixture/projects/mootools/8/input.js",
946941
"swc minifier: fixture/projects/react/15/input.js",
@@ -1288,22 +1283,17 @@ const OXC_SMALLER_BY_REASON = [
12881283
[
12891284
"oxc writes conditional expressions shorter, as oxc's `peephole/minimize_conditional_expression.rs` tests show",
12901285
[
1291-
"oxc minifier: peephole/minimize_conditional_expression.rs:104",
12921286
"oxc minifier: peephole/minimize_conditional_expression.rs:112",
12931287
"oxc minifier: peephole/minimize_conditional_expression.rs:115",
12941288
"oxc minifier: peephole/minimize_conditional_expression.rs:121",
12951289
"oxc minifier: peephole/minimize_conditional_expression.rs:122",
12961290
"oxc minifier: peephole/minimize_conditional_expression.rs:181",
12971291
"oxc minifier: peephole/minimize_conditional_expression.rs:182",
1298-
"oxc minifier: peephole/minimize_conditional_expression.rs:44",
1299-
"oxc minifier: peephole/minimize_conditional_expression.rs:48",
13001292
"oxc minifier: peephole/minimize_conditional_expression.rs:55",
13011293
"oxc minifier: peephole/minimize_conditional_expression.rs:72",
13021294
"oxc minifier: peephole/minimize_conditional_expression.rs:73",
13031295
"oxc minifier: peephole/minimize_conditional_expression.rs:80",
1304-
"oxc minifier: peephole/minimize_conditional_expression.rs:84",
1305-
"oxc minifier: peephole/minimize_conditional_expression.rs:97",
1306-
"oxc minifier: peephole/minimize_conditional_expression.rs:98"
1296+
"oxc minifier: peephole/minimize_conditional_expression.rs:84"
13071297
]
13081298
],
13091299
[

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,6 @@ const SWC_SMALLER_BY_REASON = [
466466
"swc minifier: fixture/issues/do-while-false-control-flow/input.js",
467467
"swc minifier: fixture/issues/do-while-false-terminal-jump/rejected/input.js",
468468
"swc minifier: fixture/issues/do-while-false-terminal-jump/scope-directives/input.js",
469-
"swc minifier: fixture/projects/backbone/2/input.js",
470-
"swc minifier: fixture/projects/backbone/5/input.js",
471-
"swc minifier: fixture/projects/mootools/10/input.js",
472469
"swc minifier: fixture/projects/mootools/8/input.js",
473470
"swc minifier: fixture/simple/switch/const/call/input.js",
474471
"swc minifier: fixture/simple/switch/const/order/input.js",

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,14 @@ const IMPROVED_YET_BIGGER = {
14141414
// Sources whose shorter spelling no longer repeats text gzip matched, by the
14151415
// spelling written; an entry that stops compressing worse fails until retired.
14161416
for (const [reason, keys] of /** @type {[string, string[]][]} */ ([
1417+
[
1418+
"`c ? x : c` written as `c && x` where the source repeats the conditional gzip matched",
1419+
[
1420+
"bound-to-collator-instance.js (the default minimizer's options)",
1421+
"returns-same-results-as-NumberFormat.js (the default minimizer's options)",
1422+
"short-circuit-compound-assignment.js (the default minimizer's options)"
1423+
]
1424+
],
14171425
[
14181426
"`Array` written as a literal where the source writes the call again",
14191427
[

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ const IMPROVED_CASES = [
266266
`(() => { let a = console.log.name; console.log(a, a); })(); (() => { ${TRY} console.log(2); })();`,
267267
{ compress: { passes: 2 }, mangle: true }
268268
],
269+
[
270+
"a conditional whose other branch is its test, or ends as its other branch",
271+
"function f(a, b, c) { var r = []; return [a ? b : a, a ? (r.push(1), c) : c, a ? (r.push(2), r.push(3), c) : c, r.join()]; } console.log(f(0, 1, 2), f(3, 4, 5));",
272+
{ compress: {}, mangle: false }
273+
],
269274
[
270275
"a nested function reading its own `this`",
271276
`!function () { ${TRY} console.log([1].map(function () { return typeof this; })[0]); }();`,
@@ -572,6 +577,7 @@ const IMPROVED_CASES = [
572577
// of its function's own, or the call passes, keeps or constructs something.
573578
/** @type {[string, string][]} */
574579
const KEPT_CASES = [
580+
["a conditional whose other branch reads a getter twice or a different binding", "var n = 0, o = { get x() { return ++n; } }; function f(b) { var a = 1; { let a = 0; var r = a ? b : o.x ? b : o.x; } return [r, a, n]; } console.log(f(2));"],
575581
["a bigint and a number concatenated, which throws", "try { console.log(1n + 2); } catch (e) { console.log(e.name); }"],
576582
["an array literal joining to a string longer than it", 'console.log([!0, !0, !0] + "");'],
577583
["`+` and `~` of what a literal does not give", "function f(a) { return [+[a], ~a !== 0 ? 1 : 2]; } console.log(f(1), f(1n));"],

0 commit comments

Comments
 (0)