Skip to content

Commit 5d7f942

Browse files
fix(minify): keep a #__PURE__ call whose value is read
The annotation lets a call go only where nothing reads its value, so writing one of a function returning nothing as `void 0` dropped its body. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent ac2ddd6 commit 5d7f942

3 files changed

Lines changed: 7 additions & 53 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24715,53 +24715,6 @@ const installImprove = (modules) => {
2471524715
return found;
2471624716
};
2471724717

24718-
/**
24719-
* Whether a function's body returns no value of its own, so a call of it
24720-
* that comes back returns `undefined`.
24721-
* @param {Node} fn a function or arrow
24722-
* @returns {boolean} true where no `return` of its own passes a value
24723-
*/
24724-
const returnsNothing = (fn) =>
24725-
!A.walk(/** @type {Node} */ (fn.body), (/** @type {Node} */ node) =>
24726-
A.isReturnNode(node) && node.argument
24727-
? A.walk_abort
24728-
: A.isLambdaNode(node) || A.isClassNode(node)
24729-
? true
24730-
: undefined
24731-
);
24732-
// A `#__PURE__` call may be taken never to run, so one of a function that
24733-
// returns nothing reads as `void 0` beside its arguments.
24734-
A.wrapOptimizer("Call", (optimizeCall) => (self, compressor) => {
24735-
const optimized = optimizeCall(self, compressor);
24736-
if (
24737-
!improvements.enabled ||
24738-
!compressor.option("side_effects") ||
24739-
!A.isCallNode(optimized) ||
24740-
A.isNewNode(optimized) ||
24741-
optimized.optional ||
24742-
!(optimized._annotations & A._PURE) ||
24743-
!(
24744-
A.isArrowNode(optimized.callee) || A.isFunctionNode(optimized.callee)
24745-
) ||
24746-
optimized.callee.async ||
24747-
optimized.callee.generator ||
24748-
optimized.arguments.some(A.isExpansionNode) ||
24749-
!returnsNothing(optimized.callee)
24750-
) {
24751-
return optimized;
24752-
}
24753-
const left = optimized.arguments
24754-
.map((argument) => A.dropSideEffectFree(argument, compressor))
24755-
.filter(Boolean);
24756-
const empty = makeNode(A.UnaryPrefixNode, optimized, {
24757-
operator: "void",
24758-
argument: makeNode(A.NumberNode, optimized, { value: 0 })
24759-
});
24760-
return left.length === 0
24761-
? empty
24762-
: makeSequence(optimized, [.../** @type {Node[]} */ (left), empty]);
24763-
});
24764-
2476524718
/**
2476624719
* @param {Node} member a class member
2476724720
* @returns {boolean} whether it is a private method, getter or setter

‎test/helpers/oxcSmaller.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ const OXC_SMALLER_BY_REASON = [
514514
"oxc's mangler picks shorter or better-compressing names, as oxc's `mangler/property_mangler.rs` tests show",
515515
["oxc minifier: mangler/property_mangler.rs:99"]
516516
],
517+
[
518+
"oxc drops more unused declarations, as oxc's `peephole/remove_unused_declaration.rs` tests show",
519+
["oxc minifier: peephole/remove_unused_declaration.rs:181"]
520+
],
517521
[
518522
"oxc's output differs: it drops a class whose static member's computed key may be `\"prototype\"`, which throws a TypeError as the class is defined",
519523
[

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9313,16 +9313,11 @@ describe("syntax-printer", () => {
93139313
expect(switched.code).toMatch(/^c,(b===2|2===b)&&/);
93149314
});
93159315

9316-
it("should drop a self-naming call running nothing, an unread `[] = arguments` and `??` after a fixed `let`, and write a `#__PURE__` call returning nothing as `void 0`", async () => {
9316+
it("should drop a self-naming call running nothing, an unread `[] = arguments` and `??` after a fixed `let`, and keep a `#__PURE__` call whose value is read", async () => {
93179317
const { minify } = await load();
93189318
const options = { compress: { passes: 2 }, mangle: false };
93199319
for (const [input, expected, module] of [
93209320
["(function foo() { return foo; })();", "", false],
9321-
[
9322-
"var x = /* @__PURE__ */ (() => { foo(); })();",
9323-
"var x=void 0;",
9324-
false
9325-
],
93269321
["export function f() { var [] = arguments; }", "export function f(){}", true],
93279322
[
93289323
"let s = 'hello'; export function a() { return s ?? other(); } export function b() { return s ?? other(); }",
@@ -9346,6 +9341,8 @@ describe("syntax-printer", () => {
93469341
"console.log((function foo(n) { return n ? foo(n - 1).x.y : {}; })(2));",
93479342
"var a = 1; var x = /* @__PURE__ */ (() => { if (a) return 1; })(); console.log(x);",
93489343
'var x = /* @__PURE__ */ (() => {})(console.log("arg")); console.log(x);',
9344+
'var y = 0; function foo(v) { console.log(v, y); } foo(/* @__PURE__ */ (() => { y = 1; })());',
9345+
'var x = /* @__PURE__ */ (() => { console.log("ran"); })(); console.log(x);',
93499346
'function f() { arguments[Symbol.iterator] = function () { console.log("it"); return [][Symbol.iterator](); }; var [] = arguments; } f();',
93509347
'function f(arguments) { var [] = arguments; } f({ [Symbol.iterator]() { console.log("own"); return [][Symbol.iterator](); } });',
93519348
'console.log(a()); var s = "x"; function a() { return s ?? "fallback"; }',

0 commit comments

Comments
 (0)