diff --git a/crates/swc_ecma_minifier/src/metadata/mod.rs b/crates/swc_ecma_minifier/src/metadata/mod.rs index 3d564b95da932..eac05979bf828 100644 --- a/crates/swc_ecma_minifier/src/metadata/mod.rs +++ b/crates/swc_ecma_minifier/src/metadata/mod.rs @@ -97,7 +97,13 @@ impl VisitMut for InfoMarker<'_> { fn visit_mut_call_expr(&mut self, n: &mut CallExpr) { n.visit_mut_children_with(self); - if has_noinline(self.comments, n.span) { + // TODO: remove after we figure out how to move comments properly + if has_noinline(self.comments, n.span) + || match &n.callee { + Callee::Expr(e) => has_noinline(self.comments, e.span()), + _ => false, + } + { n.span = n.span.apply_mark(self.marks.noinline); } diff --git a/crates/swc_ecma_minifier/src/pass/precompress.rs b/crates/swc_ecma_minifier/src/pass/precompress.rs index a187d670bc89e..efd758a510bf4 100644 --- a/crates/swc_ecma_minifier/src/pass/precompress.rs +++ b/crates/swc_ecma_minifier/src/pass/precompress.rs @@ -8,6 +8,7 @@ use crate::HEAVY_TASK_PARALLELS; /// Optimizer invoked before invoking compressor. /// /// - Remove parens. +/// TODO: remove completely after #8333 pub(crate) fn precompress_optimizer<'a>() -> impl 'a + VisitMut { PrecompressOptimizer {} } diff --git a/crates/swc_ecma_minifier/tests/compress.rs b/crates/swc_ecma_minifier/tests/compress.rs index c13ac95f099cf..19220e987afe7 100644 --- a/crates/swc_ecma_minifier/tests/compress.rs +++ b/crates/swc_ecma_minifier/tests/compress.rs @@ -39,7 +39,11 @@ use swc_ecma_parser::{ EsConfig, Parser, Syntax, }; use swc_ecma_testing::{exec_node_js, JsExecOptions}; -use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver}; +use swc_ecma_transforms_base::{ + fixer::{fixer, paren_remover}, + hygiene::hygiene, + resolver, +}; use swc_ecma_utils::drop_span; use swc_ecma_visit::{FoldWith, Visit, VisitMut, VisitMutWith, VisitWith}; use testing::{assert_eq, unignore_fixture, DebugUsingDisplay, NormalizedOutput}; @@ -194,7 +198,14 @@ fn run( .map_err(|err| { err.into_diagnostic(handler).emit(); }) - .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))); + .map(|mut module| { + module.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); + module.visit_mut_with(&mut paren_remover(Some(&comments))); + + module + }); + + println!("{:#?}", program); // Ignore parser errors. // diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8398/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8398/input.js new file mode 100644 index 0000000000000..49f7ae0b3f002 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8398/input.js @@ -0,0 +1 @@ +(obj?.a).b; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8398/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8398/output.js new file mode 100644 index 0000000000000..49f7ae0b3f002 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8398/output.js @@ -0,0 +1 @@ +(obj?.a).b; diff --git a/crates/swc_ecma_transforms_base/src/fixer.rs b/crates/swc_ecma_transforms_base/src/fixer.rs index 4b1988e0efa3c..1b80145201ff1 100644 --- a/crates/swc_ecma_transforms_base/src/fixer.rs +++ b/crates/swc_ecma_transforms_base/src/fixer.rs @@ -857,17 +857,13 @@ impl Fixer<'_> { } } - let expr = Expr::Seq(SeqExpr { span: *span, exprs }); + let mut expr = Expr::Seq(SeqExpr { span: *span, exprs }); - match self.ctx { - Context::ForcedExpr => { - *e = Expr::Paren(ParenExpr { - span: *span, - expr: Box::new(expr), - }) - } - _ => *e = expr, + if let Context::ForcedExpr = self.ctx { + self.wrap(&mut expr); }; + + *e = expr; } Expr::Cond(expr) => { @@ -907,10 +903,7 @@ impl Fixer<'_> { || callee.is_await_expr() || callee.is_assign() => { - *callee = Box::new(Expr::Paren(ParenExpr { - span: callee.span(), - expr: callee.take(), - })) + self.wrap(callee); } Expr::OptChain(OptChainExpr { base, .. }) => match &mut **base { OptChainBase::Call(OptCall { callee, .. }) @@ -919,10 +912,7 @@ impl Fixer<'_> { || callee.is_await_expr() || callee.is_assign() => { - *callee = Box::new(Expr::Paren(ParenExpr { - span: callee.span(), - expr: callee.take(), - })) + self.wrap(callee); } OptChainBase::Call(OptCall { callee, .. }) if callee.is_fn_expr() => match self.ctx