Skip to content

Commit a42343f

Browse files
feat(javascript): write x ? y : x as x && y in the minifier (#22502)
* 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 * test(javascript): retire an swc exclusion `main` no longer needs `fixture/issues/11684/class-decl` is no bigger than swc's output since the latest minifier changes on `main`. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * fix(javascript): keep a conditional whose branches differ in their optional links `isEquivalent` reads `a?.b.c` and `a.b?.c` as one, as terser's `equivalent_to` does, so `f ? (g(), a?.b.c) : a.b?.c` became a read of `a.b?.c` that throws where `a` is nullish. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent f68f3af commit a42343f

5 files changed

Lines changed: 90 additions & 10 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19060,6 +19060,79 @@ const installImprove = (modules) => {
1906019060
: loose;
1906119061
};
1906219062

19063+
/**
19064+
* Which links of a tree are optional, in walk order: `isEquivalent` reads
19065+
* `a?.b.c` and `a.b?.c` as one, as terser's `equivalent_to` does.
19066+
* @param {Node} node a tree
19067+
* @returns {string} one `?` or `.` per call or property access
19068+
*/
19069+
const optionalLinks = (node) => {
19070+
let links = "";
19071+
walkNode(
19072+
node,
19073+
A.createWalker((/** @type {Node} */ child) => {
19074+
if (A.isCallNode(child) || A.isPropAccessNode(child)) {
19075+
links += child.optional ? "?" : ".";
19076+
}
19077+
})
19078+
);
19079+
return links;
19080+
};
19081+
19082+
// What terser does for `x ? x : y` and `a ? b : (c, b)`, mirrored.
19083+
A.wrapOptimizer(
19084+
"Conditional",
19085+
(optimizeConditional) => (self, compressor) => {
19086+
const optimized = optimizeConditional(self, compressor);
19087+
if (!improvements.enabled || !A.isConditionalNode(optimized)) {
19088+
return optimized;
19089+
}
19090+
const { test, consequent, alternate } = optimized;
19091+
// x ? y : x -> x && y
19092+
if (
19093+
A.isSymbolRefNode(test) &&
19094+
A.isSymbolRefNode(alternate) &&
19095+
test.definition === alternate.definition &&
19096+
// A global may be a getter, answering each read differently.
19097+
readsAlike(test, compressor)
19098+
) {
19099+
return makeNode(A.BinaryNode, optimized, {
19100+
operator: "&&",
19101+
left: test,
19102+
right: consequent
19103+
});
19104+
}
19105+
// a ? (c, b) : b -> (a && c), b
19106+
if (
19107+
A.isSequenceNode(consequent) &&
19108+
A.isEquivalent(
19109+
alternate,
19110+
consequent.expressions[consequent.expressions.length - 1]
19111+
) &&
19112+
optionalLinks(alternate) ===
19113+
optionalLinks(
19114+
consequent.expressions[consequent.expressions.length - 1]
19115+
)
19116+
) {
19117+
return A.optimize(
19118+
makeSequence(optimized, [
19119+
makeNode(A.BinaryNode, optimized, {
19120+
operator: "&&",
19121+
left: test,
19122+
right: makeSequence(
19123+
optimized,
19124+
consequent.expressions.slice(0, -1)
19125+
)
19126+
}),
19127+
alternate
19128+
]),
19129+
compressor
19130+
);
19131+
}
19132+
return optimized;
19133+
}
19134+
);
19135+
1906319136
A.wrapOptimizer(
1906419137
"Conditional",
1906519138
(optimizeConditional) => (self, compressor) => {

‎test/helpers/oxcSmaller.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ const OXC_SMALLER_BY_REASON = [
5454
"oxc minifier: peephole/esbuild.rs:598",
5555
"oxc minifier: peephole/esbuild.rs:599",
5656
"oxc minifier: peephole/esbuild.rs:600",
57-
"oxc minifier: peephole/esbuild.rs:617",
5857
"oxc minifier: peephole/esbuild.rs:628",
5958
"oxc minifier: peephole/esbuild.rs:629",
6059
"oxc minifier: peephole/esbuild.rs:632",
@@ -757,7 +756,6 @@ const OXC_SMALLER_BY_REASON = [
757756
"oxc minifier: peephole/remove_unused_expression.rs:531",
758757
"oxc minifier: peephole/remove_unused_expression.rs:535",
759758
"oxc minifier: peephole/remove_unused_expression.rs:539",
760-
"oxc minifier: peephole/remove_unused_expression.rs:545",
761759
"oxc minifier: peephole/remove_unused_expression.rs:560",
762760
"oxc minifier: peephole/remove_unused_expression.rs:587",
763761
"oxc minifier: peephole/remove_unused_expression.rs:589",
@@ -880,7 +878,6 @@ const OXC_SMALLER_BY_REASON = [
880878
"swc minifier: fixture/projects/backbone/7/input.js",
881879
"swc minifier: fixture/projects/jquery/18/input.js",
882880
"swc minifier: fixture/projects/jquery/7/input.js",
883-
"swc minifier: fixture/projects/mootools/10/input.js",
884881
"swc minifier: fixture/projects/mootools/6/input.js",
885882
"swc minifier: fixture/projects/mootools/8/input.js",
886883
"swc minifier: fixture/projects/react/15/input.js",
@@ -1217,14 +1214,11 @@ const OXC_SMALLER_BY_REASON = [
12171214
"oxc minifier: peephole/minimize_conditional_expression.rs:181",
12181215
"oxc minifier: peephole/minimize_conditional_expression.rs:182",
12191216
"oxc minifier: peephole/minimize_conditional_expression.rs:44",
1220-
"oxc minifier: peephole/minimize_conditional_expression.rs:48",
12211217
"oxc minifier: peephole/minimize_conditional_expression.rs:55",
12221218
"oxc minifier: peephole/minimize_conditional_expression.rs:72",
12231219
"oxc minifier: peephole/minimize_conditional_expression.rs:73",
12241220
"oxc minifier: peephole/minimize_conditional_expression.rs:80",
1225-
"oxc minifier: peephole/minimize_conditional_expression.rs:84",
1226-
"oxc minifier: peephole/minimize_conditional_expression.rs:97",
1227-
"oxc minifier: peephole/minimize_conditional_expression.rs:98"
1221+
"oxc minifier: peephole/minimize_conditional_expression.rs:84"
12281222
]
12291223
],
12301224
[

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,6 @@ const SWC_SMALLER_BY_REASON = [
452452
"swc minifier: fixture/issues/do-while-false-control-flow/input.js",
453453
"swc minifier: fixture/issues/do-while-false-terminal-jump/rejected/input.js",
454454
"swc minifier: fixture/issues/do-while-false-terminal-jump/scope-directives/input.js",
455-
"swc minifier: fixture/projects/backbone/2/input.js",
456-
"swc minifier: fixture/projects/backbone/5/input.js",
457-
"swc minifier: fixture/projects/mootools/10/input.js",
458455
"swc minifier: fixture/projects/mootools/8/input.js",
459456
"swc minifier: fixture/simple/switch/const/call/input.js",
460457
"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
@@ -1429,6 +1429,14 @@ for (const [reason, keys] of /** @type {[string, string[]][]} */ ([
14291429
"new/new_statements_3 (the default minimizer's options)"
14301430
]
14311431
],
1432+
[
1433+
"`c ? x : c` written as `c && x` where the source repeats the conditional gzip matched",
1434+
[
1435+
"bound-to-collator-instance.js (the default minimizer's options)",
1436+
"returns-same-results-as-NumberFormat.js (the default minimizer's options)",
1437+
"short-circuit-compound-assignment.js (the default minimizer's options)"
1438+
]
1439+
],
14321440
[
14331441
"`Array` written as a literal where the source writes the call again",
14341442
[

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ const IMPROVED_CASES = [
289289
`(() => { let a = console.log.name; console.log(a, a); })(); (() => { ${TRY} console.log(2); })();`,
290290
{ compress: { passes: 2 }, mangle: true }
291291
],
292+
[
293+
"a conditional whose other branch is its test, or ends as its other branch",
294+
"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));",
295+
{ compress: {}, mangle: false }
296+
],
292297
[
293298
"a nested function reading its own `this`",
294299
`!function () { ${TRY} console.log([1].map(function () { return typeof this; })[0]); }();`,
@@ -676,6 +681,9 @@ const IMPROVED_CASES = [
676681
// of its function's own, or the call passes, keeps or constructs something.
677682
/** @type {[string, string][]} */
678683
const KEPT_CASES = [
684+
["a conditional whose branches end in one chain made optional at different links", "function f(flag, obj, t) { return flag ? (t(), obj?.b.c) : obj.b?.c; } console.log(f(1, { b: { c: 1 } }, () => 0), f(0, {}, () => 0));"],
685+
["a conditional whose other branch reads a global getter again", 'var n = 0; Object.defineProperty(globalThis, "g", { get: function () { return n++; }, configurable: true }); function f(b) { return g ? b : g; } console.log(f(1), f(2));'],
686+
["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));"],
679687
["a bigint and a number concatenated, which throws", "try { console.log(1n + 2); } catch (e) { console.log(e.name); }"],
680688
["an array literal joining to a string longer than it", 'console.log([!0, !0, !0] + "");'],
681689
["`+` 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)