Skip to content

Commit

Permalink
fix: fixer should respect remove_only
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Dec 7, 2023
1 parent 7c4430b commit e381e91
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
8 changes: 7 additions & 1 deletion crates/swc_ecma_minifier/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/src/pass/precompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
15 changes: 13 additions & 2 deletions crates/swc_ecma_minifier/tests/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(obj?.a).b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(obj?.a).b;
24 changes: 7 additions & 17 deletions crates/swc_ecma_transforms_base/src/fixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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, .. })
Expand All @@ -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
Expand Down

0 comments on commit e381e91

Please sign in to comment.