fix(es/transforms/optimization): Fix dead_branch_remover (#2373)

swc_ecma_transforms_optimization:
 - `dead_branch_remover`: Fix handling of `switch(boolean)`.
This commit is contained in:
Niklas Mischkulnig 2021-10-08 04:56:09 +02:00 committed by GitHub
parent b0ee9543d4
commit 1f99c3a44c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 2 deletions

2
Cargo.lock generated
View File

@ -2851,7 +2851,7 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_optimization"
version = "0.49.0"
version = "0.49.1"
dependencies = [
"dashmap",
"indexmap",

View File

@ -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]

View File

@ -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()

View File

@ -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}",
"",