Skip to content

Commit 6e6a464

Browse files
feat(javascript): let a return of undefined fall off the end inside a function's last if in the minifier (#22538)
A function's last `if` or block now has its own last statement shortened as the function's last `return` already was: `return c ? y : void 0` as `if (c) return y`, and `if (c) return; return x` as `if (!c) return x`. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 02a9f1d commit 6e6a464

4 files changed

Lines changed: 65 additions & 12 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20814,21 +20814,51 @@ const installImprove = (modules) => {
2081420814
A.isNumberNode(node.argument));
2081520815

2081620816
/**
20817-
* A function's last statement, falling off its end to return `undefined`:
20818-
* `return void x` as `x`, `return c ? y : void 0` as `if (c) return y`.
20819-
* @param {Node} lambda a function
20817+
* A list's last statement where falling off it ends the function, returning
20818+
* `undefined`: `return void x` as `x`, `return c ? y : void 0` as `if (c) return y`.
20819+
* @param {Node[]} statements the statements, changed in place
20820+
* @param {boolean} concise whether they are an arrow's whole body
2082020821
* @param {CompressorShape} compressor the compressor
2082120822
* @returns {void}
2082220823
*/
20823-
const shortenTailReturn = (lambda, compressor) => {
20824-
// An async generator awaits a returned value, a tick falling off skips.
20825-
if (lambda.async && lambda.generator) return;
20826-
const statements = lambda.body.body;
20824+
const shortenTail = (statements, concise, compressor) => {
2082720825
const last = statements[statements.length - 1];
20828-
if (!last || !A.isReturnNode(last) || !last.argument) return;
20826+
if (!last) return;
20827+
if (A.isBlockStatementNode(last)) {
20828+
shortenTail(last.body, false, compressor);
20829+
return;
20830+
}
20831+
if (A.isIfNode(last)) {
20832+
for (const branch of ["consequent", "alternate"]) {
20833+
const statement = last[branch];
20834+
if (!statement) continue;
20835+
const list = [statement];
20836+
shortenTail(list, false, compressor);
20837+
last[branch] = list[0];
20838+
}
20839+
return;
20840+
}
20841+
if (!A.isReturnNode(last) || !last.argument) return;
2082920842
const value = last.argument;
20830-
// An arrow of one `return` prints as its value, shorter than an `if`.
20831-
const concise = A.isArrowNode(lambda) && statements.length === 1;
20843+
const previous = statements[statements.length - 2];
20844+
// `if (c) return; return x` falls off where `c` holds.
20845+
if (
20846+
A.isIfNode(previous) &&
20847+
!previous.alternate &&
20848+
A.isReturnNode(previous.consequent) &&
20849+
!previous.consequent.argument
20850+
) {
20851+
statements.splice(
20852+
-2,
20853+
2,
20854+
makeNode(A.IfNode, previous, {
20855+
test: A.negate(previous.test, compressor),
20856+
consequent: last,
20857+
alternate: null
20858+
})
20859+
);
20860+
return;
20861+
}
2083220862
// terser's `return` already drops a `void` of what has no effects.
2083320863
if (A.isUnaryPrefixNode(value) && value.operator === "void") {
2083420864
statements[statements.length - 1] = makeNode(
@@ -20865,6 +20895,24 @@ const installImprove = (modules) => {
2086520895
});
2086620896
};
2086720897

20898+
/**
20899+
* A function's last statements, shortened where they fall off its end.
20900+
* @param {Node} lambda a function
20901+
* @param {CompressorShape} compressor the compressor
20902+
* @returns {void}
20903+
*/
20904+
const shortenTailReturn = (lambda, compressor) => {
20905+
// An async generator awaits a returned value, a tick falling off skips.
20906+
if (lambda.async && lambda.generator) return;
20907+
const statements = lambda.body.body;
20908+
// An arrow of one `return` prints as its value, shorter than an `if`.
20909+
shortenTail(
20910+
statements,
20911+
A.isArrowNode(lambda) && statements.length === 1,
20912+
compressor
20913+
);
20914+
};
20915+
2086820916
/**
2086920917
* What a `return` of `undefined` runs first, `a, b` for `return a, void b`,
2087020918
* as the statement it can run as before a bare `return`.

‎test/helpers/oxcSmaller.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ const OXC_SMALLER_BY_REASON = [
171171
"swc minifier: fixture/pr/11381/input.js",
172172
"swc minifier: fixture/pr/6272/input.js",
173173
"swc minifier: fixture/projects/backbone/16/input.js",
174-
"swc minifier: fixture/projects/backbone/19/input.js",
175174
"swc minifier: fixture/projects/backbone/20/input.js",
176175
"swc minifier: fixture/projects/backbone/6/input.js",
177176
"swc minifier: fixture/projects/jquery/26/input.js",

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ const SWC_SMALLER_BY_REASON = [
385385
"swc minifier: fixture/issues/drop-console-es2015/input.js",
386386
"swc minifier: fixture/issues/object-accessor-function-boundary/input.js",
387387
"swc minifier: fixture/next/43052/input.js",
388-
"swc minifier: fixture/projects/backbone/19/input.js",
389388
"swc minifier: fixture/projects/jquery/24/input.js",
390389
"swc minifier: fixture/projects/mootools/3/input.js",
391390
"swc minifier: fixture/projects/react/13/input.js",

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ const IMPROVED_CASES = [
309309
"var r = []; function f(a, b) { return a ? (r.push(1), r.push(b), r.length) : (r.push(2), r.push(b), r.length); } function g(a) { return [a ? (r.push(3), r.pop(), r.length) : (r.pop(), r.length), a ? (r.pop(), r.length) : (r.push(4), r.pop(), r.length)]; } console.log(f(1, 5), f(0, 6), g(1), g(0), r.join());",
310310
{ compress: {}, mangle: false }
311311
],
312+
[
313+
"a `return` of `undefined` in an `if` ending a function, and `if (c) return; return x` there, falling off after an `if`",
314+
'function f(a, b) { if (a) { var h = b.p; return h && h.q ? h : void 0; } } function g(a, b) { if (a) { for (; !b.n();); if (b.x) return; return b.y; } } function k(a, b) { if (a) { for (; !b.n();); b.m(); } else { for (; !b.n();); b.m(); } } var o = { p: { q: 1 }, n: () => 1, m() { console.log("m"); }, x: 0, y: 2 }; k(1, o); console.log(f(1, o), f(0, o), g(1, o), g(0, o));',
315+
{ compress: {}, mangle: false },
316+
'function f(a,b){if(a){var h=b.p;if(h&&h.q)return h}}function g(a,b){if(a){for(;!b.n(););if(!b.x)return b.y}}function k(a,b){if(a){for(;!b.n(););b.m()}else{for(;!b.n(););b.m()}}var o={p:{q:1},n:()=>1,m(){console.log("m")},x:0,y:2};k(1,o),console.log(f(1,o),f(0,o),g(1,o),g(0,o));'
317+
],
312318
[
313319
"a function's last `return void` of a call, which terser leaves with its defaults off",
314320
"var r = []; function f(a) { r.push(a); return void r.push(a + 1); } f(1); console.log(r.join(), f(2));",
@@ -852,6 +858,7 @@ const KEPT_CASES = [
852858
["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));'],
853859
["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));"],
854860
["a bigint and a number concatenated, which throws", "try { console.log(1n + 2); } catch (e) { console.log(e.name); }"],
861+
["a `return` of `undefined` in an `if` an async generator ends with, or in a loop, a `try` or an `if` something follows", "async function* a(c) { if (c) { await 0; return c.x ? 1 : void 0; } } function b(c) { for (var i of c) { g(); return i ? 1 : void 0; } } function d(c) { try { g(); return c ? 1 : void 0; } finally { g(); } } function e(c, x) { if (c) { g(); return x ? 1 : void 0; } g(); } function g() {} console.log(b([1]), d(1), e(1, 0)); a({ x: 1 }).next().then((r) => console.log(r.value));"],
855862
["consecutive `if`s leaving with different values or to different labels", 'function f(a, b) { if (a) return 1; if (b) return 2; x: for (;;) { for (;;) { if (a) break x; if (b) break; console.log(3); break x; } console.log(4); break; } for (var i = 0; i < 2; i++) console.log(i); } f(1, 0); f(0, 1); f(0, 0);'],
856863
["consecutive `if`s leaving by a bare `return`, which terser merges itself", 'function f(a, b) { if (a) return; if (b) return; console.log("fall through"); } f(1, 0); f(0, 1); f(0, 0);'],
857864
["consecutive `if`s alike whose branch does not always leave", 'function f(a, b) { for (var i = 0; i < 3; i++) { if (a === i) try { g(i); } finally { console.log(i); } if (b === i) try { g(i); } finally { console.log(i); } console.log(i); } } function g(i) { console.log("g", i); } f(0, 1); f(1, 1);'],

0 commit comments

Comments
 (0)