feat(es/minifier): Remove unused parameters of arrow functions (#8636)

**Related issue:**

 - Closes #8626
This commit is contained in:
Donny/강동윤 2024-02-14 11:09:11 +09:00 committed by GitHub
parent d170d7bc2c
commit 8cd4813067
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 44 additions and 12 deletions

View File

@ -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,

View File

@ -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<Param>) {
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<Pat>) {
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))]

View File

@ -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]));
};
})();

View File

@ -0,0 +1,4 @@
{
"defaults": true,
"keep_fargs": false
}

View File

@ -0,0 +1,5 @@
export function foo(cb) {
cb();
}
foo((a, b) => true);

View File

@ -0,0 +1,4 @@
export function foo(cb) {
cb();
}
foo(()=>!0);