From 1f99c3a44cab4faed5f5ddfe08f6b1a6da80f167 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 8 Oct 2021 04:56:09 +0200 Subject: [PATCH] fix(es/transforms/optimization): Fix `dead_branch_remover` (#2373) swc_ecma_transforms_optimization: - `dead_branch_remover`: Fix handling of `switch(boolean)`. --- Cargo.lock | 2 +- ecmascript/transforms/optimization/Cargo.toml | 2 +- .../transforms/optimization/src/simplify/branch/mod.rs | 4 ++++ .../transforms/optimization/src/simplify/branch/tests.rs | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33bc8427181b..9329a80ff492 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2851,7 +2851,7 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_optimization" -version = "0.49.0" +version = "0.49.1" dependencies = [ "dashmap", "indexmap", diff --git a/ecmascript/transforms/optimization/Cargo.toml b/ecmascript/transforms/optimization/Cargo.toml index adb6bc86e7b5..2fdc9ecf0da3 100644 --- a/ecmascript/transforms/optimization/Cargo.toml +++ b/ecmascript/transforms/optimization/Cargo.toml @@ -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] diff --git a/ecmascript/transforms/optimization/src/simplify/branch/mod.rs b/ecmascript/transforms/optimization/src/simplify/branch/mod.rs index d0d1611af085..1f291967965c 100644 --- a/ecmascript/transforms/optimization/src/simplify/branch/mod.rs +++ b/ecmascript/transforms/optimization/src/simplify/branch/mod.rs @@ -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() diff --git a/ecmascript/transforms/optimization/src/simplify/branch/tests.rs b/ecmascript/transforms/optimization/src/simplify/branch/tests.rs index f71467c7352e..30f5239922bd 100644 --- a/ecmascript/transforms/optimization/src/simplify/branch/tests.rs +++ b/ecmascript/transforms/optimization/src/simplify/branch/tests.rs @@ -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}", "",