Skip to content

Commit

Permalink
fix(es/minifier): Only merge last if return (#9633)
Browse files Browse the repository at this point in the history
**Related issue:**
 - Closes #9628
  • Loading branch information
Austaras authored Oct 12, 2024
1 parent f74c1f3 commit 6f52949
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-lions-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---

fix(es/minifier): Only merge last if return
6 changes: 3 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/if_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,16 @@ impl Optimizer<'_> {
}
}

fn can_merge_stmt_as_if_return(&self, s: &Stmt, _is_last: bool) -> bool {
fn can_merge_stmt_as_if_return(&self, s: &Stmt, is_last: bool) -> bool {
// if !res {
// trace!("Cannot merge: {}", dump(s));
// }

match s {
Stmt::Expr(..) => true,
Stmt::Return(..) => true,
Stmt::Return(..) => is_last,
Stmt::Block(s) => {
s.stmts.len() == 1 && self.can_merge_stmt_as_if_return(&s.stmts[0], false)
s.stmts.len() == 1 && self.can_merge_stmt_as_if_return(&s.stmts[0], is_last)
}
Stmt::If(stmt) => {
matches!(&*stmt.cons, Stmt::Return(..))
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9628/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const getString = () => {
switch ("apple") {
case "apple":
return "1";
case "banana":
return "2";
}
return "3";
};

console.log(getString());
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("1");

0 comments on commit 6f52949

Please sign in to comment.