diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index 3226a31f811..f9e1a714691 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -1454,6 +1454,8 @@ impl VisitMut for Optimizer<'_> { #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) { + self.drop_unused_arrow_params(&mut n.params); + let prepend = self.prepend_stmts.take(); let ctx = self.ctx; @@ -1685,9 +1687,7 @@ impl VisitMut for Optimizer<'_> { match n { DefaultDecl::Class(_) => {} DefaultDecl::Fn(f) => { - if !self.options.keep_fargs && self.options.unused { - self.drop_unused_params(&mut f.function.params); - } + self.drop_unused_params(&mut f.function.params); } DefaultDecl::TsInterfaceDecl(_) => {} } @@ -1708,9 +1708,7 @@ impl VisitMut for Optimizer<'_> { #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] fn visit_mut_export_decl(&mut self, n: &mut ExportDecl) { if let Decl::Fn(f) = &mut n.decl { - if !self.options.keep_fargs && self.options.unused { - self.drop_unused_params(&mut f.function.params); - } + self.drop_unused_params(&mut f.function.params); } let ctx = Ctx { @@ -2027,9 +2025,7 @@ impl VisitMut for Optimizer<'_> { .entry(f.ident.to_id()) .or_insert_with(|| FnMetadata::from(&*f.function)); - if !self.options.keep_fargs && self.options.unused { - self.drop_unused_params(&mut f.function.params); - } + self.drop_unused_params(&mut f.function.params); let ctx = Ctx { top_level: false, diff --git a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs index 19c5a952f0d..575fbb414df 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs @@ -132,13 +132,36 @@ impl Optimizer<'_> { #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] pub(super) fn drop_unused_params(&mut self, params: &mut Vec) { + if self.options.keep_fargs || !self.options.unused { + return; + } + for param in params.iter_mut().rev() { self.take_pat_if_unused(&mut param.pat, None, false); if !param.pat.is_invalid() { - return; + break; } } + + params.retain(|p| !p.pat.is_invalid()); + } + + #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] + pub(super) fn drop_unused_arrow_params(&mut self, params: &mut Vec) { + if self.options.keep_fargs || !self.options.unused { + return; + } + + for param in params.iter_mut().rev() { + self.take_pat_if_unused(param, None, false); + + if !param.is_invalid() { + break; + } + } + + params.retain(|p| !p.is_invalid()); } #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/3126/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/3126/1/output.js index 7c9c480ebb2..86b77a9dcb2 100644 --- a/crates/swc_ecma_minifier/tests/fixture/issues/3126/1/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/issues/3126/1/output.js @@ -1,12 +1,12 @@ (()=>{ var __webpack_modules__ = { - 746: (__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__1)=>{ + 746: ()=>{ Object.prototype.hasOwnProperty; } }; __webpack_require__.m = __webpack_modules__; (()=>{ - __webpack_require__.O = (result, chunkIds, fn, priority)=>{ + __webpack_require__.O = (result, chunkIds)=>{ for(var j = 0; j < chunkIds.length; j++)Object.keys(__webpack_require__.O).every((key)=>__webpack_require__.O[key](chunkIds[j])); }; })(); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8626/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/8626/config.json new file mode 100644 index 00000000000..a67b77ab4da --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8626/config.json @@ -0,0 +1,4 @@ +{ + "defaults": true, + "keep_fargs": false +} diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8626/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8626/input.js new file mode 100644 index 00000000000..996b5e17596 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8626/input.js @@ -0,0 +1,5 @@ +export function foo(cb) { + cb(); +} + +foo((a, b) => true); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8626/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8626/output.js new file mode 100644 index 00000000000..91d6657c93c --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8626/output.js @@ -0,0 +1,4 @@ +export function foo(cb) { + cb(); +} +foo(()=>!0);