From 07ce20efa3609c757875a037a4834a9b20937b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 18 Jul 2024 10:37:40 +0900 Subject: [PATCH] Partial revert --- .../src/compress/hoist_decls.rs | 4 ++-- crates/swc_ecma_minifier/src/compress/mod.rs | 4 ++-- .../src/compress/optimize/arguments.rs | 4 ++-- .../src/compress/optimize/if_return.rs | 4 ++-- .../src/compress/optimize/mod.rs | 4 ++-- .../src/compress/optimize/sequences.rs | 4 ++-- .../src/compress/optimize/switches.rs | 4 ++-- .../src/compress/optimize/unused.rs | 4 ++-- .../src/compress/optimize/util.rs | 8 ++++---- .../src/compress/pure/dead_code.rs | 4 ++-- .../swc_ecma_minifier/src/compress/pure/mod.rs | 4 ++-- .../swc_ecma_minifier/src/compress/pure/vars.rs | 8 ++++---- .../swc_ecma_minifier/src/compress/util/mod.rs | 8 ++++---- .../src/compress/util/tests.rs | 4 ++-- crates/swc_ecma_minifier/src/debug.rs | 4 ++-- crates/swc_ecma_minifier/src/lib.rs | 3 ++- crates/swc_ecma_minifier/src/metadata/mod.rs | 6 +++--- .../swc_ecma_minifier/src/pass/expand_names.rs | 15 +++++++++++++++ .../swc_ecma_minifier/src/pass/global_defs.rs | 4 ++-- .../swc_ecma_minifier/src/pass/hygiene/vars.rs | 2 +- .../src/pass/mangle_names/mod.rs | 4 ++-- .../src/pass/mangle_names/preserver.rs | 4 ++-- .../src/pass/mangle_names/private_name.rs | 4 ++-- .../swc_ecma_minifier/src/pass/mangle_props.rs | 4 ++-- .../swc_ecma_minifier/src/pass/merge_exports.rs | 4 ++-- crates/swc_ecma_minifier/src/pass/mod.rs | 1 + .../swc_ecma_minifier/src/pass/postcompress.rs | 4 ++-- .../swc_ecma_minifier/src/pass/precompress.rs | 6 ++---- crates/swc_ecma_minifier/src/util/base54.rs | 4 ++-- crates/swc_ecma_minifier/src/util/mod.rs | 12 ++++++------ crates/swc_ecma_minifier/tests/eval.rs | 4 ++-- .../src/const_modules.rs | 4 ++-- .../src/debug.rs | 4 ++-- .../src/inline_globals.rs | 4 ++-- .../src/json_parse.rs | 4 ++-- .../src/simplify/branch/mod.rs | 7 +++---- .../src/simplify/const_propagation.rs | 4 ++-- .../src/simplify/dce/mod.rs | 7 +++---- .../src/simplify/expr/mod.rs | 4 ++-- .../src/simplify/inlining/mod.rs | 10 +++++----- .../tests/simplify_dce.rs/issue_1150_1.js | 2 +- .../tests/dce-jsx/property/input.js | 7 +++++++ .../tests/dce-jsx/property/output.js | 6 ++++++ .../tests/dce-jsx/property2/input.js | 8 ++++++++ .../tests/dce-jsx/property2/output.js | 6 ++++++ .../tests/dce-jsx/spread/input.js | 8 ++++++++ .../tests/dce-jsx/spread/output.js | 8 ++++++++ .../tests/dce-jsx/var/input.js | 8 ++++++++ .../tests/dce-jsx/var/output.js | 8 ++++++++ .../tests/fixture.rs | 17 +++++++++++++++++ .../tests/simplify.rs | 4 ++-- .../tests/simplify_dce.rs | 14 +++++++------- .../tests/simplify_inlining.rs | 11 +++++------ .../src/refresh/tests.rs | 7 ++++++- .../include_hook_signature_in_commonjs.js | 3 +++ crates/swc_ecma_usage_analyzer/src/alias/mod.rs | 4 ++-- .../swc_ecma_usage_analyzer/src/analyzer/mod.rs | 4 ++-- 57 files changed, 211 insertions(+), 115 deletions(-) create mode 100644 crates/swc_ecma_minifier/src/pass/expand_names.rs create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/input.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/output.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/input.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/output.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/input.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/output.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/input.js create mode 100644 crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/output.js diff --git a/crates/swc_ecma_minifier/src/compress/hoist_decls.rs b/crates/swc_ecma_minifier/src/compress/hoist_decls.rs index 242fc05c1f912..be315596507fe 100644 --- a/crates/swc_ecma_minifier/src/compress/hoist_decls.rs +++ b/crates/swc_ecma_minifier/src/compress/hoist_decls.rs @@ -4,7 +4,7 @@ use swc_common::{collections::AHashSet, pass::Repeated, util::take::Take, DUMMY_ use swc_ecma_ast::*; use swc_ecma_usage_analyzer::analyzer::UsageAnalyzer; use swc_ecma_utils::{find_pat_ids, StmtLike}; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; use super::util::drop_invalid_stmts; use crate::{ @@ -279,7 +279,7 @@ impl Hoister<'_> { } impl VisitMut for Hoister<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_module_items(&mut self, stmts: &mut Vec) { self.handle_stmt_likes(stmts); diff --git a/crates/swc_ecma_minifier/src/compress/mod.rs b/crates/swc_ecma_minifier/src/compress/mod.rs index 6362dd829a2ff..e63d784d294b5 100644 --- a/crates/swc_ecma_minifier/src/compress/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/mod.rs @@ -15,7 +15,7 @@ use swc_ecma_transforms_optimization::simplify::{ dead_branch_remover, expr_simplifier, ExprSimplifierConfig, }; use swc_ecma_usage_analyzer::{analyzer::UsageAnalyzer, marks::Marks}; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; use swc_timer::timer; use tracing::{debug, error}; @@ -325,7 +325,7 @@ impl Compressor<'_> { } impl VisitMut for Compressor<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_script(&mut self, n: &mut Script) { self.optimize_unit_repeatedly(n); diff --git a/crates/swc_ecma_minifier/src/compress/optimize/arguments.rs b/crates/swc_ecma_minifier/src/compress/optimize/arguments.rs index eec94a93ab64a..a28bbb27b7904 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/arguments.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/arguments.rs @@ -3,7 +3,7 @@ use std::iter::repeat_with; use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{find_pat_ids, is_valid_prop_ident, private_ident}; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use super::Optimizer; use crate::compress::optimize::is_left_access_to_arguments; @@ -146,7 +146,7 @@ impl ArgReplacer<'_> { } impl VisitMut for ArgReplacer<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); /// Noop. fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {} diff --git a/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs b/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs index eecafd76a6878..a2604c6441322 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs @@ -2,7 +2,7 @@ use swc_common::{util::take::Take, Spanned, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_optimization::debug_assert_valid; use swc_ecma_utils::{StmtExt, StmtLike}; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use super::Optimizer; #[cfg(feature = "debug")] @@ -529,7 +529,7 @@ pub(super) struct ReturnFinder { } impl Visit for ReturnFinder { - standard_only_visit!(); + noop_visit_type!(); fn visit_return_stmt(&mut self, n: &ReturnStmt) { n.visit_children_with(self); diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index 75fd5374602b4..b3f706b2fe172 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -14,7 +14,7 @@ use swc_ecma_usage_analyzer::{analyzer::UsageAnalyzer, marks::Marks}; use swc_ecma_utils::{ prepend_stmts, ExprCtx, ExprExt, ExprFactory, IsEmpty, ModuleItemLike, StmtLike, Type, Value, }; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; #[cfg(feature = "debug")] use tracing::{debug, span, Level}; use Value::Known; @@ -1497,7 +1497,7 @@ impl Optimizer<'_> { } impl VisitMut for Optimizer<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) { diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index abe4b4add8d58..72b7e3fcbb1c9 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -9,7 +9,7 @@ use swc_ecma_usage_analyzer::{ use swc_ecma_utils::{ contains_arguments, contains_this_expr, prepend_stmts, ExprExt, IdentUsageFinder, StmtLike, }; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; #[cfg(feature = "debug")] use tracing::{span, Level}; @@ -2597,7 +2597,7 @@ struct UsageCounter<'a> { } impl Visit for UsageCounter<'_> { - standard_only_visit!(); + noop_visit_type!(); fn visit_ident(&mut self, i: &Ident) { if self.target.sym == i.sym && self.target.ctxt == i.ctxt { diff --git a/crates/swc_ecma_minifier/src/compress/optimize/switches.rs b/crates/swc_ecma_minifier/src/compress/optimize/switches.rs index 95831bf6473b2..a85048d73e7c7 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/switches.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/switches.rs @@ -1,7 +1,7 @@ use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{prepend_stmt, ExprExt, ExprFactory, StmtExt}; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use super::Optimizer; use crate::{compress::util::is_primitive, util::idents_used_by}; @@ -560,7 +560,7 @@ struct BreakFinder { } impl Visit for BreakFinder { - standard_only_visit!(); + noop_visit_type!(); fn visit_break_stmt(&mut self, s: &BreakStmt) { if !self.top_level && s.label.is_none() { diff --git a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs index a1fd5cd49cc9e..b7f01caf5d457 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs @@ -4,7 +4,7 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_usage_analyzer::util::is_global_var_with_pure_property_access; use swc_ecma_utils::{contains_ident_ref, contains_this_expr, ExprExt}; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use super::Optimizer; #[cfg(feature = "debug")] @@ -942,7 +942,7 @@ struct ThisPropertyVisitor { } impl Visit for ThisPropertyVisitor { - standard_only_visit!(); + noop_visit_type!(); fn visit_assign_expr(&mut self, e: &AssignExpr) { if self.should_abort { diff --git a/crates/swc_ecma_minifier/src/compress/optimize/util.rs b/crates/swc_ecma_minifier/src/compress/optimize/util.rs index 63af8a3ef74fd..b336dfc0b79da 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/util.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/util.rs @@ -9,7 +9,7 @@ use swc_common::{collections::AHashSet, util::take::Take, Mark, SyntaxContext, D use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{Parallel, ParallelExt}; use swc_ecma_utils::{collect_decls, ExprCtx, ExprExt, Remapper}; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use tracing::debug; use super::{Ctx, Optimizer}; @@ -307,7 +307,7 @@ enum FinalizerMode { } impl VisitMut for Finalizer<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_callee(&mut self, e: &mut Callee) { e.visit_mut_children_with(self); @@ -483,7 +483,7 @@ impl<'a> NormalMultiReplacer<'a> { } impl VisitMut for NormalMultiReplacer<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { if self.vars.is_empty() { @@ -568,7 +568,7 @@ impl ExprReplacer { } impl VisitMut for ExprReplacer { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { e.visit_mut_children_with(self); diff --git a/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs b/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs index e1a83a55b196f..b905025d3b543 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs @@ -3,7 +3,7 @@ use rayon::prelude::*; use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{extract_var_ids, ExprCtx, ExprExt, StmtExt, StmtLike, Value}; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use super::Pure; use crate::{compress::util::is_fine_for_if_cons, maybe_par, util::ModuleItemExt}; @@ -610,7 +610,7 @@ struct LabelFinder<'a> { found: bool, } impl Visit for LabelFinder<'_> { - standard_only_visit!(); + noop_visit_type!(); fn visit_break_stmt(&mut self, s: &BreakStmt) { match &s.label { diff --git a/crates/swc_ecma_minifier/src/compress/pure/mod.rs b/crates/swc_ecma_minifier/src/compress/pure/mod.rs index 174c3b456884f..9050eeccd22f8 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/mod.rs @@ -7,7 +7,7 @@ use swc_ecma_ast::*; use swc_ecma_transforms_optimization::debug_assert_valid; use swc_ecma_usage_analyzer::marks::Marks; use swc_ecma_utils::ExprCtx; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; #[cfg(feature = "debug")] use tracing::{debug, span, Level}; @@ -254,7 +254,7 @@ impl Pure<'_> { } impl VisitMut for Pure<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_assign_expr(&mut self, e: &mut AssignExpr) { { diff --git a/crates/swc_ecma_minifier/src/compress/pure/vars.rs b/crates/swc_ecma_minifier/src/compress/pure/vars.rs index f8d944ef75008..5f07bf785675a 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/vars.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/vars.rs @@ -3,7 +3,7 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{prepend_stmt, StmtLike}; use swc_ecma_visit::{ - standard_only_visit, standard_only_visit_mut, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; use super::Pure; @@ -300,7 +300,7 @@ pub(super) struct VarWithOutInitCounter { } impl Visit for VarWithOutInitCounter { - standard_only_visit!(); + noop_visit_type!(); fn visit_arrow_expr(&mut self, _: &ArrowExpr) {} @@ -368,7 +368,7 @@ pub(super) struct VarMover { } impl VisitMut for VarMover { - standard_only_visit_mut!(); + noop_visit_mut_type!(); /// Noop fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {} @@ -486,7 +486,7 @@ pub(super) struct VarPrepender { } impl VisitMut for VarPrepender { - standard_only_visit_mut!(); + noop_visit_mut_type!(); /// Noop fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {} diff --git a/crates/swc_ecma_minifier/src/compress/util/mod.rs b/crates/swc_ecma_minifier/src/compress/util/mod.rs index be656e3836a71..1663cef5ce99d 100644 --- a/crates/swc_ecma_minifier/src/compress/util/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/util/mod.rs @@ -8,7 +8,7 @@ use swc_ecma_utils::{number::JsNumber, ExprCtx, ExprExt, IdentUsageFinder, Value #[cfg(feature = "debug")] use swc_ecma_visit::{as_folder, FoldWith}; use swc_ecma_visit::{ - standard_only_visit, standard_only_visit_mut, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; #[cfg(feature = "debug")] @@ -568,7 +568,7 @@ impl VisitMut for ExprReplacer where F: FnMut(&mut Expr), { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { e.visit_mut_children_with(self); @@ -687,7 +687,7 @@ impl UnreachableHandler { } impl VisitMut for UnreachableHandler { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {} @@ -737,7 +737,7 @@ pub struct SuperFinder { } impl Visit for SuperFinder { - standard_only_visit!(); + noop_visit_type!(); /// Don't recurse into constructor fn visit_constructor(&mut self, _: &Constructor) {} diff --git a/crates/swc_ecma_minifier/src/compress/util/tests.rs b/crates/swc_ecma_minifier/src/compress/util/tests.rs index 0e11b519d5f95..a128574a0d890 100644 --- a/crates/swc_ecma_minifier/src/compress/util/tests.rs +++ b/crates/swc_ecma_minifier/src/compress/util/tests.rs @@ -3,7 +3,7 @@ use swc_ecma_ast::*; use swc_ecma_parser::parse_file_as_expr; use swc_ecma_transforms_base::fixer::fixer; use swc_ecma_utils::ExprCtx; -use swc_ecma_visit::{standard_only_visit_mut, FoldWith, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith}; use tracing::{info, warn}; use super::negate_cost; @@ -11,7 +11,7 @@ use crate::{compress::util::negate, debug::dump}; struct UnwrapParen; impl VisitMut for UnwrapParen { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { e.visit_mut_children_with(self); diff --git a/crates/swc_ecma_minifier/src/debug.rs b/crates/swc_ecma_minifier/src/debug.rs index b76e1cfc94dd9..e302dfbd3f8a2 100644 --- a/crates/swc_ecma_minifier/src/debug.rs +++ b/crates/swc_ecma_minifier/src/debug.rs @@ -9,13 +9,13 @@ use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene}; pub use swc_ecma_transforms_optimization::{debug_assert_valid, AssertValid}; use swc_ecma_utils::{drop_span, DropSpan}; -use swc_ecma_visit::{standard_only_visit_mut, FoldWith, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith}; use tracing::debug; pub(crate) struct Debugger {} impl VisitMut for Debugger { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_ident(&mut self, n: &mut Ident) { if !cfg!(feature = "debug") { diff --git a/crates/swc_ecma_minifier/src/lib.rs b/crates/swc_ecma_minifier/src/lib.rs index 30874cce1b834..5839e5cf6a3ec 100644 --- a/crates/swc_ecma_minifier/src/lib.rs +++ b/crates/swc_ecma_minifier/src/lib.rs @@ -53,6 +53,7 @@ use crate::{ mode::{Minification, Mode}, option::{CompressOptions, ExtraOptions, MinifyOptions}, pass::{ + expand_names::name_expander, global_defs, mangle_names::{idents_to_preserve, name_mangler}, mangle_props::mangle_properties, @@ -171,7 +172,7 @@ pub fn optimize( if options.rename && DISABLE_BUGGY_PASSES { // toplevel.figure_out_scope(options.mangle); // TODO: Pass `options.mangle` to name expander. - // n.visit_mut_with(&mut name_expander()); + n.visit_mut_with(&mut name_expander()); } if let Some(ref mut t) = timings { diff --git a/crates/swc_ecma_minifier/src/metadata/mod.rs b/crates/swc_ecma_minifier/src/metadata/mod.rs index 6fa165a5c0f29..f09c24dbb6457 100644 --- a/crates/swc_ecma_minifier/src/metadata/mod.rs +++ b/crates/swc_ecma_minifier/src/metadata/mod.rs @@ -7,7 +7,7 @@ use swc_ecma_ast::*; use swc_ecma_usage_analyzer::marks::Marks; use swc_ecma_utils::NodeIgnoringSpan; use swc_ecma_visit::{ - standard_only_visit, standard_only_visit_mut, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; use crate::option::CompressOptions; @@ -91,7 +91,7 @@ impl InfoMarker<'_> { } impl VisitMut for InfoMarker<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_call_expr(&mut self, n: &mut CallExpr) { n.visit_mut_children_with(self); @@ -234,7 +234,7 @@ struct InfoCollector<'a> { } impl Visit for InfoCollector<'_> { - standard_only_visit!(); + noop_visit_type!(); fn visit_export_decl(&mut self, f: &ExportDecl) { f.visit_children_with(self); diff --git a/crates/swc_ecma_minifier/src/pass/expand_names.rs b/crates/swc_ecma_minifier/src/pass/expand_names.rs new file mode 100644 index 0000000000000..f53646567d6cf --- /dev/null +++ b/crates/swc_ecma_minifier/src/pass/expand_names.rs @@ -0,0 +1,15 @@ +use swc_ecma_visit::{noop_visit_mut_type, VisitMut}; + +/// Currently noop. +/// +/// See: https://github.com/terser/terser/blob/0c5fde1f6951c70b2dfc91f6960dfedbf0e84fef/lib/minify.js#L177-L182 +pub fn name_expander() -> impl VisitMut { + Expander {} +} + +#[derive(Debug)] +struct Expander {} + +impl VisitMut for Expander { + noop_visit_mut_type!(); +} diff --git a/crates/swc_ecma_minifier/src/pass/global_defs.rs b/crates/swc_ecma_minifier/src/pass/global_defs.rs index 518e8ff7c629e..84ea6df30d591 100644 --- a/crates/swc_ecma_minifier/src/pass/global_defs.rs +++ b/crates/swc_ecma_minifier/src/pass/global_defs.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use swc_common::{pass::CompilerPass, EqIgnoreSpan, Mark, SyntaxContext}; use swc_ecma_ast::*; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; pub fn globals_defs( defs: Vec<(Box, Box)>, @@ -35,7 +35,7 @@ impl CompilerPass for GlobalDefs { /// We use [VisitMut] instead of [swc_ecma_visit::Fold] because it's faster. impl VisitMut for GlobalDefs { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) { let old = self.in_lhs_of_assign; diff --git a/crates/swc_ecma_minifier/src/pass/hygiene/vars.rs b/crates/swc_ecma_minifier/src/pass/hygiene/vars.rs index 386116c23669e..7d07fc860a031 100644 --- a/crates/swc_ecma_minifier/src/pass/hygiene/vars.rs +++ b/crates/swc_ecma_minifier/src/pass/hygiene/vars.rs @@ -86,7 +86,7 @@ macro_rules! scoped { } impl Visit for VarAnalyzer<'_> { - standard_only_visit!(); + noop_visit_type!(); fn visit_arrow_expr(&mut self, n: &ArrowExpr) { scoped!(self, n); diff --git a/crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs b/crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs index 3e25cc754d6d6..31bc4aa7e38e3 100644 --- a/crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs +++ b/crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs @@ -3,7 +3,7 @@ use swc_atoms::JsWord; use swc_common::{chain, Mark}; use swc_ecma_ast::*; use swc_ecma_transforms_base::rename::{renamer, Renamer}; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; pub(crate) use self::preserver::idents_to_preserve; use crate::{option::MangleOptions, util::base54::Base54Chars}; @@ -77,7 +77,7 @@ impl LabelMangler { } impl VisitMut for LabelMangler { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_labeled_stmt(&mut self, s: &mut LabeledStmt) { self.mangle(&mut s.label); diff --git a/crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs b/crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs index d9caacadbbd54..ac7984075e24d 100644 --- a/crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs +++ b/crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs @@ -1,7 +1,7 @@ use rustc_hash::FxHashSet; use swc_ecma_ast::*; use swc_ecma_utils::find_pat_ids; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use crate::option::MangleOptions; @@ -35,7 +35,7 @@ impl Preserver { } impl Visit for Preserver { - standard_only_visit!(); + noop_visit_type!(); fn visit_block_stmt(&mut self, n: &BlockStmt) { let old_top_level = self.in_top_level; diff --git a/crates/swc_ecma_minifier/src/pass/mangle_names/private_name.rs b/crates/swc_ecma_minifier/src/pass/mangle_names/private_name.rs index 228e30364fa88..31f4e84dd65f4 100644 --- a/crates/swc_ecma_minifier/src/pass/mangle_names/private_name.rs +++ b/crates/swc_ecma_minifier/src/pass/mangle_names/private_name.rs @@ -1,7 +1,7 @@ use swc_atoms::JsWord; use swc_common::collections::AHashMap; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; use super::Base54Chars; @@ -43,7 +43,7 @@ impl PrivateNameMangler { } impl VisitMut for PrivateNameMangler { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_member_expr(&mut self, n: &mut MemberExpr) { n.obj.visit_mut_with(self); diff --git a/crates/swc_ecma_minifier/src/pass/mangle_props.rs b/crates/swc_ecma_minifier/src/pass/mangle_props.rs index 9e578d7625c75..591e73c9a7c7f 100644 --- a/crates/swc_ecma_minifier/src/pass/mangle_props.rs +++ b/crates/swc_ecma_minifier/src/pass/mangle_props.rs @@ -7,7 +7,7 @@ use swc_ecma_ast::{ CallExpr, Callee, Expr, IdentName, KeyValueProp, Lit, MemberExpr, MemberProp, Program, Prop, PropName, Str, SuperProp, SuperPropExpr, }; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use crate::{ option::ManglePropertiesOptions, @@ -238,7 +238,7 @@ impl Mangler<'_> { } impl VisitMut for Mangler<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_call_expr(&mut self, call: &mut CallExpr) { call.visit_mut_children_with(self); diff --git a/crates/swc_ecma_minifier/src/pass/merge_exports.rs b/crates/swc_ecma_minifier/src/pass/merge_exports.rs index ff3be75ca8864..1701435e2e536 100644 --- a/crates/swc_ecma_minifier/src/pass/merge_exports.rs +++ b/crates/swc_ecma_minifier/src/pass/merge_exports.rs @@ -1,6 +1,6 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use crate::maybe_par; @@ -16,7 +16,7 @@ struct Merger { } impl VisitMut for Merger { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_module_items(&mut self, stmts: &mut Vec) { let was_module = maybe_par!( diff --git a/crates/swc_ecma_minifier/src/pass/mod.rs b/crates/swc_ecma_minifier/src/pass/mod.rs index 6f3eca4421e46..17e07ec819aae 100644 --- a/crates/swc_ecma_minifier/src/pass/mod.rs +++ b/crates/swc_ecma_minifier/src/pass/mod.rs @@ -1,3 +1,4 @@ +pub mod expand_names; pub mod global_defs; pub mod mangle_names; pub mod mangle_props; diff --git a/crates/swc_ecma_minifier/src/pass/postcompress.rs b/crates/swc_ecma_minifier/src/pass/postcompress.rs index 6bbb6d2dd3f4d..7f2941df753f5 100644 --- a/crates/swc_ecma_minifier/src/pass/postcompress.rs +++ b/crates/swc_ecma_minifier/src/pass/postcompress.rs @@ -1,7 +1,7 @@ use swc_common::util::take::Take; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{Parallel, ParallelExt}; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use crate::{maybe_par, option::CompressOptions, LIGHT_TASK_PARALLELS}; @@ -36,7 +36,7 @@ impl Parallel for PostcompressOptimizer<'_> { } impl VisitMut for PostcompressOptimizer<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_export_decl(&mut self, export: &mut ExportDecl) { match &mut export.decl { diff --git a/crates/swc_ecma_minifier/src/pass/precompress.rs b/crates/swc_ecma_minifier/src/pass/precompress.rs index 74f6c8e28efaf..7d414993802f6 100644 --- a/crates/swc_ecma_minifier/src/pass/precompress.rs +++ b/crates/swc_ecma_minifier/src/pass/precompress.rs @@ -1,8 +1,6 @@ -use std::vec::Vec; - use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{Parallel, ParallelExt}; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use crate::HEAVY_TASK_PARALLELS; @@ -26,7 +24,7 @@ impl Parallel for PrecompressOptimizer { } impl VisitMut for PrecompressOptimizer { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_stmts(&mut self, n: &mut Vec) { self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| { diff --git a/crates/swc_ecma_minifier/src/util/base54.rs b/crates/swc_ecma_minifier/src/util/base54.rs index c474588d365cc..b4b9996d57bd7 100644 --- a/crates/swc_ecma_minifier/src/util/base54.rs +++ b/crates/swc_ecma_minifier/src/util/base54.rs @@ -8,7 +8,7 @@ use swc_common::{ }; use swc_ecma_ast::*; use swc_ecma_codegen::{text_writer::WriteJs, Emitter}; -use swc_ecma_visit::{standard_only_visit, visit_obj_and_computed, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, visit_obj_and_computed, Visit, VisitWith}; #[derive(Clone, Copy)] @@ -296,7 +296,7 @@ struct CharFreqAnalyzer<'a> { } impl Visit for CharFreqAnalyzer<'_> { - standard_only_visit!(); + noop_visit_type!(); visit_obj_and_computed!(); diff --git a/crates/swc_ecma_minifier/src/util/mod.rs b/crates/swc_ecma_minifier/src/util/mod.rs index c3796221bb8ec..dbe10aeb0b44c 100644 --- a/crates/swc_ecma_minifier/src/util/mod.rs +++ b/crates/swc_ecma_minifier/src/util/mod.rs @@ -7,7 +7,7 @@ use swc_atoms::Atom; use swc_common::{util::take::Take, Span, Spanned, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{stack_size::maybe_grow_default, ModuleItemLike, StmtLike, Value}; -use swc_ecma_visit::{standard_only_visit, visit_obj_and_computed, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, visit_obj_and_computed, Visit, VisitWith}; pub(crate) mod base54; pub(crate) mod size; @@ -216,7 +216,7 @@ pub(crate) struct LeapFinder { } impl Visit for LeapFinder { - standard_only_visit!(); + noop_visit_type!(); fn visit_await_expr(&mut self, n: &AwaitExpr) { n.visit_children_with(self); @@ -308,7 +308,7 @@ pub struct DeepThisExprVisitor { } impl Visit for DeepThisExprVisitor { - standard_only_visit!(); + noop_visit_type!(); fn visit_this_expr(&mut self, _: &ThisExpr) { self.found = true; @@ -331,7 +331,7 @@ pub(crate) struct IdentUsageCollector { } impl Visit for IdentUsageCollector { - standard_only_visit!(); + noop_visit_type!(); visit_obj_and_computed!(); @@ -399,7 +399,7 @@ pub(crate) struct CapturedIdCollector { } impl Visit for CapturedIdCollector { - standard_only_visit!(); + noop_visit_type!(); visit_obj_and_computed!(); @@ -503,7 +503,7 @@ pub(crate) struct EvalFinder { } impl Visit for EvalFinder { - standard_only_visit!(); + noop_visit_type!(); visit_obj_and_computed!(); diff --git a/crates/swc_ecma_minifier/tests/eval.rs b/crates/swc_ecma_minifier/tests/eval.rs index 3e2859c225cad..77ca2d5ad0f77 100644 --- a/crates/swc_ecma_minifier/tests/eval.rs +++ b/crates/swc_ecma_minifier/tests/eval.rs @@ -10,7 +10,7 @@ use swc_ecma_minifier::{ }; use swc_ecma_parser::{parse_file_as_expr, parse_file_as_module, EsSyntax, Syntax}; use swc_ecma_transforms_base::resolver; -use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use testing::{assert_eq, DebugUsingDisplay}; fn eval(module: &str, expr: &str) -> Option { @@ -169,7 +169,7 @@ impl PartialInliner { } impl VisitMut for PartialInliner { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { e.visit_mut_children_with(self); diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs index c2bb8fe65930e..663edda2fa5ab 100644 --- a/crates/swc_ecma_transforms_optimization/src/const_modules.rs +++ b/crates/swc_ecma_transforms_optimization/src/const_modules.rs @@ -17,7 +17,7 @@ use swc_common::{ use swc_ecma_ast::*; use swc_ecma_parser::parse_file_as_expr; use swc_ecma_utils::drop_span; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; pub fn const_modules( cm: Lrc, @@ -93,7 +93,7 @@ struct Scope { } impl VisitMut for ConstModules { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_module_items(&mut self, n: &mut Vec) { *n = n.take().move_flat_map(|item| match item { diff --git a/crates/swc_ecma_transforms_optimization/src/debug.rs b/crates/swc_ecma_transforms_optimization/src/debug.rs index f9b14f567ab5f..221e9d3e223d4 100644 --- a/crates/swc_ecma_transforms_optimization/src/debug.rs +++ b/crates/swc_ecma_transforms_optimization/src/debug.rs @@ -3,7 +3,7 @@ use std::{fmt::Debug, mem::forget}; use swc_ecma_ast::*; -use swc_ecma_visit::{noop_visit_type, standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; /// Assert in debug mode. This is noop in release build. pub fn debug_assert_valid(node: &N) @@ -29,7 +29,7 @@ impl Drop for Ctx<'_> { pub struct AssertValid; impl Visit for AssertValid { - standard_only_visit!(); + noop_visit_type!(); #[cfg(debug_assertions)] fn visit_expr(&mut self, n: &Expr) { diff --git a/crates/swc_ecma_transforms_optimization/src/inline_globals.rs b/crates/swc_ecma_transforms_optimization/src/inline_globals.rs index 32c5c435bfe12..4b3422c349530 100644 --- a/crates/swc_ecma_transforms_optimization/src/inline_globals.rs +++ b/crates/swc_ecma_transforms_optimization/src/inline_globals.rs @@ -6,7 +6,7 @@ use swc_common::{ use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{ParVisitMut, Parallel}; use swc_ecma_utils::{collect_decls, parallel::cpu_count, NodeIgnoringSpan}; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; /// The key will be compared using [EqIgnoreSpan::eq_ignore_span], and matched /// expressions will be replaced with the value. @@ -63,7 +63,7 @@ impl Parallel for InlineGlobals { } impl VisitMut for InlineGlobals { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, expr: &mut Expr) { if let Expr::Ident(Ident { ref sym, ctxt, .. }) = expr { diff --git a/crates/swc_ecma_transforms_optimization/src/json_parse.rs b/crates/swc_ecma_transforms_optimization/src/json_parse.rs index 1b5522a861d2a..c60e319aa0f0c 100644 --- a/crates/swc_ecma_transforms_optimization/src/json_parse.rs +++ b/crates/swc_ecma_transforms_optimization/src/json_parse.rs @@ -3,7 +3,7 @@ use swc_common::{util::take::Take, Spanned, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::{calc_literal_cost, member_expr, ExprFactory}; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; /// Transform to optimize performance of literals. /// @@ -52,7 +52,7 @@ impl Default for JsonParse { } impl VisitMut for JsonParse { - standard_only_visit_mut!(); + noop_visit_mut_type!(); /// Handles parent expressions before child expressions. fn visit_mut_expr(&mut self, expr: &mut Expr) { diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs index e6adb60134935..eb959e085e273 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs @@ -15,8 +15,7 @@ use swc_ecma_utils::{ StmtExt, StmtLike, Value::Known, }; use swc_ecma_visit::{ - as_folder, standard_only_visit, standard_only_visit_mut, Visit, VisitMut, VisitMutWith, - VisitWith, + as_folder, noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; use tracing::{debug, trace}; @@ -73,7 +72,7 @@ impl Parallel for Remover { } impl VisitMut for Remover { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { e.visit_mut_children_with(self); @@ -1906,7 +1905,7 @@ fn check_for_stopper(s: &[Stmt], only_conditional: bool) -> bool { } impl Visit for Visitor { - standard_only_visit!(); + noop_visit_type!(); fn visit_switch_case(&mut self, node: &SwitchCase) { let old = self.in_cond; diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/const_propagation.rs b/crates/swc_ecma_transforms_optimization/src/simplify/const_propagation.rs index aff4d4200dfbd..bf588f63810f6 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/const_propagation.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/const_propagation.rs @@ -2,7 +2,7 @@ use swc_common::{collections::AHashMap, util::take::Take}; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; /// This pass is kind of inliner, but it's far faster. pub fn constant_propagation() -> impl 'static + Fold + VisitMut { @@ -38,7 +38,7 @@ impl<'a> Scope<'a> { } impl VisitMut for ConstPropagation<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); /// No-op fn visit_mut_assign_expr(&mut self, _: &mut AssignExpr) {} diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs index 3e7b0260607fb..56d85c701c997 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs @@ -19,8 +19,7 @@ use swc_ecma_utils::{ collect_decls, find_pat_ids, ExprCtx, ExprExt, IsEmpty, ModuleItemLike, StmtLike, }; use swc_ecma_visit::{ - as_folder, standard_only_visit, standard_only_visit_mut, Fold, Visit, VisitMut, VisitMutWith, - VisitWith, + as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_fast_graph::digraph::FastDiGraphMap; use tracing::{debug, span, Level}; @@ -356,7 +355,7 @@ impl Analyzer<'_> { } impl Visit for Analyzer<'_> { - standard_only_visit!(); + noop_visit_type!(); fn visit_callee(&mut self, n: &Callee) { n.visit_children_with(self); @@ -659,7 +658,7 @@ impl TreeShaker { } impl VisitMut for TreeShaker { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) { n.visit_mut_children_with(self); diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs index 837d215b875b3..35d84224d27f2 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs @@ -16,7 +16,7 @@ use swc_ecma_utils::{ is_literal, number::JsNumber, prop_name_eq, to_int32, BoolType, ExprCtx, ExprExt, NullType, NumberType, ObjectType, StringType, SymbolType, UndefinedType, Value, }; -use swc_ecma_visit::{as_folder, standard_only_visit_mut, VisitMut, VisitMutWith}; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith}; use Value::{Known, Unknown}; use crate::debug::debug_assert_valid; @@ -1213,7 +1213,7 @@ impl SimplifyExpr { } impl VisitMut for SimplifyExpr { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) { let old = self.is_modifying; diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/inlining/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/inlining/mod.rs index 499d5178b6bc8..90341b5f7e433 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/inlining/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/inlining/mod.rs @@ -8,8 +8,8 @@ use swc_ecma_ast::*; use swc_ecma_transforms_base::{pass::RepeatedJsPass, scope::IdentType}; use swc_ecma_utils::{contains_this_expr, find_pat_ids}; use swc_ecma_visit::{ - as_folder, standard_only_visit, standard_only_visit_mut, visit_obj_and_computed, Visit, - VisitMut, VisitMutWith, VisitWith, + as_folder, noop_visit_mut_type, noop_visit_type, visit_obj_and_computed, Visit, VisitMut, + VisitMutWith, VisitWith, }; use tracing::{span, Level}; @@ -103,7 +103,7 @@ impl Inlining<'_> { } impl VisitMut for Inlining<'_> { - standard_only_visit_mut!(); + noop_visit_mut_type!(); fn visit_mut_arrow_expr(&mut self, node: &mut ArrowExpr) { self.visit_with_child(ScopeKind::Fn { named: false }, node) @@ -739,7 +739,7 @@ struct IdentListVisitor<'a, 'b> { } impl Visit for IdentListVisitor<'_, '_> { - standard_only_visit!(); + noop_visit_type!(); visit_obj_and_computed!(); @@ -754,7 +754,7 @@ struct WriteVisitor<'a, 'b> { } impl Visit for WriteVisitor<'_, '_> { - standard_only_visit!(); + noop_visit_type!(); visit_obj_and_computed!(); diff --git a/crates/swc_ecma_transforms_optimization/tests/__swc_snapshots__/tests/simplify_dce.rs/issue_1150_1.js b/crates/swc_ecma_transforms_optimization/tests/__swc_snapshots__/tests/simplify_dce.rs/issue_1150_1.js index edb596654fc14..8e3efc894c5f8 100644 --- a/crates/swc_ecma_transforms_optimization/tests/__swc_snapshots__/tests/simplify_dce.rs/issue_1150_1.js +++ b/crates/swc_ecma_transforms_optimization/tests/__swc_snapshots__/tests/simplify_dce.rs/issue_1150_1.js @@ -1,5 +1,5 @@ class A { - constructor(o = {}){ + constructor(o: AOptions = {}){ const { a = defaultA, c } = o; this.#a = a; this.#c = c; diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/input.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/input.js new file mode 100644 index 0000000000000..d69b170101553 --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/input.js @@ -0,0 +1,7 @@ +function A() { + return "a"; +} +const b = "b"; +export function c() { + return ; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/output.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/output.js new file mode 100644 index 0000000000000..dcc099a5ba651 --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property/output.js @@ -0,0 +1,6 @@ +function A() { + return "a"; +} +export function c() { + return ; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/input.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/input.js new file mode 100644 index 0000000000000..450635316d530 --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/input.js @@ -0,0 +1,8 @@ +function A() { + return "a"; +} +const b = "b"; +const c = "c"; +export function d() { + return ; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/output.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/output.js new file mode 100644 index 0000000000000..ef1f791b5cf20 --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/property2/output.js @@ -0,0 +1,6 @@ +function A() { + return "a"; +} +export function d() { + return ; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/input.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/input.js new file mode 100644 index 0000000000000..a5ee2839c7e87 --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/input.js @@ -0,0 +1,8 @@ +function A() { + return "a"; +} +const b = "b"; +const c = "c"; +export function d() { + return {c}; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/output.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/output.js new file mode 100644 index 0000000000000..a5ee2839c7e87 --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/spread/output.js @@ -0,0 +1,8 @@ +function A() { + return "a"; +} +const b = "b"; +const c = "c"; +export function d() { + return {c}; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/input.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/input.js new file mode 100644 index 0000000000000..819e09994e12a --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/input.js @@ -0,0 +1,8 @@ +function A() { + return "a"; +} +const b = "b"; +const c = "c"; +export function d() { + return {c}; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/output.js b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/output.js new file mode 100644 index 0000000000000..819e09994e12a --- /dev/null +++ b/crates/swc_ecma_transforms_optimization/tests/dce-jsx/var/output.js @@ -0,0 +1,8 @@ +function A() { + return "a"; +} +const b = "b"; +const c = "c"; +export function d() { + return {c}; +} diff --git a/crates/swc_ecma_transforms_optimization/tests/fixture.rs b/crates/swc_ecma_transforms_optimization/tests/fixture.rs index 3576f0fa1b444..9ecb1adb07f0f 100644 --- a/crates/swc_ecma_transforms_optimization/tests/fixture.rs +++ b/crates/swc_ecma_transforms_optimization/tests/fixture.rs @@ -54,6 +54,23 @@ fn dce_repeated(input: PathBuf) { ); } +#[testing::fixture("tests/dce-jsx/**/input.js")] +fn dce_jsx(input: PathBuf) { + let output = input.with_file_name("output.js"); + + test_fixture( + Syntax::Es(EsSyntax { + decorators: true, + jsx: true, + ..Default::default() + }), + &|t| chain!(remover(t), dce(Default::default(), Mark::new())), + &input, + &output, + Default::default(), + ); +} + #[testing::fixture("tests/expr-simplifier/**/input.js")] fn expr(input: PathBuf) { let output = input.with_file_name("output.js"); diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify.rs b/crates/swc_ecma_transforms_optimization/tests/simplify.rs index 285c9f5d4278c..eff3c0596ec50 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify.rs @@ -500,7 +500,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), class_properties( Some(t.comments.clone()), class_properties::Config { @@ -553,7 +553,7 @@ test!( chain!( decorators(Default::default()), resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), class_properties( Some(t.comments.clone()), Default::default(), diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs b/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs index 064072a095007..6b2345308deec 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs @@ -382,7 +382,7 @@ test!( use_define_for_class_fields: false, }), resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), tr() ) }, @@ -414,7 +414,7 @@ test!( use_define_for_class_fields: false, }), resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), tr() ) }, @@ -439,7 +439,7 @@ test!( issue_1150_1, " class A { - constructor(o = {}) { + constructor(o: AOptions = {}) { const { a = defaultA, c, @@ -471,7 +471,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), class_properties( Some(t.comments.clone()), class_properties::Config { @@ -511,7 +511,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), class_properties( Some(t.comments.clone()), class_properties::Config { @@ -565,7 +565,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), tr(), ) }, @@ -594,7 +594,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), - strip(unresolved_mark, top_level_mark), + strip(top_level_mark), class_properties( Some(t.comments.clone()), class_properties::Config { diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs b/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs index e98a6cc436ff4..895222658174a 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs @@ -9,13 +9,12 @@ use swc_ecma_transforms_testing::test; use swc_ecma_transforms_typescript::typescript; use swc_ecma_visit::Fold; -fn simple_strip(unresolved_mark: Mark, top_level_mark: Mark) -> impl Fold { +fn simple_strip(top_level_mark: Mark) -> impl Fold { typescript( typescript::Config { no_empty_export: true, ..Default::default() }, - unresolved_mark, top_level_mark, ) } @@ -2067,7 +2066,7 @@ test!( let top_level_mark = Mark::fresh(Mark::root()); chain!( resolver(unresolved_mark, top_level_mark, false), - simple_strip(unresolved_mark, top_level_mark), + simple_strip(top_level_mark), class_properties( Some(t.comments.clone()), class_properties::Config { @@ -2106,7 +2105,7 @@ test!( let top_level_mark = Mark::new(); chain!( resolver(unresolved_mark, top_level_mark, false), - simple_strip(unresolved_mark, top_level_mark), + simple_strip(top_level_mark), class_properties( Some(t.comments.clone()), class_properties::Config { @@ -2159,7 +2158,7 @@ test!( let top_level_mark = Mark::new(); chain!( resolver(unresolved_mark, top_level_mark, false), - simple_strip(unresolved_mark, top_level_mark), + simple_strip(top_level_mark), inlining(Default::default()) ) }, @@ -2193,7 +2192,7 @@ test!( let top_level_mark = Mark::new(); chain!( resolver(unresolved_mark, top_level_mark, false), - simple_strip(unresolved_mark, top_level_mark), + simple_strip(top_level_mark), inlining(Default::default()) ) }, diff --git a/crates/swc_ecma_transforms_react/src/refresh/tests.rs b/crates/swc_ecma_transforms_react/src/refresh/tests.rs index f8012a87d33e4..464d812b4b2b0 100644 --- a/crates/swc_ecma_transforms_react/src/refresh/tests.rs +++ b/crates/swc_ecma_transforms_react/src/refresh/tests.rs @@ -3,7 +3,6 @@ use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::{test, Tester}; use super::*; -use crate::jsx; fn tr(t: &mut Tester) -> impl Fold { let unresolved_mark = Mark::new(); @@ -563,6 +562,12 @@ test!( top_level_mark, unresolved_mark ), + common_js( + unresolved_mark, + Default::default(), + Default::default(), + Some(t.comments.clone()) + ) ) }, include_hook_signature_in_commonjs, diff --git a/crates/swc_ecma_transforms_react/tests/__swc_snapshots__/src/refresh/tests.rs/include_hook_signature_in_commonjs.js b/crates/swc_ecma_transforms_react/tests/__swc_snapshots__/src/refresh/tests.rs/include_hook_signature_in_commonjs.js index 86d2eb956f997..895b5116401f7 100644 --- a/crates/swc_ecma_transforms_react/tests/__swc_snapshots__/src/refresh/tests.rs/include_hook_signature_in_commonjs.js +++ b/crates/swc_ecma_transforms_react/tests/__swc_snapshots__/src/refresh/tests.rs/include_hook_signature_in_commonjs.js @@ -6,6 +6,9 @@ export default function App() { const bar = useFancyState(); const foo = useFoo(); return /*#__PURE__*/ React.createElement("h1", null, bar); + const bar = (0, _hooks.useFancyState)(); + const foo = (0, _foo.default)(); + return

{bar}

; } _s(App, "useFancyState{bar}\nuseFoo{foo}", false, function() { return [ diff --git a/crates/swc_ecma_usage_analyzer/src/alias/mod.rs b/crates/swc_ecma_usage_analyzer/src/alias/mod.rs index bc7db73f0aba4..29c2d13fa3e9d 100644 --- a/crates/swc_ecma_usage_analyzer/src/alias/mod.rs +++ b/crates/swc_ecma_usage_analyzer/src/alias/mod.rs @@ -4,7 +4,7 @@ use rustc_hash::FxHashSet; use swc_common::{collections::AHashSet, SyntaxContext}; use swc_ecma_ast::*; use swc_ecma_utils::{collect_decls, BindingCollector}; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; pub use self::ctx::Ctx; use crate::{marks::Marks, util::is_global_var_with_pure_property_access}; @@ -120,7 +120,7 @@ impl InfectionCollector<'_> { } impl Visit for InfectionCollector<'_> { - standard_only_visit!(); + noop_visit_type!(); fn visit_bin_expr(&mut self, e: &BinExpr) { match e.op { diff --git a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs index 37f8ffc70bf70..3f8ddb4b23e06 100644 --- a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs +++ b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs @@ -1,7 +1,7 @@ use swc_common::{collections::AHashMap, SyntaxContext}; use swc_ecma_ast::*; use swc_ecma_utils::{find_pat_ids, ExprCtx, ExprExt, IsEmpty, StmtExt}; -use swc_ecma_visit::{standard_only_visit, Visit, VisitWith}; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use swc_timer::timer; pub use self::ctx::Ctx; @@ -215,7 +215,7 @@ impl Visit for UsageAnalyzer where S: Storage, { - standard_only_visit!(); + noop_visit_type!(); fn visit_array_lit(&mut self, n: &ArrayLit) { let ctx = Ctx {