Skip to content

Commit

Permalink
fix(es/utils): Preserve optional chain effect (#8850)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8844
  • Loading branch information
Austaras committed Apr 12, 2024
1 parent 6bc4de6 commit a7a32c4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
7 changes: 7 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8844/input.js
@@ -0,0 +1,7 @@
const k = (() => {
let x;
switch (x) {
case x?.x?.():
default:
}
})();
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8844/output.js
@@ -0,0 +1,2 @@
let x;
x?.x?.();
9 changes: 1 addition & 8 deletions crates/swc_ecma_utils/src/lib.rs
Expand Up @@ -2520,11 +2520,6 @@ impl ExprCtx {
to.push(Box::new(Expr::New(e)))
}
Expr::Member(_) | Expr::SuperProp(_) => to.push(Box::new(expr)),
Expr::OptChain(OptChainExpr { ref base, .. })
if matches!(&**base, OptChainBase::Member(_)) =>
{
to.push(Box::new(expr))
}

// We are at here because we could not determine value of test.
//TODO: Drop values if it does not have side effects.
Expand Down Expand Up @@ -2664,9 +2659,7 @@ impl ExprCtx {
| Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => {
self.extract_side_effects_to(to, *expr)
}
Expr::OptChain(OptChainExpr { base: child, .. }) => {
self.extract_side_effects_to(to, (*child).into())
}
Expr::OptChain(..) => to.push(Box::new(expr)),

Expr::Invalid(..) => unreachable!(),
}
Expand Down

0 comments on commit a7a32c4

Please sign in to comment.