diff --git a/.changeset/dirty-ways-remain.md b/.changeset/dirty-ways-remain.md new file mode 100644 index 000000000000..72d16bfe0a4e --- /dev/null +++ b/.changeset/dirty-ways-remain.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_minifier: patch +--- + +fix(es/minifier): drop console in opt chains diff --git a/crates/swc_ecma_minifier/src/compress/pure/drop_console.rs b/crates/swc_ecma_minifier/src/compress/pure/drop_console.rs index 15d525935ef0..cfc9c7697382 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/drop_console.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/drop_console.rs @@ -9,44 +9,47 @@ impl Pure<'_> { return; } - if let Expr::Call(CallExpr { callee, .. }) = e { - // Find console.log - let callee = match callee { - Callee::Expr(callee) => callee, - _ => return, - }; + let Some(callee) = (match e { + Expr::Call(call) => call.callee.as_expr(), + Expr::OptChain(opt_chain) => opt_chain.base.as_call().map(|call| &call.callee), + _ => None, + }) else { + return; + }; - if let Expr::Member(MemberExpr { - obj: callee_obj, - prop: MemberProp::Ident(_), - .. - }) = &**callee - { - let mut loop_co = &**callee_obj; - loop { - match loop_co { - Expr::Ident(obj) => { - if obj.sym != *"console" { - return; - } - break; - } + let Some(mut loop_co) = (match callee.as_ref() { + Expr::Member(member) => Some(&member.obj), + Expr::OptChain(opt_chain) => opt_chain.base.as_member().map(|member| &member.obj), + _ => None, + }) else { + return; + }; - Expr::Member(MemberExpr { - obj: loop_co_obj, - prop: MemberProp::Ident(_), - .. - }) => { - loop_co = loop_co_obj; - } - _ => return, + loop { + match loop_co.as_ref() { + Expr::Ident(obj) => { + if obj.sym != *"console" { + return; } + break; } - - report_change!("drop_console: Removing console call"); - self.changed = true; - *e = *Expr::undefined(DUMMY_SP); + Expr::Member(MemberExpr { + obj: loop_co_obj, + prop: MemberProp::Ident(_), + .. + }) => { + loop_co = loop_co_obj; + } + Expr::OptChain(opt_chain) => match opt_chain.base.as_member() { + Some(member) => loop_co = &member.obj, + None => return, + }, + _ => return, } } + + report_change!("drop_console: Removing console call"); + self.changed = true; + *e = *Expr::undefined(DUMMY_SP); } } diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9757/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/9757/config.json new file mode 100644 index 000000000000..023412969f8f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9757/config.json @@ -0,0 +1,4 @@ +{ + "unused": true, + "drop_console": true +} diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9757/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/9757/input.js new file mode 100644 index 000000000000..cb42e1e6054d --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9757/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/fixture/issues/9757/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/9757/output.js new file mode 100644 index 000000000000..3e24f6461661 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9757/output.js @@ -0,0 +1 @@ +export default function A() {}