Skip to content

Commit 88c6ac7

Browse files
fix(es/minifier): Remove unused arrow functions in dead code elimination (#11319)
## Description This PR fixes issue #11303 where the minifier was preserving unused arrow functions. ## Root Cause The `ignore_return_value` method in the optimizer handled function expressions (Expr::Fn) by returning None (indicating no side effects), but did not handle arrow functions (Expr::Arrow) the same way. Arrow functions would fall through to the default case and be preserved even when unused. ## Changes - Added handling for Expr::Arrow in ignore_return_value method - Arrow functions are now treated as side-effect-free expressions - Added test case for issue #11303 - Updated tinymce test expectation to reflect more aggressive optimization ## Test Results All 2790 tests pass. Closes #11303 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Donny/강동윤 <kdy1@users.noreply.github.com>
1 parent b0d3154 commit 88c6ac7

File tree

49 files changed

+83
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+83
-64
lines changed

.changeset/stupid-worms-boil.md

Lines changed: 6 additions & 0 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [parserArrowFunctionExpression10.ts]
22
//// [fileJs.js]
3-
a || ((e)=>f);
3+
a;
44
//// [fileTs.ts]
5-
a || ((e)=>f);
5+
a;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [parserArrowFunctionExpression12.ts]
22
//// [fileJs.js]
3-
a || ((d)=>e);
3+
a;
44
//// [fileTs.ts]
5-
a || ((d)=>e);
5+
a;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [parserArrowFunctionExpression13.ts]
22
//// [fileJs.js]
3-
a || (()=>null);
3+
a;
44
//// [fileTs.ts]
5-
a || (()=>null);
5+
a;
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
//// [parserArrowFunctionExpression8.ts]
22
//// [fileJs.js]
3-
x || ((z)=>({
4-
z
5-
}));
3+
x;
64
//// [fileTs.ts]
7-
x || ((z)=>({
8-
z
9-
}));
5+
x;

crates/swc_ecma_minifier/src/compress/optimize/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,16 @@ impl Optimizer<'_> {
658658
return None;
659659
}
660660

661+
// Arrow function expression cannot have a side effect.
662+
Expr::Arrow(_) => {
663+
report_change!(
664+
"ignore_return_value: Dropping unused arrow expr as it does not have any side \
665+
effect"
666+
);
667+
self.changed = true;
668+
return None;
669+
}
670+
661671
Expr::Class(cls) => {
662672
// Do not remove class if it's self-referencing
663673
if let Some(id) = &cls.ident {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class X {
2+
constructor() {
3+
4+
}
5+
}
6+
7+
class Y extends X {
8+
9+
}
10+
11+
const t = (a) => (b => {
12+
if (a.foo()) throw Error();
13+
return a;
14+
}), y = t(new Y);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class X {
2+
}
3+
new class extends X {
4+
};
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
1-
Yp.revoke, (a)=>Yp.config({
2-
aria: {
3-
mode: "checked"
4-
},
5-
...ge(a, (e, t)=>"exclusive" !== t),
6-
onToggled: (c, d)=>{
7-
p(a.onToggled) && a.onToggled(c, d), Or(c, th, {
8-
item: c,
9-
state: d
10-
});
11-
}
12-
});
1+
Yp.revoke;

packages/core/scripts/npm/darwin-arm64/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
"bugs": {
4040
"url": "https://github.com/swc-project/swc/issues"
4141
}
42-
}
42+
}

0 commit comments

Comments
 (0)