refactor(es/minifier): Support stable rustc (#7734)

This commit is contained in:
Donny/강동윤 2023-08-08 07:55:00 +09:00 committed by GitHub
parent 14906e279f
commit f7afe7edec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 49 deletions

View File

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

View File

@ -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(),
}),
});
}
_ => (),
}
}
}

View File

@ -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};

View File

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