Skip to content

Commit 8050699

Browse files
feat(minify): close oxc gaps, and keep delete (null ?? x) and (null ?? o.f)() (#22878)
* feat(minify): close oxc gaps, and keep `delete (null ?? x)` and `(null ?? o.f)()` Drop an unused catch binding from ECMAScript 2019 on, join the `if` the tail return leaves in another, dot a key past ASCII, drop what a loop's update does not run, and read a constructor's `this` as an object. Fix `??` folding to a bare reference where `delete` or a call reads its binding. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * test: retire next/66237 from the corpus files oxc minifies smaller Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * fix(minify): keep a constructor's `this.x` read a getter may answer Reading `this` in a constructor as never throwing let an unused `this.x` drop, losing a getter's effect; terser keeps it too. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 82501c3 commit 8050699

4 files changed

Lines changed: 132 additions & 14 deletions

File tree

‎.changeset/065-minify-oxc-gaps.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Fix `delete (null ?? x)`, and drop unused catch bindings when minifying.

‎lib/javascript/syntax-printer.js‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29106,10 +29106,39 @@ const installImprove = (modules) => {
2910629106
A.wrapOptimizer("Try", (optimizeTry) => (self, compressor) => {
2910729107
if (improvements.enabled && self.handler) {
2910829108
dropCatchRedeclarations(self.handler, compressor);
29109+
dropCatchBinding(self.handler, compressor);
2910929110
}
2911029111
return optimizeTry(self, compressor);
2911129112
});
2911229113

29114+
/**
29115+
* `catch (e) {}` as `catch {}` from ECMAScript 2019 on, where nothing in the
29116+
* clause names `e`: no read, no `var e` an Annex B write would reach.
29117+
* @param {Node} handler a catch clause
29118+
* @param {Compressor} compressor the compressor
29119+
* @returns {void}
29120+
*/
29121+
const dropCatchBinding = (handler, compressor) => {
29122+
const { param } = handler;
29123+
if (
29124+
!param ||
29125+
!A.isSymbolCatchNode(param) ||
29126+
compressor.option("ecma") < 2019
29127+
) {
29128+
return;
29129+
}
29130+
const named = A.walk(
29131+
/** @type {Node} */ (handler.body),
29132+
(/** @type {Node} */ node) =>
29133+
(A.isSymbolNode(node) &&
29134+
(node.name === param.name || node.name === "eval")) ||
29135+
A.isWithNode(node)
29136+
? A.walk_abort
29137+
: undefined
29138+
);
29139+
if (!named) handler.param = NO_NODE;
29140+
};
29141+
2911329142
/**
2911429143
* The `typeof` an equality with a string reads, and that string, in either
2911529144
* order, as terser leaves either.
@@ -32937,11 +32966,41 @@ const installImprove = (modules) => {
3293732966
return (
3293832967
(improvements.enabled &&
3293932968
A.isSubNode(optimized) &&
32940-
elementRead(optimized, compressor)) ||
32969+
(elementRead(optimized, compressor) ||
32970+
wideNameRead(optimized, compressor))) ||
3294132971
optimized
3294232972
);
3294332973
});
3294432974

32975+
const unicode = createUnicode();
32976+
/**
32977+
* `a["ÑStuff"]` as `a.ÑStuff`: a name past ASCII, which terser leaves in
32978+
* brackets, prints as a name from ECMAScript 2015 on.
32979+
* @param {Node} self an indexed read
32980+
* @param {Compressor} compressor the compressor
32981+
* @returns {Node | undefined} the dotted read, where one fits
32982+
*/
32983+
const wideNameRead = (self, compressor) => {
32984+
const { property } = self;
32985+
if (
32986+
!compressor.option("properties") ||
32987+
compressor.option("ecma") < 2015 ||
32988+
!A.isStringNode(property)
32989+
) {
32990+
return undefined;
32991+
}
32992+
const name = /** @type {string} */ (property.value);
32993+
if (isPlainKey(name) || !unicode.isIdentifierString(name, true)) {
32994+
return undefined;
32995+
}
32996+
return makeNode(A.DotNode, self, {
32997+
object: self.object,
32998+
optional: self.optional,
32999+
property: A.SymbolPropertyNode({ name }),
33000+
quote: property.quote
33001+
});
33002+
};
33003+
3294533004
/**
3294633005
* Whether a function writes a binding, or the `arguments` object is written,
3294733006
* replaced or handed on, where it may come to differ from a parameter.
@@ -44789,14 +44848,24 @@ const installOperatorOptimizers = (modules, helpers) => {
4478944848
compressor
4479044849
);
4479144850
}
44851+
// `delete (null ?? x)` deletes nothing and `(null ?? o.f)()` calls `f`
44852+
// without `this`, where `delete x` and `o.f()` would not.
44853+
const right =
44854+
modules.corrections !== undefined && modules.corrections.enabled
44855+
? maintainThisBinding(
44856+
compressor.parent(),
44857+
compressor.self(),
44858+
self.right
44859+
)
44860+
: self.right;
4479244861
if (isNullish(self.left, compressor)) {
44793-
return self.right;
44862+
return right;
4479444863
}
4479544864

4479644865
const leftValue = ast.evaluate(self.left, compressor);
4479744866
if (!A.isSyntaxNode(leftValue)) {
4479844867
return leftValue === null || leftValue === undefined
44799-
? self.right
44868+
? right
4480044869
: self.left;
4480144870
}
4480244871

‎test/helpers/oxcSmaller.js‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,8 @@ const OXC_SMALLER_BY_REASON = [
237237
"oxc minifier: peephole/esbuild.rs:1181",
238238
"oxc minifier: peephole/esbuild.rs:1215",
239239
"oxc minifier: peephole/esbuild.rs:1222",
240-
"oxc minifier: peephole/esbuild.rs:1598",
241240
"oxc minifier: peephole/esbuild.rs:1798",
242241
"oxc minifier: peephole/esbuild.rs:2309",
243-
"oxc minifier: peephole/esbuild.rs:2368",
244242
"oxc minifier: peephole/esbuild.rs:255"
245243
]
246244
],
@@ -303,7 +301,6 @@ const OXC_SMALLER_BY_REASON = [
303301
"oxc drops what nothing reads or what has no effect: unused variables, functions, parameters and pure expressions",
304302
[
305303
"swc minifier: fixture/collapse-vars/trivial-literal-chain/input.js",
306-
"swc minifier: fixture/issues/9785/input.js",
307304
"terser compress: asm/asm_function_expression",
308305
"terser compress: asm/asm_nested_functions",
309306
"terser compress: asm/asm_toplevel",
@@ -356,7 +353,6 @@ const OXC_SMALLER_BY_REASON = [
356353
"real-world code, where the gap sums several of the transforms above; split it once those land",
357354
[
358355
"swc minifier: fixture/issues/d3-color/1/input.js",
359-
"swc minifier: fixture/next/66237/input.js",
360356
"swc minifier: fixture/next/asmjs/1/input.js",
361357
"swc minifier: fixture/projects/jquery/11/input.js",
362358
"swc minifier: projects/files/backbone-1.1.0.js",
@@ -569,13 +565,6 @@ const OXC_SMALLER_BY_REASON = [
569565
"oxc folds more constant expressions, as oxc's `peephole/fold_constants.rs` tests show",
570566
["oxc minifier: peephole/fold_constants.rs:521"]
571567
],
572-
[
573-
'oxc writes more `a["b"]` as `a.b`, as oxc\'s `peephole/convert_to_dotted_properties.rs` tests show',
574-
[
575-
"oxc minifier: peephole/convert_to_dotted_properties.rs:24",
576-
"oxc minifier: peephole/convert_to_dotted_properties.rs:26"
577-
]
578-
],
579568
[
580569
"oxc aborts: its code generator panics on this source (`crates/oxc_codegen/src/str.rs:712`, a slice out of range)",
581570
["swc minifier: fixture/issues/string-index-utf16/input.js"]

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9109,6 +9109,61 @@ describe("syntax-printer", () => {
91099109
}
91109110
});
91119111

9112+
it("should write what oxc writes shorter: a catch binding, a nested `if`, a wide name and a loop update", async () => {
9113+
const { minify } = await load();
9114+
for (const [input, expected] of [
9115+
["try { f(); } catch (e) { g(); }", "catch{g()}"],
9116+
["function _() { if (a) return; if (b) return c; } _();", "if(!a&&b)return c"],
9117+
['a["ÑStuffAfter"];', "a.ÑStuffAfter"],
9118+
["for (var i = 0; i < 10; i++, 0) foo(i);", "i<10;i++)"]
9119+
]) {
9120+
const { code } = await minify(input, {
9121+
compress: { passes: 2 },
9122+
mangle: false,
9123+
ecma: 2020
9124+
});
9125+
expect(code).toContain(expected);
9126+
}
9127+
for (const [input, unexpected, options] of [
9128+
["try { f(); } catch (e) { g(e); }", "catch{", { ecma: 2020 }],
9129+
["try { f(); } catch (e) { g(); }", "catch{", { ecma: 2015 }],
9130+
["try { f(); } catch (e) { var e = 1; }", "catch{", { ecma: 2020 }],
9131+
["try { f(); } catch (e) { eval('g(e)'); }", "catch{", { ecma: 2020 }],
9132+
['a["ÑStuffAfter"];', "a.Ñ", { ecma: 5 }]
9133+
]) {
9134+
const { code } = await minify(/** @type {string} */ (input), {
9135+
compress: { passes: 2 },
9136+
mangle: false,
9137+
.../** @type {object} */ (options)
9138+
});
9139+
expect(code).not.toContain(unexpected);
9140+
}
9141+
});
9142+
9143+
it("should keep a constructor's `this.x` read a getter may answer", async () => {
9144+
const { minify } = await load();
9145+
for (const input of [
9146+
'class A { get x() { console.log("get"); return 1; } constructor() { this.x; } } new A();',
9147+
'class A { get x() { console.log("get"); return 1; } constructor() { for (var i = 0; i < 2; i++, this.x); } } new A();',
9148+
'class B { get x() { console.log("get"); return 1; } } class A extends B { constructor() { super(); this.x; } } new A();'
9149+
]) {
9150+
const { code } = await minify(input, { compress: {}, mangle: false });
9151+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
9152+
}
9153+
});
9154+
9155+
it("should keep what `delete` and a call read through `??` once its left side is nullish", async () => {
9156+
const { minify } = await load();
9157+
for (const input of [
9158+
"var x = 1; console.log(delete (null ?? x), x);",
9159+
"x = 1; console.log(delete (null ?? x), typeof x);",
9160+
"var o = { f() { return this === o; } }; console.log((null ?? o.f)(), (void 0 ?? o.f)());"
9161+
]) {
9162+
const { code } = await minify(input, { compress: {}, mangle: false });
9163+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
9164+
}
9165+
});
9166+
91129167
it("should keep a global written through `globalThis` and a `slice` a patched `Array.prototype` changes", async () => {
91139168
const { minify } = await load();
91149169
for (const input of [

0 commit comments

Comments
 (0)