Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/utils): Preserve optional chain effect #8850

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8844/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const k = (() => {
let x;
switch (x) {
case x?.x?.():
default:
}
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let x;
x?.x?.();
9 changes: 1 addition & 8 deletions crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
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, .. })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously wrong -- hasn't considered computed keys, and I'm worried about nested case too, so it's economical to just remove it.

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