fix(es/optimization): Don't create invalid sequence expressions (#4285)

This commit is contained in:
Donny/강동윤 2022-04-09 03:08:31 +09:00 committed by GitHub
parent 6c50ae6b0d
commit 4868c73d5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -82,6 +82,12 @@ impl VisitMut for Remover {
e.visit_mut_children_with(self); e.visit_mut_children_with(self);
match e { match e {
Expr::Seq(s) => {
if s.exprs.is_empty() {
*e = Expr::dummy();
}
}
Expr::Assign(AssignExpr { Expr::Assign(AssignExpr {
op: op!("="), op: op!("="),
left: PatOrExpr::Pat(l), left: PatOrExpr::Pat(l),
@ -154,6 +160,19 @@ impl VisitMut for Remover {
*e = *cond.cons.take(); *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") { if cfg!(feature = "debug") {
debug!("Removing dead branches"); debug!("Removing dead branches");
} }
self.fold_stmt_like(n) self.fold_stmt_like(n);
} }
fn visit_mut_object_pat(&mut self, p: &mut ObjectPat) { fn visit_mut_object_pat(&mut self, p: &mut ObjectPat) {
@ -1483,6 +1502,10 @@ fn ignore_result(e: Expr) -> Option<Expr> {
exprs.extend(last); exprs.extend(last);
if exprs.is_empty() {
return None;
}
Some(Expr::Seq(SeqExpr { span, exprs })) Some(Expr::Seq(SeqExpr { span, exprs }))
} }

View File

@ -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!( test!(
Syntax::default(), Syntax::default(),
|_| chain!( |_| chain!(