diff --git a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs index 0a808a2cbce..0f1959a831e 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs @@ -635,7 +635,7 @@ impl Optimizer<'_> { } } - trace_op!("iife: Empry function"); + trace_op!("iife: Empty function"); let body = f.function.body.as_mut().unwrap(); if body.stmts.is_empty() && call.args.is_empty() { @@ -724,7 +724,7 @@ impl Optimizer<'_> { // Abort on eval. // See https://github.com/swc-project/swc/pull/6478 // - // We completetly abort on eval, because we cannot know whether a variable in + // We completely abort on eval, because we cannot know whether a variable in // upper scope will be afftected by eval. // https://github.com/swc-project/swc/issues/6628 if self.data.top.has_eval_call { @@ -1077,11 +1077,14 @@ impl Optimizer<'_> { Expr::Arrow(ArrowExpr { params, - body: box BlockStmtOrExpr::Expr(body), + body, is_async: false, is_generator: false, .. - }) => params.iter().all(|p| p.is_ident()) && self.can_be_inlined_for_iife(body), + }) if body.is_expr() => { + params.iter().all(|p| p.is_ident()) + && self.can_be_inlined_for_iife(body.as_expr().unwrap()) + } _ => false, } diff --git a/crates/swc_ecma_minifier/src/compress/pure/arrows.rs b/crates/swc_ecma_minifier/src/compress/pure/arrows.rs index 8fd24e225f5..16614f05dda 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/arrows.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/arrows.rs @@ -131,35 +131,32 @@ impl Pure<'_> { return; } - if let Expr::Arrow( - m @ ArrowExpr { - body: box BlockStmtOrExpr::BlockStmt(..), - .. - }, - ) = &mut *kv.value - { - *p = Prop::Method(MethodProp { - key: kv.key.take(), - function: Box::new(Function { - params: m - .params - .take() - .into_iter() - .map(|pat| Param { - span: DUMMY_SP, - decorators: Default::default(), - pat, - }) - .collect(), - decorators: Default::default(), - span: m.span, - body: m.body.take().block_stmt(), - is_generator: m.is_generator, - is_async: m.is_async, - type_params: Default::default(), - return_type: Default::default(), - }), - }); + match &mut *kv.value { + Expr::Arrow(m) if m.body.is_block_stmt() => { + *p = Prop::Method(MethodProp { + key: kv.key.take(), + function: Box::new(Function { + params: m + .params + .take() + .into_iter() + .map(|pat| Param { + span: DUMMY_SP, + decorators: Default::default(), + pat, + }) + .collect(), + decorators: Default::default(), + span: m.span, + body: m.body.take().block_stmt(), + is_generator: m.is_generator, + is_async: m.is_async, + type_params: Default::default(), + return_type: Default::default(), + }), + }); + } + _ => (), } } } diff --git a/crates/swc_ecma_minifier/src/lib.rs b/crates/swc_ecma_minifier/src/lib.rs index ef2d1b67eab..071c76527ec 100644 --- a/crates/swc_ecma_minifier/src/lib.rs +++ b/crates/swc_ecma_minifier/src/lib.rs @@ -36,7 +36,6 @@ #![allow(clippy::only_used_in_recursion)] #![allow(unstable_name_collisions)] #![allow(clippy::match_like_matches_macro)] -#![feature(box_patterns)] use once_cell::sync::Lazy; use swc_common::{comments::Comments, pass::Repeated, sync::Lrc, SourceMap, SyntaxContext}; diff --git a/crates/swc_ecma_minifier/src/pass/global_defs.rs b/crates/swc_ecma_minifier/src/pass/global_defs.rs index 33ceb6b5bc4..9d9cf4d5e1e 100644 --- a/crates/swc_ecma_minifier/src/pass/global_defs.rs +++ b/crates/swc_ecma_minifier/src/pass/global_defs.rs @@ -101,7 +101,24 @@ fn should_replace(pred: &Expr, node: &Expr) -> bool { return true; } - match (pred, node) { + fn match_node(node: &Expr) -> Option<(&Expr, &MemberProp)> { + match node { + Expr::Member(MemberExpr { + obj: node_obj, + prop: nodes, + .. + }) => Some((node_obj, nodes)), + + Expr::OptChain(OptChainExpr { base, .. }) => { + let base = base.as_member()?; + Some((&base.obj, &base.prop)) + } + + _ => None, + } + } + + match (pred, match_node(node)) { // super?. is invalid ( Expr::Member(MemberExpr { @@ -109,20 +126,7 @@ fn should_replace(pred: &Expr, node: &Expr) -> bool { prop: pred, .. }), - Expr::Member(MemberExpr { - obj: node_obj, - prop: nodes, - .. - }) - | Expr::OptChain(OptChainExpr { - base: - box OptChainBase::Member(MemberExpr { - obj: node_obj, - prop: nodes, - .. - }), - .. - }), + Some((node_obj, nodes)), ) if !(pred.is_computed() || nodes.is_computed()) => { if !pred.eq_ignore_span(nodes) { return false;