mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 09:38:16 +03:00
fix(es/optimization): Don't create invalid sequence expressions (#4285)
This commit is contained in:
parent
6c50ae6b0d
commit
4868c73d5b
@ -82,6 +82,12 @@ impl VisitMut for Remover {
|
||||
e.visit_mut_children_with(self);
|
||||
|
||||
match e {
|
||||
Expr::Seq(s) => {
|
||||
if s.exprs.is_empty() {
|
||||
*e = Expr::dummy();
|
||||
}
|
||||
}
|
||||
|
||||
Expr::Assign(AssignExpr {
|
||||
op: op!("="),
|
||||
left: PatOrExpr::Pat(l),
|
||||
@ -154,6 +160,19 @@ impl VisitMut for Remover {
|
||||
*e = *cond.cons.take();
|
||||
}
|
||||
|
||||
Expr::Bin(be) => match (be.left.is_invalid(), be.right.is_invalid()) {
|
||||
(true, true) => {
|
||||
*e = Expr::dummy();
|
||||
}
|
||||
(true, false) => {
|
||||
*e = *be.right.take();
|
||||
}
|
||||
(false, true) => {
|
||||
*e = *be.left.take();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@ -189,7 +208,7 @@ impl VisitMut for Remover {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Removing dead branches");
|
||||
}
|
||||
self.fold_stmt_like(n)
|
||||
self.fold_stmt_like(n);
|
||||
}
|
||||
|
||||
fn visit_mut_object_pat(&mut self, p: &mut ObjectPat) {
|
||||
@ -1483,6 +1502,10 @@ fn ignore_result(e: Expr) -> Option<Expr> {
|
||||
|
||||
exprs.extend(last);
|
||||
|
||||
if exprs.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Expr::Seq(SeqExpr { span, exprs }))
|
||||
}
|
||||
|
||||
|
@ -631,6 +631,24 @@ test!(
|
||||
"
|
||||
);
|
||||
|
||||
test!(
|
||||
Syntax::default(),
|
||||
|_| dead_branch_remover(),
|
||||
issue_4272,
|
||||
"
|
||||
function oe() {
|
||||
var e, t;
|
||||
return t !== i && (e, t = t), e = t;
|
||||
}
|
||||
",
|
||||
"
|
||||
function oe() {
|
||||
var e, t;
|
||||
return e = t;
|
||||
}
|
||||
"
|
||||
);
|
||||
|
||||
test!(
|
||||
Syntax::default(),
|
||||
|_| chain!(
|
||||
|
Loading…
Reference in New Issue
Block a user