Skip to content

Commit

Permalink
fix(es/transforms/optimization): Fix dead_branch_remover (#2373)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_optimization:
 - `dead_branch_remover`: Fix handling of `switch(boolean)`.
  • Loading branch information
mischnic authored Oct 8, 2021
1 parent b0ee954 commit 1f99c3a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/transforms/optimization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_optimization"
repository = "https://github.com/swc-project/swc.git"
version = "0.49.0"
version = "0.49.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions ecmascript/transforms/optimization/src/simplify/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ impl VisitMut for Remover {
&Expr::Lit(Lit::Num(Number { value: test, .. })),
&Expr::Lit(Lit::Num(Number { value: d, .. })),
) => (test - d).abs() < 1e-10,
(
&Expr::Lit(Lit::Bool(Bool { value: test, .. })),
&Expr::Lit(Lit::Bool(Bool { value: d, .. })),
) => test == d,
(&Expr::Lit(Lit::Null(..)), &Expr::Lit(Lit::Null(..))) => true,
(&Expr::Ident(ref test), &Expr::Ident(ref d)) => {
test.sym == d.sym && test.span.ctxt() == d.span.ctxt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,11 @@ fn test_optimize_switch_4() {
"switch (1) {\ncase 1:\n foo();\n break;\ncase 2:\n bar();\n break;\n}",
"foo();",
);
test(
"switch (true) {\ncase true:\n foo();\n break;\ncase false:\n bar();\n break;
default: foobar(); break; \n}",
"foo();",
);
test(
"switch (1) {\ncase 1.1:\n foo();\n break;\ncase 2:\n bar();\n break;\n}",
"",
Expand Down

0 comments on commit 1f99c3a

Please sign in to comment.