Skip to content

Commit 39271ad

Browse files
authored
fix(es/minifier): Drop console in optional chainings (#9759)
**Related issue:** - Closes #9757
1 parent 04e01ef commit 39271ad

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

.changeset/dirty-ways-remain.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_minifier: patch
4+
---
5+
6+
fix(es/minifier): drop console in opt chains

crates/swc_ecma_minifier/src/compress/pure/drop_console.rs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,47 @@ impl Pure<'_> {
99
return;
1010
}
1111

12-
if let Expr::Call(CallExpr { callee, .. }) = e {
13-
// Find console.log
14-
let callee = match callee {
15-
Callee::Expr(callee) => callee,
16-
_ => return,
17-
};
12+
let Some(callee) = (match e {
13+
Expr::Call(call) => call.callee.as_expr(),
14+
Expr::OptChain(opt_chain) => opt_chain.base.as_call().map(|call| &call.callee),
15+
_ => None,
16+
}) else {
17+
return;
18+
};
1819

19-
if let Expr::Member(MemberExpr {
20-
obj: callee_obj,
21-
prop: MemberProp::Ident(_),
22-
..
23-
}) = &**callee
24-
{
25-
let mut loop_co = &**callee_obj;
26-
loop {
27-
match loop_co {
28-
Expr::Ident(obj) => {
29-
if obj.sym != *"console" {
30-
return;
31-
}
32-
break;
33-
}
20+
let Some(mut loop_co) = (match callee.as_ref() {
21+
Expr::Member(member) => Some(&member.obj),
22+
Expr::OptChain(opt_chain) => opt_chain.base.as_member().map(|member| &member.obj),
23+
_ => None,
24+
}) else {
25+
return;
26+
};
3427

35-
Expr::Member(MemberExpr {
36-
obj: loop_co_obj,
37-
prop: MemberProp::Ident(_),
38-
..
39-
}) => {
40-
loop_co = loop_co_obj;
41-
}
42-
_ => return,
28+
loop {
29+
match loop_co.as_ref() {
30+
Expr::Ident(obj) => {
31+
if obj.sym != *"console" {
32+
return;
4333
}
34+
break;
4435
}
45-
46-
report_change!("drop_console: Removing console call");
47-
self.changed = true;
48-
*e = *Expr::undefined(DUMMY_SP);
36+
Expr::Member(MemberExpr {
37+
obj: loop_co_obj,
38+
prop: MemberProp::Ident(_),
39+
..
40+
}) => {
41+
loop_co = loop_co_obj;
42+
}
43+
Expr::OptChain(opt_chain) => match opt_chain.base.as_member() {
44+
Some(member) => loop_co = &member.obj,
45+
None => return,
46+
},
47+
_ => return,
4948
}
5049
}
50+
51+
report_change!("drop_console: Removing console call");
52+
self.changed = true;
53+
*e = *Expr::undefined(DUMMY_SP);
5154
}
5255
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"unused": true,
3+
"drop_console": true
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function A() {
2+
console?.log?.(123);
3+
console?.log?.apply(console, arguments);
4+
console?.a.b?.c(console, arguments);
5+
console?.any();
6+
console?.warn();
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function A() {}

0 commit comments

Comments
 (0)