feat(es/minifier): Improve drop_console (#2830)

swc_ecma_minifier:
 - Implement more rules for `drop_console`. (Closes #2807)
This commit is contained in:
黄伟 2021-11-22 16:54:36 +08:00 committed by GitHub
parent bfa6458737
commit c5768d7672
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 9 deletions

View File

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

View File

@ -0,0 +1,4 @@
{
"unused": true,
"drop_console": true
}

View File

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

View File

@ -0,0 +1,2 @@
export default function A() {
};