From c5768d7672ba33e75e0268176a90f9fb1d725fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F?= Date: Mon, 22 Nov 2021 16:54:36 +0800 Subject: [PATCH] feat(es/minifier): Improve `drop_console` (#2830) swc_ecma_minifier: - Implement more rules for `drop_console`. (Closes #2807) --- .../src/compress/drop_console.rs | 30 +++++++++++++------ .../fixture/issues/2807/1/config.json | 4 +++ .../compress/fixture/issues/2807/1/input.js | 7 +++++ .../compress/fixture/issues/2807/1/output.js | 2 ++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/config.json create mode 100644 crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/input.js create mode 100644 crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/output.js diff --git a/crates/swc_ecma_minifier/src/compress/drop_console.rs b/crates/swc_ecma_minifier/src/compress/drop_console.rs index 5b536fc50bd..7b66b31c103 100644 --- a/crates/swc_ecma_minifier/src/compress/drop_console.rs +++ b/crates/swc_ecma_minifier/src/compress/drop_console.rs @@ -46,18 +46,30 @@ impl VisitMut for DropConsole { computed: false, .. }) => { - match (&**callee_obj, &**callee_prop) { - (Expr::Ident(obj), Expr::Ident(prop)) => { - if obj.sym != *"console" { - return; + let mut loop_co = &**callee_obj; + let mut loop_cp = &**callee_prop; + loop { + match (loop_co, loop_cp) { + (Expr::Ident(obj), Expr::Ident(_prop)) => { + if obj.sym != *"console" { + return; + } + break; } - - match &*prop.sym { - "log" | "info" => {} - _ => return, + ( + Expr::Member(MemberExpr { + obj: ExprOrSuper::Expr(loop_co_obj), + prop: loop_cp_prop, + computed: false, + .. + }), + Expr::Ident(_prop), + ) => { + loop_co = &loop_co_obj; + loop_cp = &loop_cp_prop; } + _ => return, } - _ => return, } // Sioplifier will remove side-effect-free items. diff --git a/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/config.json b/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/config.json new file mode 100644 index 00000000000..39cc083610e --- /dev/null +++ b/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/config.json @@ -0,0 +1,4 @@ +{ + "unused": true, + "drop_console": true +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/input.js b/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/input.js new file mode 100644 index 00000000000..425615de6f8 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/input.js @@ -0,0 +1,7 @@ +export default function A() { + console.log(123); + console.log.apply(console, arguments); + console.a.b.c(console, arguments); + console.any(); + console.warn(); +} diff --git a/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/output.js b/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/output.js new file mode 100644 index 00000000000..dc6fa4d0bad --- /dev/null +++ b/crates/swc_ecma_minifier/tests/compress/fixture/issues/2807/1/output.js @@ -0,0 +1,2 @@ +export default function A() { +};