Skip to content

Commit 8cd4813

Browse files
authoredFeb 14, 2024
feat(es/minifier): Remove unused parameters of arrow functions (#8636)
**Related issue:** - Closes #8626
1 parent d170d7b commit 8cd4813

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed
 

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,8 @@ impl VisitMut for Optimizer<'_> {
14541454

14551455
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
14561456
fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) {
1457+
self.drop_unused_arrow_params(&mut n.params);
1458+
14571459
let prepend = self.prepend_stmts.take();
14581460

14591461
let ctx = self.ctx;
@@ -1685,9 +1687,7 @@ impl VisitMut for Optimizer<'_> {
16851687
match n {
16861688
DefaultDecl::Class(_) => {}
16871689
DefaultDecl::Fn(f) => {
1688-
if !self.options.keep_fargs && self.options.unused {
1689-
self.drop_unused_params(&mut f.function.params);
1690-
}
1690+
self.drop_unused_params(&mut f.function.params);
16911691
}
16921692
DefaultDecl::TsInterfaceDecl(_) => {}
16931693
}
@@ -1708,9 +1708,7 @@ impl VisitMut for Optimizer<'_> {
17081708
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
17091709
fn visit_mut_export_decl(&mut self, n: &mut ExportDecl) {
17101710
if let Decl::Fn(f) = &mut n.decl {
1711-
if !self.options.keep_fargs && self.options.unused {
1712-
self.drop_unused_params(&mut f.function.params);
1713-
}
1711+
self.drop_unused_params(&mut f.function.params);
17141712
}
17151713

17161714
let ctx = Ctx {
@@ -2027,9 +2025,7 @@ impl VisitMut for Optimizer<'_> {
20272025
.entry(f.ident.to_id())
20282026
.or_insert_with(|| FnMetadata::from(&*f.function));
20292027

2030-
if !self.options.keep_fargs && self.options.unused {
2031-
self.drop_unused_params(&mut f.function.params);
2032-
}
2028+
self.drop_unused_params(&mut f.function.params);
20332029

20342030
let ctx = Ctx {
20352031
top_level: false,

‎crates/swc_ecma_minifier/src/compress/optimize/unused.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,36 @@ impl Optimizer<'_> {
132132

133133
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
134134
pub(super) fn drop_unused_params(&mut self, params: &mut Vec<Param>) {
135+
if self.options.keep_fargs || !self.options.unused {
136+
return;
137+
}
138+
135139
for param in params.iter_mut().rev() {
136140
self.take_pat_if_unused(&mut param.pat, None, false);
137141

138142
if !param.pat.is_invalid() {
139-
return;
143+
break;
144+
}
145+
}
146+
147+
params.retain(|p| !p.pat.is_invalid());
148+
}
149+
150+
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
151+
pub(super) fn drop_unused_arrow_params(&mut self, params: &mut Vec<Pat>) {
152+
if self.options.keep_fargs || !self.options.unused {
153+
return;
154+
}
155+
156+
for param in params.iter_mut().rev() {
157+
self.take_pat_if_unused(param, None, false);
158+
159+
if !param.is_invalid() {
160+
break;
140161
}
141162
}
163+
164+
params.retain(|p| !p.is_invalid());
142165
}
143166

144167
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]

‎crates/swc_ecma_minifier/tests/fixture/issues/3126/1/output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(()=>{
22
var __webpack_modules__ = {
3-
746: (__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__1)=>{
3+
746: ()=>{
44
Object.prototype.hasOwnProperty;
55
}
66
};
77
__webpack_require__.m = __webpack_modules__;
88
(()=>{
9-
__webpack_require__.O = (result, chunkIds, fn, priority)=>{
9+
__webpack_require__.O = (result, chunkIds)=>{
1010
for(var j = 0; j < chunkIds.length; j++)Object.keys(__webpack_require__.O).every((key)=>__webpack_require__.O[key](chunkIds[j]));
1111
};
1212
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"defaults": true,
3+
"keep_fargs": false
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function foo(cb) {
2+
cb();
3+
}
4+
5+
foo((a, b) => true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function foo(cb) {
2+
cb();
3+
}
4+
foo(()=>!0);

0 commit comments

Comments
 (0)
Please sign in to comment.