diff --git a/.changeset/few-pigs-shout.md b/.changeset/few-pigs-shout.md new file mode 100644 index 000000000000..c7cafce9af0b --- /dev/null +++ b/.changeset/few-pigs-shout.md @@ -0,0 +1,7 @@ +--- +swc_visit: major +swc_common: major +swc_plugin_proxy: major +--- + +perf(visit): Introduce `Pass` API and adjust visitor APIs for it diff --git a/Cargo.lock b/Cargo.lock index a498f46d6389..e63664a0f0c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4372,6 +4372,7 @@ dependencies = [ "string_enum", "swc_atoms", "swc_common", + "swc_visit", "unicode-id-start", ] diff --git a/bindings/binding_minifier_node/src/minify.rs b/bindings/binding_minifier_node/src/minify.rs index 051e2d2fe5e8..7cb2181bbdbe 100644 --- a/bindings/binding_minifier_node/src/minify.rs +++ b/bindings/binding_minifier_node/src/minify.rs @@ -184,7 +184,7 @@ fn do_work( let is_mangler_enabled = min_opts.mangle.is_some(); let module = { - let module = module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false)); + let module = module.apply(resolver(unresolved_mark, top_level_mark, false)); let mut module = swc_core::ecma::minifier::optimize( module, diff --git a/crates/binding_macros/src/wasm.rs b/crates/binding_macros/src/wasm.rs index e4655fc0de27..c75e0f1e6a49 100644 --- a/crates/binding_macros/src/wasm.rs +++ b/crates/binding_macros/src/wasm.rs @@ -25,9 +25,11 @@ pub use swc_common::{ }; use swc_common::{sync::Lrc, FilePathMapping, SourceMap}; #[doc(hidden)] +pub use swc_ecma_ast::noop_pass; +#[doc(hidden)] pub use swc_ecma_ast::{EsVersion, Program}; #[doc(hidden)] -pub use swc_ecma_transforms::{pass::noop, resolver}; +pub use swc_ecma_transforms::resolver; #[doc(hidden)] pub use swc_ecma_visit::VisitMutWith; #[doc(hidden)] @@ -293,7 +295,7 @@ macro_rules! build_print { #[macro_export] macro_rules! build_transform_sync { ($(#[$m:meta])*) => { - build_transform_sync!($(#[$m])*, |_| $crate::wasm::noop(), |_| $crate::wasm::noop(), Default::default()); + build_transform_sync!($(#[$m])*, |_| $crate::wasm::noop_pass(), |_| $crate::wasm::noop_pass(), Default::default()); }; ($(#[$m:meta])*, $before_pass: expr, $after_pass: expr) => { build_transform_sync!($(#[$m])*, $before_pass, $after_pass, Default::default()); diff --git a/crates/swc/benches/typescript.rs b/crates/swc/benches/typescript.rs index a9e9ab3a15bc..e9cf30ae3cf1 100644 --- a/crates/swc/benches/typescript.rs +++ b/crates/swc/benches/typescript.rs @@ -14,7 +14,6 @@ use swc_compiler_base::PrintArgs; use swc_ecma_ast::{EsVersion, Program}; use swc_ecma_parser::Syntax; use swc_ecma_transforms::{fixer, resolver, typescript}; -use swc_ecma_visit::FoldWith; static SOURCE: &str = include_str!("assets/Observable.ts"); @@ -52,8 +51,8 @@ fn as_es(c: &swc::Compiler) -> Program { let top_level_mark = Mark::new(); program - .fold_with(&mut resolver(unresolved_mark, top_level_mark, true)) - .fold_with(&mut typescript::strip(unresolved_mark, top_level_mark)) + .apply(&mut resolver(unresolved_mark, top_level_mark, true)) + .apply(&mut typescript::strip(unresolved_mark, top_level_mark)) } fn base_tr_group(c: &mut Criterion) { @@ -73,7 +72,7 @@ fn base_tr_fixer(b: &mut Bencher) { GLOBALS.set(&Default::default(), || { let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone())); black_box(c.run_transform(&handler, true, || { - module.clone().fold_with(&mut fixer(Some(c.comments()))) + module.clone().apply(&mut fixer(Some(c.comments()))) })) }) }); diff --git a/crates/swc/src/builder.rs b/crates/swc/src/builder.rs index cf91fc8fc4a3..0b06b2961eaa 100644 --- a/crates/swc/src/builder.rs +++ b/crates/swc/src/builder.rs @@ -5,10 +5,9 @@ use either::Either; use rustc_hash::FxHashMap; use swc_atoms::JsWord; use swc_common::{ - chain, comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark, - SourceMap, + comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark, SourceMap, }; -use swc_ecma_ast::{EsVersion, Module, Script}; +use swc_ecma_ast::{EsVersion, Module, Pass, Script}; use swc_ecma_minifier::option::{terser::TerserTopLevelOptions, MinifyOptions}; use swc_ecma_parser::Syntax; use swc_ecma_transforms::{ @@ -19,15 +18,15 @@ use swc_ecma_transforms::{ hygiene::{self, hygiene_with_config}, modules::{self, path::ImportResolver}, optimization::const_modules, - pass::Optional, resolver, Assumptions, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; +use swc_visit::Optional; use crate::config::{GlobalPassOption, JsMinifyOptions, ModuleConfig}; /// Builder is used to create a high performance `Compiler`. -pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> { +pub struct PassBuilder<'a, 'b, P: Pass> { cm: &'a Arc, handler: &'b Handler, env: Option, @@ -48,7 +47,7 @@ pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> { regenerator: regenerator::Config, } -impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { +impl<'a, 'b, P: Pass> PassBuilder<'a, 'b, P> { pub fn new( cm: &'a Arc, handler: &'b Handler, @@ -76,11 +75,11 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { } } - pub fn then(self, next: N) -> PassBuilder<'a, 'b, swc_visit::AndThen> + pub fn then(self, next: N) -> PassBuilder<'a, 'b, (P, N)> where - N: swc_ecma_visit::Fold, + N: Pass, { - let pass = chain!(self.pass, next); + let pass = (self.pass, next); PassBuilder { cm: self.cm, handler: self.handler, @@ -126,15 +125,12 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { pub fn const_modules( self, globals: FxHashMap>, - ) -> PassBuilder<'a, 'b, impl swc_ecma_visit::Fold> { + ) -> PassBuilder<'a, 'b, (P, impl Pass)> { let cm = self.cm.clone(); self.then(const_modules(cm, globals)) } - pub fn inline_globals( - self, - c: GlobalPassOption, - ) -> PassBuilder<'a, 'b, impl swc_ecma_visit::Fold> { + pub fn inline_globals(self, c: GlobalPassOption) -> PassBuilder<'a, 'b, (P, impl Pass)> { let pass = c.build(self.cm, self.handler); self.then(pass) } @@ -172,7 +168,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { module: Option, comments: Option<&'cmt dyn Comments>, resolver: Option<(FileName, Arc)>, - ) -> impl 'cmt + swc_ecma_visit::Fold + ) -> impl 'cmt + Pass where P: 'cmt, { @@ -206,7 +202,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { feature_flag = enable_available_feature_from_es_version(self.target); - Either::Right(chain!( + Either::Right(( Optional::new( compat::class_fields_use_set::class_fields_use_set(assumptions.pure_getters), assumptions.set_public_class_fields, @@ -221,60 +217,60 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { no_document_all: assumptions.no_document_all, static_blocks_mark: Mark::new(), pure_getter: assumptions.pure_getters, - } + }, }, - self.unresolved_mark + self.unresolved_mark, ), - should_enable(self.target, EsVersion::Es2022) + should_enable(self.target, EsVersion::Es2022), ), Optional::new( compat::es2021::es2021(), - should_enable(self.target, EsVersion::Es2021) + should_enable(self.target, EsVersion::Es2021), ), Optional::new( compat::es2020::es2020( compat::es2020::Config { nullish_coalescing: compat::es2020::nullish_coalescing::Config { - no_document_all: assumptions.no_document_all + no_document_all: assumptions.no_document_all, }, optional_chaining: compat::es2020::optional_chaining::Config { no_document_all: assumptions.no_document_all, - pure_getter: assumptions.pure_getters - } + pure_getter: assumptions.pure_getters, + }, }, - self.unresolved_mark + self.unresolved_mark, ), - should_enable(self.target, EsVersion::Es2020) + should_enable(self.target, EsVersion::Es2020), ), Optional::new( compat::es2019::es2019(), - should_enable(self.target, EsVersion::Es2019) + should_enable(self.target, EsVersion::Es2019), ), Optional::new( compat::es2018(compat::es2018::Config { object_rest_spread: compat::es2018::object_rest_spread::Config { no_symbol: assumptions.object_rest_no_symbols, set_property: assumptions.set_spread_properties, - pure_getters: assumptions.pure_getters - } + pure_getters: assumptions.pure_getters, + }, }), - should_enable(self.target, EsVersion::Es2018) + should_enable(self.target, EsVersion::Es2018), ), Optional::new( compat::es2017( compat::es2017::Config { async_to_generator: compat::es2017::async_to_generator::Config { ignore_function_name: assumptions.ignore_function_name, - ignore_function_length: assumptions.ignore_function_length + ignore_function_length: assumptions.ignore_function_length, }, }, - self.unresolved_mark + self.unresolved_mark, ), - should_enable(self.target, EsVersion::Es2017) + should_enable(self.target, EsVersion::Es2017), ), Optional::new( compat::es2016(), - should_enable(self.target, EsVersion::Es2016) + should_enable(self.target, EsVersion::Es2016), ), Optional::new( compat::es2015( @@ -286,36 +282,36 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { no_class_calls: assumptions.no_class_calls, set_class_methods: assumptions.set_class_methods, super_is_callable_constructor: assumptions - .super_is_callable_constructor + .super_is_callable_constructor, }, computed_props: compat::es2015::computed_props::Config { - loose: self.loose + loose: self.loose, }, for_of: compat::es2015::for_of::Config { assume_array: false, - loose: self.loose + loose: self.loose, }, spread: compat::es2015::spread::Config { loose: self.loose }, destructuring: compat::es2015::destructuring::Config { - loose: self.loose + loose: self.loose, }, regenerator: self.regenerator, template_literal: compat::es2015::template_literal::Config { ignore_to_primitive: assumptions.ignore_to_primitive_hint, - mutable_template: assumptions.mutable_template_object + mutable_template: assumptions.mutable_template_object, }, parameters: compat::es2015::parameters::Config { ignore_function_length: assumptions.ignore_function_length, }, - typescript: syntax.typescript() - } + typescript: syntax.typescript(), + }, ), - should_enable(self.target, EsVersion::Es2015) + should_enable(self.target, EsVersion::Es2015), ), Optional::new( compat::es3(true), - cfg!(feature = "es3") && self.target == EsVersion::Es3 - ) + cfg!(feature = "es3") && self.target == EsVersion::Es3, + ), )) }; @@ -325,21 +321,21 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { .map(|v| v.mangle.is_obj() || v.mangle.is_true()) .unwrap_or(false); - chain!( + ( self.pass, Optional::new( paren_remover(comments.map(|v| v as &dyn Comments)), - self.fixer + self.fixer, ), compat_pass, // module / helper Optional::new( modules::import_analysis::import_analyzer(import_interop, ignore_dynamic), - need_analyzer + need_analyzer, ), Optional::new( helpers::inject_helpers(self.unresolved_mark), - self.inject_helpers + self.inject_helpers, ), ModuleConfig::build( self.cm.clone(), @@ -349,7 +345,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { feature_flag, resolver, ), - as_folder(MinifierPass { + visit_mut_pass(MinifierPass { options: self.minify, cm: self.cm.clone(), comments, @@ -360,7 +356,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { top_level_mark: self.top_level_mark, ..self.hygiene.clone().unwrap_or_default() }), - self.hygiene.is_some() && !is_mangler_enabled + self.hygiene.is_some() && !is_mangler_enabled, ), Optional::new(fixer(comments.map(|v| v as &dyn Comments)), self.fixer), ) diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 3d9854a5550c..39c2fd929560 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -7,7 +7,6 @@ use std::{ use anyhow::{bail, Context, Error}; use dashmap::DashMap; -use either::Either; use indexmap::IndexMap; use once_cell::sync::Lazy; use rustc_hash::FxHashMap; @@ -17,7 +16,6 @@ use swc_cached::regex::CachedRegex; #[allow(unused)] use swc_common::plugin::metadata::TransformPluginMetadataContext; use swc_common::{ - chain, collections::{AHashMap, AHashSet, ARandomState}, comments::{Comments, SingleThreadedComments}, errors::Handler, @@ -28,7 +26,7 @@ use swc_config::{ config_types::{BoolConfig, BoolOr, BoolOrDataConfig, MergingOption}, merge::Merge, }; -use swc_ecma_ast::{EsVersion, Expr, Program}; +use swc_ecma_ast::{noop_pass, EsVersion, Expr, Pass, Program}; use swc_ecma_ext_transforms::jest; use swc_ecma_lints::{ config::LintConfig, @@ -53,7 +51,6 @@ use swc_ecma_transforms::{ EsModuleConfig, }, optimization::{const_modules, json_parse, simplifier}, - pass::{noop, Optional}, proposals::{ decorators, explicit_resource_management::explicit_resource_management, export_default_from, import_assertions, @@ -70,7 +67,8 @@ use swc_ecma_transforms_optimization::{ GlobalExprMap, }; use swc_ecma_utils::NodeIgnoringSpan; -use swc_ecma_visit::{Fold, VisitMutWith}; +use swc_ecma_visit::VisitMutWith; +use swc_visit::Optional; pub use crate::plugin::PluginConfig; use crate::{ @@ -231,9 +229,9 @@ impl Options { config: Option, comments: Option<&'a SingleThreadedComments>, custom_before_pass: impl FnOnce(&Program) -> P, - ) -> Result>, Error> + ) -> Result>, Error> where - P: 'a + swc_ecma_visit::Fold, + P: 'a + Pass, { let mut cfg = self.config.clone(); @@ -463,11 +461,11 @@ impl Options { }; let json_parse_pass = { - if let Some(ref cfg) = optimizer.as_ref().and_then(|v| v.jsonify) { - Either::Left(json_parse(cfg.min_cost)) - } else { - Either::Right(noop()) - } + optimizer + .as_ref() + .and_then(|v| v.jsonify) + .as_ref() + .map(|cfg| json_parse(cfg.min_cost)) }; let simplifier_pass = { @@ -475,12 +473,12 @@ impl Options { match opts { SimplifyOption::Bool(allow_simplify) => { if *allow_simplify { - Either::Left(simplifier(top_level_mark, Default::default())) + Some(simplifier(top_level_mark, Default::default())) } else { - Either::Right(noop()) + None } } - SimplifyOption::Json(cfg) => Either::Left(simplifier( + SimplifyOption::Json(cfg) => Some(simplifier( top_level_mark, SimplifyConfig { dce: DceConfig { @@ -493,27 +491,25 @@ impl Options { )), } } else { - Either::Right(noop()) + None } }; let optimization = { - if let Some(opts) = optimizer.and_then(|o| o.globals) { - Either::Left(opts.build(cm, handler)) - } else { - Either::Right(noop()) - } + optimizer + .and_then(|o| o.globals) + .map(|opts| opts.build(cm, handler)) }; let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark); let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark); - let pass = chain!( + let pass = ( const_modules, optimization, Optional::new(export_default_from(), syntax.export_default_from()), simplifier_pass, - json_parse_pass + json_parse_pass, ); let import_export_assign_config = match cfg.module { @@ -582,7 +578,7 @@ impl Options { let disable_all_lints = experimental.disable_all_lints.into_bool(); #[cfg(feature = "plugin")] - let plugin_transforms: Box = { + let plugin_transforms: Box = { let transform_filename = match base { FileName::Real(path) => path.as_os_str().to_str().map(String::from), FileName::Custom(filename) => Some(filename.to_owned()), @@ -668,25 +664,25 @@ impl Options { }; #[cfg(not(feature = "plugin"))] - let plugin_transforms: Box = { + let plugin_transforms: Box = { if experimental.plugins.is_some() { handler.warn( "Plugin is not supported with current @swc/core. Plugin transform will be \ skipped.", ); } - Box::new(noop()) + Box::new(noop_pass()) }; let mut plugin_transforms = Some(plugin_transforms); - let pass: Box = if experimental + let pass: Box = if experimental .disable_builtin_transforms_for_internal_testing .into_bool() { plugin_transforms.unwrap() } else { - let decorator_pass: Box = + let decorator_pass: Box = match transform.decorator_version.unwrap_or_default() { DecoratorVersion::V202112 => Box::new(decorators(decorators::Config { legacy: transform.legacy_decorator.into_bool(), @@ -699,80 +695,86 @@ impl Options { DecoratorVersion::V202311 => todo!("2023-11 decorator"), }; - Box::new(chain!( - if experimental.run_plugin_first.into_bool() { - option_pass(plugin_transforms.take()) - } else { - Box::new(noop()) - }, - Optional::new( - lint_to_fold(swc_ecma_lints::rules::all(LintParams { - program: &program, - lint_config: &lints, - top_level_ctxt, - unresolved_ctxt, - es_version, - source_map: cm.clone(), - })), - !disable_all_lints - ), - // Decorators may use type information - Optional::new(decorator_pass, syntax.decorators()), - Optional::new( - explicit_resource_management(), - syntax.explicit_resource_management() + Box::new(( + ( + if experimental.run_plugin_first.into_bool() { + plugin_transforms.take() + } else { + None + }, + Optional::new( + lint_to_fold(swc_ecma_lints::rules::all(LintParams { + program: &program, + lint_config: &lints, + top_level_ctxt, + unresolved_ctxt, + es_version, + source_map: cm.clone(), + })), + !disable_all_lints, + ), + // Decorators may use type information + Optional::new(decorator_pass, syntax.decorators()), + Optional::new( + explicit_resource_management(), + syntax.explicit_resource_management(), + ), ), // The transform strips import assertions, so it's only enabled if // keep_import_assertions is false. - Optional::new(import_assertions(), !keep_import_attributes), - Optional::new( - typescript::tsx::>( - cm.clone(), - typescript::Config { - import_export_assign_config, - verbatim_module_syntax, - ..Default::default() - }, - typescript::TsxConfig { - pragma: Some( - transform - .react - .pragma - .clone() - .unwrap_or_else(default_pragma) - ), - pragma_frag: Some( - transform - .react - .pragma_frag - .clone() - .unwrap_or_else(default_pragma_frag) - ), - }, - comments.map(|v| v as _), - unresolved_mark, - top_level_mark + ( + Optional::new(import_assertions(), !keep_import_attributes), + Optional::new( + typescript::tsx::>( + cm.clone(), + typescript::Config { + import_export_assign_config, + verbatim_module_syntax, + ..Default::default() + }, + typescript::TsxConfig { + pragma: Some( + transform + .react + .pragma + .clone() + .unwrap_or_else(default_pragma), + ), + pragma_frag: Some( + transform + .react + .pragma_frag + .clone() + .unwrap_or_else(default_pragma_frag), + ), + }, + comments.map(|v| v as _), + unresolved_mark, + top_level_mark, + ), + syntax.typescript(), ), - syntax.typescript() ), - option_pass(plugin_transforms.take()), - custom_before_pass(&program), - // handle jsx - Optional::new( - react::react::<&dyn Comments>( - cm.clone(), - comments.map(|v| v as _), - transform.react, - top_level_mark, - unresolved_mark + ( + plugin_transforms.take(), + custom_before_pass(&program), + // handle jsx + Optional::new( + react::react::<&dyn Comments>( + cm.clone(), + comments.map(|v| v as _), + transform.react, + top_level_mark, + unresolved_mark, + ), + syntax.jsx(), + ), + pass, + Optional::new(jest::jest(), transform.hidden.jest.into_bool()), + Optional::new( + dropped_comments_preserver(comments.cloned()), + preserve_all_comments, ), - syntax.jsx() - ), - pass, - Optional::new(jest::jest(), transform.hidden.jest.into_bool()), - Optional::new( - dropped_comments_preserver(comments.cloned()), - preserve_all_comments ), )) }; @@ -1081,7 +1083,7 @@ impl Config { /// One `BuiltConfig` per a directory with swcrc #[non_exhaustive] -pub struct BuiltInput { +pub struct BuiltInput { pub program: Program, pub pass: P, pub syntax: Syntax, @@ -1113,11 +1115,11 @@ pub struct BuiltInput { impl

BuiltInput

where - P: swc_ecma_visit::Fold, + P: Pass, { pub fn with_pass(self, map: impl FnOnce(P) -> N) -> BuiltInput where - N: swc_ecma_visit::Fold, + N: Pass, { BuiltInput { program: self.program, @@ -1318,7 +1320,7 @@ impl ModuleConfig { unresolved_mark: Mark, available_features: FeatureFlag, resolver: Option<(FileName, Arc)>, - ) -> Box { + ) -> Box { let resolver = if let Some((base, resolver)) = resolver { Resolver::Real { base, resolver } } else { @@ -1328,7 +1330,7 @@ impl ModuleConfig { match config { None | Some(ModuleConfig::Es6(..)) | Some(ModuleConfig::NodeNext(..)) => match resolver { - Resolver::Default => Box::new(noop()), + Resolver::Default => Box::new(noop_pass()), Resolver::Real { base, resolver } => Box::new(import_rewriter(base, resolver)), }, Some(ModuleConfig::CommonJs(config)) => Box::new(modules::common_js::common_js( @@ -1540,7 +1542,7 @@ impl Default for GlobalInliningPassEnvs { } impl GlobalPassOption { - pub fn build(self, cm: &SourceMap, handler: &Handler) -> impl 'static + Fold { + pub fn build(self, cm: &SourceMap, handler: &Handler) -> impl 'static + Pass { type ValuesMap = Arc>; fn expr(cm: &SourceMap, handler: &Handler, src: String) -> Box { @@ -1761,10 +1763,3 @@ fn build_resolver( r } - -fn option_pass(pass: Option>) -> Box { - match pass { - None => Box::new(noop()), - Some(pass) => pass, - } -} diff --git a/crates/swc/src/dropped_comments_preserver.rs b/crates/swc/src/dropped_comments_preserver.rs index 0f3478b641a7..19609d5f0dea 100644 --- a/crates/swc/src/dropped_comments_preserver.rs +++ b/crates/swc/src/dropped_comments_preserver.rs @@ -2,8 +2,8 @@ use swc_common::{ comments::{Comment, Comments, SingleThreadedComments}, BytePos, Span, DUMMY_SP, }; -use swc_ecma_ast::{Module, Script}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_ast::{Module, Pass, Script}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// Preserves comments that would otherwise be dropped. /// @@ -16,10 +16,8 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWit /// This transformer shifts orphaned comments to the next closest known span /// while making a best-effort to preserve the "general orientation" of /// comments. -pub fn dropped_comments_preserver( - comments: Option, -) -> impl Fold + VisitMut { - as_folder(DroppedCommentsPreserver { +pub fn dropped_comments_preserver(comments: Option) -> impl Pass { + visit_mut_pass(DroppedCommentsPreserver { comments, is_first_span: true, known_spans: Vec::new(), diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index ee1a6b3a099f..6f53258c7303 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -131,12 +131,12 @@ use once_cell::sync::Lazy; use serde_json::error::Category; pub use sourcemap; use swc_common::{ - chain, comments::Comments, errors::Handler, sync::Lrc, FileName, Mark, SourceFile, SourceMap, - Spanned, GLOBALS, + comments::Comments, errors::Handler, sync::Lrc, FileName, Mark, SourceFile, SourceMap, Spanned, + GLOBALS, }; pub use swc_compiler_base::{PrintArgs, TransformOutput}; pub use swc_config::config_types::{BoolConfig, BoolOr, BoolOrDataConfig}; -use swc_ecma_ast::{EsVersion, Program}; +use swc_ecma_ast::{noop_pass, EsVersion, Pass, Program}; use swc_ecma_codegen::{to_code_with_comments, Node}; use swc_ecma_loader::resolvers::{ lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver, @@ -148,7 +148,6 @@ use swc_ecma_transforms::{ helpers::{self, Helpers}, hygiene, modules::{path::NodeImportResolver, rewriter::import_rewriter}, - pass::noop, resolver, }; use swc_ecma_transforms_base::fixer::paren_remover; @@ -590,9 +589,9 @@ impl Compiler { name: &FileName, comments: Option<&'a SingleThreadedComments>, before_pass: impl 'a + FnOnce(&Program) -> P, - ) -> Result>, Error> + ) -> Result>, Error> where - P: 'a + swc_ecma_visit::Fold, + P: 'a + Pass, { self.run(move || { let _timer = timer!("Compiler.parse"); @@ -684,8 +683,8 @@ impl Compiler { custom_after_pass: impl FnOnce(&Program) -> P2, ) -> Result where - P1: swc_ecma_visit::Fold, - P2: swc_ecma_visit::Fold, + P1: Pass, + P2: Pass, { self.run(|| -> Result<_, Error> { let config = self.run(|| { @@ -708,7 +707,7 @@ impl Compiler { let after_pass = custom_after_pass(&config.program); - let config = config.with_pass(|pass| chain!(pass, after_pass)); + let config = config.with_pass(|pass| (pass, after_pass)); let orig = if config.source_maps.enabled() { self.get_orig_src_map( @@ -742,8 +741,8 @@ impl Compiler { handler, opts, SingleThreadedComments::default(), - |_| noop(), - |_| noop(), + |_| noop_pass(), + |_| noop_pass(), ) } @@ -810,7 +809,7 @@ impl Compiler { let comments = SingleThreadedComments::default(); - let program = self + let mut program = self .parse_js( fm.clone(), handler, @@ -858,14 +857,13 @@ impl Compiler { let is_mangler_enabled = min_opts.mangle.is_some(); - let module = self.run_transform(handler, false, || { - let module = program.fold_with(&mut paren_remover(Some(&comments))); + program = self.run_transform(handler, false, || { + program.mutate(&mut paren_remover(Some(&comments))); - let module = - module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false)); + program.mutate(&mut resolver(unresolved_mark, top_level_mark, false)); - let mut module = swc_ecma_minifier::optimize( - module, + let mut program = swc_ecma_minifier::optimize( + program, self.cm.clone(), Some(&comments), None, @@ -878,9 +876,10 @@ impl Compiler { ); if !is_mangler_enabled { - module.visit_mut_with(&mut hygiene()) + program.visit_mut_with(&mut hygiene()) } - module.fold_with(&mut fixer(Some(&comments as &dyn Comments))) + program.mutate(&mut fixer(Some(&comments as &dyn Comments))); + program }); let preserve_comments = opts @@ -892,7 +891,7 @@ impl Compiler { swc_compiler_base::minify_file_comments(&comments, preserve_comments); self.print( - &module, + &program, PrintArgs { source_root: None, source_file_name: Some(&fm.name.to_string()), @@ -936,8 +935,8 @@ impl Compiler { handler, opts, SingleThreadedComments::default(), - |_| noop(), - |_| noop(), + |_| noop_pass(), + |_| noop_pass(), ) } @@ -948,7 +947,7 @@ impl Compiler { comments: SingleThreadedComments, fm: Arc, orig: Option<&sourcemap::SourceMap>, - config: BuiltInput, + config: BuiltInput, ) -> Result { self.run(|| { let program = config.program; @@ -980,12 +979,13 @@ impl Compiler { let comments = SingleThreadedComments::from_leading_and_trailing(leading, trailing); let mut checker = FastDts::new(fm.name.clone()); - let mut module = program.clone().expect_module(); + let mut module = program.clone(); if let Some((base, resolver)) = config.resolver { - module = module.fold_with(&mut import_rewriter(base, resolver)); + module.mutate(import_rewriter(base, resolver)); } + let mut module = module.expect_module(); let issues = checker.transform(&mut module); for issue in issues { @@ -1001,7 +1001,7 @@ impl Compiler { None }; - let mut pass = config.pass; + let pass = config.pass; let (program, output) = swc_transform_common::output::capture(|| { if let Some(dts_code) = dts_code { emit( @@ -1013,7 +1013,7 @@ impl Compiler { helpers::HELPERS.set(&Helpers::new(config.external_helpers), || { HANDLER.set(handler, || { // Fold module - program.fold_with(&mut pass) + program.apply(pass) }) }) }); diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index 2f6e13977e25..0afba9e4ef1a 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -7,11 +7,10 @@ )] use serde::{Deserialize, Serialize}; +use swc_ecma_ast::Pass; #[cfg(feature = "plugin")] use swc_ecma_ast::*; -#[cfg(not(any(feature = "plugin")))] -use swc_ecma_transforms::pass::noop; -use swc_ecma_visit::{noop_fold_type, Fold}; +use swc_ecma_visit::{fold_pass, noop_fold_type, Fold}; /// A tuple represents a plugin. /// @@ -30,14 +29,14 @@ pub fn plugins( comments: Option, source_map: std::sync::Arc, unresolved_mark: swc_common::Mark, -) -> impl Fold { - RustPlugins { +) -> impl Pass { + fold_pass(RustPlugins { plugins: configured_plugins, metadata_context, comments, source_map, unresolved_mark, - } + }) } struct RustPlugins { diff --git a/crates/swc/tests/projects.rs b/crates/swc/tests/projects.rs index eda7252199ac..d2d29382b0fd 100644 --- a/crates/swc/tests/projects.rs +++ b/crates/swc/tests/projects.rs @@ -14,7 +14,6 @@ use swc::{ try_with_handler, BoolOrDataConfig, Compiler, TransformOutput, }; use swc_common::{ - chain, comments::{Comment, SingleThreadedComments}, errors::{EmitterWriter, Handler, HANDLER}, sync::Lrc, @@ -24,11 +23,8 @@ use swc_compiler_base::{IsModule, PrintArgs}; use swc_ecma_ast::*; use swc_ecma_minifier::option::MangleOptions; use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax}; -use swc_ecma_transforms::{ - helpers::{self, Helpers}, - pass::noop, -}; -use swc_ecma_visit::{Fold, FoldWith}; +use swc_ecma_transforms::helpers::{self, Helpers}; +use swc_ecma_visit::{fold_pass, Fold}; use testing::{NormalizedOutput, StdErr, Tester}; use walkdir::WalkDir; @@ -732,14 +728,14 @@ fn should_visit() { }, &fm.name, Some(&comments), - |_| noop(), + |_| noop_pass(), ) .unwrap() .unwrap(); dbg!(config.syntax); - let config = config.with_pass(|pass| chain!(Panicking, pass)); + let config = config.with_pass(|pass| (fold_pass(Panicking), pass)); if config.minify { let preserve_excl = |_: &BytePos, vc: &mut Vec| -> bool { @@ -749,12 +745,12 @@ fn should_visit() { c.comments().leading.retain(preserve_excl); c.comments().trailing.retain(preserve_excl); } - let mut pass = config.pass; + let pass = config.pass; let program = config.program; let program = helpers::HELPERS.set(&Helpers::new(config.external_helpers), || { HANDLER.set(&handler, || { // Fold module - program.fold_with(&mut pass) + program.apply(pass) }) }); @@ -1072,7 +1068,7 @@ fn issue_6009() { // test parsing input let config = c .parse_js_as_input(fm.clone(), None, &handler, &options, &fm.name, None, |_| { - noop() + noop_pass() }) .unwrap(); diff --git a/crates/swc/tests/rust_api.rs b/crates/swc/tests/rust_api.rs index 7111005e456b..b1a048f7c6a6 100644 --- a/crates/swc/tests/rust_api.rs +++ b/crates/swc/tests/rust_api.rs @@ -5,8 +5,7 @@ use swc::{ use swc_common::{comments::SingleThreadedComments, FileName}; use swc_ecma_ast::*; use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax}; -use swc_ecma_transforms::pass::noop; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut}; struct PanicOnVisit; @@ -49,8 +48,8 @@ fn test_visit_mut() { ..Default::default() }, SingleThreadedComments::default(), - |_| as_folder(PanicOnVisit), - |_| noop(), + |_| visit_mut_pass(PanicOnVisit), + |_| noop_pass(), ); assert_ne!(res.unwrap().code, "console.log(5 as const)"); @@ -100,9 +99,9 @@ fn shopify_1_check_filename() { SingleThreadedComments::default(), |_| { // Ensure comment API - noop() + noop_pass() }, - |_| noop(), + |_| noop_pass(), ); if res.is_err() { @@ -184,8 +183,8 @@ fn shopify_2_same_opt() { &handler, &opts, SingleThreadedComments::default(), - |_| noop(), - |_| noop(), + |_| noop_pass(), + |_| noop_pass(), ); if res.is_err() { @@ -252,8 +251,8 @@ fn shopify_3_reduce_defaults() { &handler, &opts, SingleThreadedComments::default(), - |_| noop(), - |_| noop(), + |_| noop_pass(), + |_| noop_pass(), ); if res.is_err() { @@ -315,8 +314,8 @@ fn shopify_4_reduce_more() { &handler, &opts, SingleThreadedComments::default(), - |_| noop(), - |_| noop(), + |_| noop_pass(), + |_| noop_pass(), ); if res.is_err() { diff --git a/crates/swc_bundler/src/bundler/finalize.rs b/crates/swc_bundler/src/bundler/finalize.rs index 58208ccd0117..d558b6c55199 100644 --- a/crates/swc_bundler/src/bundler/finalize.rs +++ b/crates/swc_bundler/src/bundler/finalize.rs @@ -10,7 +10,7 @@ use swc_ecma_transforms_base::{ hygiene::hygiene, }; use swc_ecma_utils::{contains_top_level_await, find_pat_ids, private_ident, ExprFactory}; -use swc_ecma_visit::{noop_fold_type, Fold, FoldWith}; +use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, VisitMutWith}; use crate::{hash::calc_hash, Bundle, BundleKind, Bundler, Load, ModuleType, Resolve}; @@ -37,13 +37,13 @@ where bundle.module = self.optimize(bundle.module); if !self.config.disable_hygiene { - bundle.module = bundle.module.fold_with(&mut hygiene()); + bundle.module.visit_mut_with(&mut hygiene()); } bundle.module = self.may_wrap_with_iife(bundle.module); if !self.config.disable_fixer { - bundle.module = bundle.module.fold_with(&mut fixer(None)); + bundle.module.visit_mut_with(&mut fixer(None)); } { @@ -55,10 +55,10 @@ where .swc_helpers .lock(); - let module = bundle.module; - - bundle.module = HELPERS.set(&Helpers::from_data(swc_helpers), || { - module.fold_with(&mut inject_helpers(unresolved_mark)) + HELPERS.set(&Helpers::from_data(swc_helpers), || { + bundle + .module + .visit_mut_with(&mut inject_helpers(unresolved_mark)); }); } diff --git a/crates/swc_bundler/src/bundler/load.rs b/crates/swc_bundler/src/bundler/load.rs index 0c543c2e1a4d..9035bde60e06 100644 --- a/crates/swc_bundler/src/bundler/load.rs +++ b/crates/swc_bundler/src/bundler/load.rs @@ -14,7 +14,7 @@ use swc_ecma_ast::{ }; use swc_ecma_transforms_base::resolver; use swc_ecma_visit::{ - noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; use super::{export::Exports, helpers::Helpers, Bundler}; @@ -139,9 +139,8 @@ where data.module.visit_mut_with(&mut ClearMark); - let mut module = - data.module - .fold_with(&mut resolver(self.unresolved_mark, local_mark, false)); + data.module + .visit_mut_with(&mut resolver(self.unresolved_mark, local_mark, false)); // { // let code = self @@ -158,7 +157,7 @@ where // println!("Resolved:\n{}\n\n", code); // } - let imports = self.extract_import_info(file_name, &mut module, local_mark); + let imports = self.extract_import_info(file_name, &mut data.module, local_mark); // { // let code = self @@ -177,7 +176,7 @@ where let exports = self.extract_export_info( file_name, - &mut module, + &mut data.module, SyntaxContext::empty().apply_mark(export_mark), ); @@ -188,7 +187,7 @@ where forced_es6: false, found_other: false, }; - module.visit_with(&mut v); + data.module.visit_with(&mut v); v.forced_es6 || !v.found_other }; @@ -200,13 +199,11 @@ where let (exports, reexport_files) = exports?; import_files.extend(reexport_files); - let module = Lrc::new(module); - Ok(( TransformedModule { id, fm: data.fm, - module, + module: Lrc::new(data.module), imports: Lrc::new(imports), exports: Lrc::new(exports), is_es6, diff --git a/crates/swc_bundler/src/bundler/optimize.rs b/crates/swc_bundler/src/bundler/optimize.rs index fd4998af6cd7..f72a30e49359 100644 --- a/crates/swc_bundler/src/bundler/optimize.rs +++ b/crates/swc_bundler/src/bundler/optimize.rs @@ -1,7 +1,7 @@ use swc_common::pass::Repeat; use swc_ecma_ast::*; use swc_ecma_transforms_optimization::simplify::{const_propagation::constant_propagation, dce}; -use swc_ecma_visit::FoldWith; +use swc_ecma_visit::VisitMutWith; use crate::{Bundler, Load, Resolve}; @@ -17,10 +17,10 @@ where pub(super) fn optimize(&self, mut node: Module) -> Module { self.run(|| { if !self.config.disable_inliner { - node = node.fold_with(&mut constant_propagation()) + node.visit_mut_with(&mut constant_propagation()) } if !self.config.disable_dce { - node = node.fold_with(&mut Repeat::new(dce::dce( + node.visit_mut_with(&mut Repeat::new(dce::dce( dce::Config { // TODO(kdy1): Apply mark to wrapped esms and use it at here. module_mark: None, diff --git a/crates/swc_bundler/tests/common/mod.rs b/crates/swc_bundler/tests/common/mod.rs index 8a453a5d7981..78f863189999 100644 --- a/crates/swc_bundler/tests/common/mod.rs +++ b/crates/swc_bundler/tests/common/mod.rs @@ -26,7 +26,6 @@ use swc_ecma_transforms_base::{ use swc_ecma_transforms_proposal::decorators; use swc_ecma_transforms_react::react; use swc_ecma_transforms_typescript::strip; -use swc_ecma_visit::FoldWith; pub struct Loader { pub cm: Lrc, @@ -135,21 +134,21 @@ impl Load for Loader { let module = HELPERS.set(&Helpers::new(false), || { Program::Module(module) - .fold_with(&mut resolver(unresolved_mark, top_level_mark, false)) - .fold_with(&mut decorators(decorators::Config { + .apply(resolver(unresolved_mark, top_level_mark, false)) + .apply(decorators(decorators::Config { legacy: true, emit_metadata: Default::default(), use_define_for_class_fields: false, })) - .fold_with(&mut strip(unresolved_mark, top_level_mark)) - .fold_with(&mut react::( + .apply(strip(unresolved_mark, top_level_mark)) + .apply(react::( self.cm.clone(), None, Default::default(), top_level_mark, unresolved_mark, )) - .fold_with(&mut inject_helpers(unresolved_mark)) + .apply(inject_helpers(unresolved_mark)) .module() .unwrap() }); diff --git a/crates/swc_bundler/tests/fixture.rs b/crates/swc_bundler/tests/fixture.rs index 7c8bed4151d6..8a3a87a85e1b 100644 --- a/crates/swc_bundler/tests/fixture.rs +++ b/crates/swc_bundler/tests/fixture.rs @@ -10,12 +10,11 @@ use swc_bundler::{BundleKind, Bundler, Config, ModuleRecord}; use swc_common::{errors::HANDLER, FileName, Globals, Span}; use swc_ecma_ast::{ Bool, Expr, IdentName, KeyValueProp, Lit, MemberExpr, MemberProp, MetaPropExpr, MetaPropKind, - PropName, Str, + Program, PropName, Str, }; use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; use swc_ecma_loader::NODE_BUILTINS; use swc_ecma_transforms_base::fixer::fixer; -use swc_ecma_visit::FoldWith; use testing::NormalizedOutput; use self::common::*; @@ -62,7 +61,7 @@ fn do_test(entry: &Path, entries: HashMap, inline: bool) { }; emitter - .emit_module(&bundled.module.fold_with(&mut fixer(None))) + .emit_program(&Program::Module(bundled.module).apply(fixer(None))) .unwrap(); } diff --git a/crates/swc_cli_impl/src/commands/plugin.rs b/crates/swc_cli_impl/src/commands/plugin.rs index 981355537e6d..a481b46b1d24 100644 --- a/crates/swc_cli_impl/src/commands/plugin.rs +++ b/crates/swc_cli_impl/src/commands/plugin.rs @@ -238,7 +238,7 @@ build-wasm32 = "build --target wasm32-unknown-unknown" r##"use swc_core::ecma::{ ast::Program, transforms::testing::test_inline, - visit::{as_folder, FoldWith, VisitMut}, + visit::{visit_mut_pass, FoldWith, VisitMut}, }; use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata}; @@ -267,7 +267,7 @@ impl VisitMut for TransformVisitor { /// Refer swc_plugin_macro to see how does it work internally. #[plugin_transform] pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program { - program.fold_with(&mut as_folder(TransformVisitor)) + program.fold_with(&mut visit_mut_pass(TransformVisitor)) } // An example to test plugin transform. @@ -276,7 +276,7 @@ pub fn process_transform(program: Program, _metadata: TransformPluginProgramMeta // unless explicitly required to do so. test_inline!( Default::default(), - |_| as_folder(TransformVisitor), + |_| visit_mut_pass(TransformVisitor), boo, // Input codes r#"console.log("transform");"#, diff --git a/crates/swc_common/src/lib.rs b/crates/swc_common/src/lib.rs index 3ca6512cf68b..ba0b27e7a868 100644 --- a/crates/swc_common/src/lib.rs +++ b/crates/swc_common/src/lib.rs @@ -39,7 +39,6 @@ use std::fmt::Debug; pub use ast_node::{ast_node, ast_serde, DeserializeEnum, Spanned}; pub use from_variant::FromVariant; pub use swc_eq_ignore_macros::{EqIgnoreSpan, TypeEq}; -pub use swc_visit::chain; pub use self::{ eq::{EqIgnoreSpan, TypeEq}, diff --git a/crates/swc_common/src/pass.rs b/crates/swc_common/src/pass.rs index e91bab0724e2..a53bd8d749f5 100644 --- a/crates/swc_common/src/pass.rs +++ b/crates/swc_common/src/pass.rs @@ -9,24 +9,119 @@ pub trait CompilerPass { /// /// - name should follow hyphen-case. /// - an implementation should return same name - fn name() -> Cow<'static, str>; + fn name(&self) -> Cow<'static, str>; } impl CompilerPass for Repeat where V: CompilerPass + Repeated, { - fn name() -> Cow<'static, str> { - Cow::Owned(format!("repeat({})", V::name())) + fn name(&self) -> Cow<'static, str> { + Cow::Owned(format!("repeat({})", self.pass.name())) } } -impl CompilerPass for AndThen -where - A: CompilerPass, - B: CompilerPass, -{ - fn name() -> Cow<'static, str> { - Cow::Owned(format!("{} -> {}", A::name(), B::name())) - } +macro_rules! impl_compiler_pass_for_tuple { + ( + [$idx:tt, $name:ident], $([$idx_rest:tt, $name_rest:ident]),* + ) => { + impl<$name, $($name_rest),*> CompilerPass for ($name, $($name_rest),*) + where + $name: CompilerPass, + $($name_rest: CompilerPass),* + { + #[inline] + fn name(&self) -> Cow<'static, str> { + use std::fmt::Write; + + let mut name = self.$idx.name().to_string(); + $( + write!(name, " / {}", self.$idx_rest.name()).unwrap(); + )* + Cow::Owned(name) + } + } + }; } + +impl_compiler_pass_for_tuple!([0, A], [1, B]); +impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C]); +impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D]); +impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E]); +impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F]); +impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F], [6, G]); +impl_compiler_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H] +); +impl_compiler_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I] +); +impl_compiler_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J] +); +impl_compiler_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K] +); +impl_compiler_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L] +); +impl_compiler_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L], + [12, M] +); diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index 89b7165fb3f6..1439bc9e790d 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -53,9 +53,7 @@ pub mod ecma { )] pub mod transforms { pub mod base { - pub use swc_ecma_transforms_base::{ - assumptions::Assumptions, feature, fixer, helpers, hygiene, pass, perf, resolver, - }; + pub use swc_ecma_transforms_base::*; } #[cfg(feature = "ecma_transforms_optimization")] #[cfg_attr(docsrs, doc(cfg(feature = "transforms_optimization")))] diff --git a/crates/swc_ecma_ast/Cargo.toml b/crates/swc_ecma_ast/Cargo.toml index 8ab8bea3687d..0b480cf65d36 100644 --- a/crates/swc_ecma_ast/Cargo.toml +++ b/crates/swc_ecma_ast/Cargo.toml @@ -32,16 +32,17 @@ serde-impl = ["serde"] arbitrary = { workspace = true, features = ["derive"], optional = true } bitflags = { workspace = true } # bytecheck version should be in sync with rkyv version. Do not bump individually. -bytecheck = { workspace = true, optional = true } -is-macro = { workspace = true } -num-bigint = { workspace = true, features = ["serde"] } -phf = { workspace = true, features = ["macros"] } -rkyv = { workspace = true, features = ["validation"], optional = true } -scoped-tls = { workspace = true } -serde = { workspace = true, features = ["derive"], optional = true } -string_enum = { version = "1.0.0", path = "../string_enum" } -swc_atoms = { version = "2.0.0", path = "../swc_atoms" } -swc_common = { version = "1.0.0", path = "../swc_common" } +bytecheck = { workspace = true, optional = true } +is-macro = { workspace = true } +num-bigint = { workspace = true, features = ["serde"] } +phf = { workspace = true, features = ["macros"] } +rkyv = { workspace = true, features = ["validation"], optional = true } +scoped-tls = { workspace = true } +serde = { workspace = true, features = ["derive"], optional = true } +string_enum = { version = "1.0.0", path = "../string_enum" } +swc_atoms = { version = "2.0.0", path = "../swc_atoms" } +swc_common = { version = "1.0.0", path = "../swc_common" } +swc_visit = { version = "1.0.0", path = "../swc_visit" } unicode-id-start = { workspace = true } [dev-dependencies] diff --git a/crates/swc_ecma_ast/src/lib.rs b/crates/swc_ecma_ast/src/lib.rs index b9502b737920..942cf099b914 100644 --- a/crates/swc_ecma_ast/src/lib.rs +++ b/crates/swc_ecma_ast/src/lib.rs @@ -12,7 +12,7 @@ pub use num_bigint::BigInt as BigIntValue; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span}; +use swc_common::{ast_node, pass::Either, util::take::Take, EqIgnoreSpan, Span}; pub use self::{ class::{ @@ -90,6 +90,243 @@ mod source_map; mod stmt; mod typescript; +/// A map from the [Program] to the [Program]. +/// +/// This trait is used to implement transformations. The implementor may decide +/// to implement [Fold] or [VisitMut] if the transform is fine to start from an +/// arbitrary node. +/// +/// Tuple of [Pass] implementations also implements [Pass], but it's limited to +/// 12 items for fast compile time. If you have more passes, nest it like `(a, +/// (b, c), (d, e))` +pub trait Pass { + fn process(&mut self, program: &mut Program); +} + +/// Optional pass implementation. +impl

Pass for Option

+where + P: Pass, +{ + #[inline(always)] + fn process(&mut self, program: &mut Program) { + if let Some(pass) = self { + pass.process(program); + } + } +} + +impl Pass for Box

+where + P: Pass, +{ + #[inline(always)] + fn process(&mut self, program: &mut Program) { + (**self).process(program); + } +} + +impl Pass for &'_ mut P +where + P: Pass, +{ + #[inline(always)] + fn process(&mut self, program: &mut Program) { + (**self).process(program); + } +} + +impl Pass for Either +where + L: Pass, + R: Pass, +{ + #[inline] + fn process(&mut self, program: &mut Program) { + match self { + Either::Left(l) => l.process(program), + Either::Right(r) => r.process(program), + } + } +} + +impl

Pass for swc_visit::Optional

+where + P: Pass, +{ + #[inline] + fn process(&mut self, program: &mut Program) { + if self.enabled { + self.visitor.process(program); + } + } +} + +impl

Pass for swc_visit::Repeat

+where + P: Pass + swc_visit::Repeated, +{ + #[inline] + fn process(&mut self, program: &mut Program) { + loop { + self.pass.reset(); + self.pass.process(program); + + if !self.pass.changed() { + break; + } + } + } +} + +impl Program { + #[inline(always)] + pub fn mutate

(&mut self, mut pass: P) + where + P: Pass, + { + pass.process(self); + } + + #[inline(always)] + pub fn apply

(mut self, mut pass: P) -> Self + where + P: Pass, + { + pass.process(&mut self); + self + } +} + +macro_rules! impl_pass_for_tuple { + ( + [$idx:tt, $name:ident], $([$idx_rest:tt, $name_rest:ident]),* + ) => { + impl<$name, $($name_rest),*> Pass for ($name, $($name_rest),*) + where + $name: Pass, + $($name_rest: Pass),* + { + #[inline] + fn process(&mut self, program: &mut Program) { + self.$idx.process(program); + + $( + self.$idx_rest.process(program); + )* + + } + } + }; +} + +impl_pass_for_tuple!([0, A], [1, B]); +impl_pass_for_tuple!([0, A], [1, B], [2, C]); +impl_pass_for_tuple!([0, A], [1, B], [2, C], [3, D]); +impl_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E]); +impl_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F]); +impl_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F], [6, G]); +impl_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H] +); +impl_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I] +); +impl_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J] +); +impl_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K] +); +impl_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L] +); +impl_pass_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L], + [12, M] +); + +#[inline(always)] +pub fn noop_pass() -> impl Pass { + fn noop(_: &mut Program) {} + + fn_pass(noop) +} + +#[inline(always)] +pub fn fn_pass(f: impl FnMut(&mut Program)) -> impl Pass { + FnPass { f } +} + +struct FnPass { + f: F, +} + +impl Pass for FnPass +where + F: FnMut(&mut Program), +{ + fn process(&mut self, program: &mut Program) { + (self.f)(program); + } +} + /// Represents a invalid node. #[ast_node("Invalid")] #[derive(Eq, Default, Hash, Copy, EqIgnoreSpan)] diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index bb7555c59a4b..9df499510689 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -4,6 +4,7 @@ use std::{ hash::{Hash, Hasher}, }; +use is_macro::Is; use num_bigint::BigInt as BigIntValue; use swc_atoms::{js_word, Atom}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; @@ -11,7 +12,7 @@ use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::jsx::JSXText; #[ast_node] -#[derive(Eq, Hash, EqIgnoreSpan)] +#[derive(Eq, Hash, EqIgnoreSpan, Is)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub enum Lit { #[tag("StringLiteral")] diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index 68df93b3e461..8655d9d092a8 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -14,8 +14,14 @@ pub enum Program { Script(Script), } +impl Take for Program { + fn dummy() -> Self { + Program::Script(Script::default()) + } +} + #[ast_node("Module")] -#[derive(Eq, Hash, EqIgnoreSpan)] +#[derive(Eq, Hash, EqIgnoreSpan, Default)] pub struct Module { pub span: Span, @@ -50,7 +56,7 @@ impl Take for Module { } #[ast_node("Script")] -#[derive(Eq, Hash, EqIgnoreSpan)] +#[derive(Eq, Hash, EqIgnoreSpan, Default)] pub struct Script { pub span: Span, diff --git a/crates/swc_ecma_compat_bugfixes/src/async_arrows_in_class.rs b/crates/swc_ecma_compat_bugfixes/src/async_arrows_in_class.rs index 6d2bbc251af2..278a44022991 100644 --- a/crates/swc_ecma_compat_bugfixes/src/async_arrows_in_class.rs +++ b/crates/swc_ecma_compat_bugfixes/src/async_arrows_in_class.rs @@ -2,7 +2,7 @@ use swc_common::{util::take::Take, Mark, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_compat_es2015::arrow; use swc_ecma_utils::prepend_stmt; -use swc_ecma_visit::{standard_only_fold, Fold, FoldWith, InjectVars}; +use swc_ecma_visit::{fold_pass, standard_only_fold, Fold, FoldWith, InjectVars, VisitMutWith}; use swc_trace_macro::swc_trace; /// A bugfix pass for Safari 10.3. @@ -12,11 +12,11 @@ use swc_trace_macro::swc_trace; /// instance via `this` within those methods would also throw. This is fixed by /// converting arrow functions in class methods into equivalent function /// expressions. See https://bugs.webkit.org/show_bug.cgi?id=166879 -pub fn async_arrows_in_class(unresolved_mark: Mark) -> impl Fold { - AsyncArrowsInClass { +pub fn async_arrows_in_class(unresolved_mark: Mark) -> impl Pass { + fold_pass(AsyncArrowsInClass { unresolved_mark, ..Default::default() - } + }) } #[derive(Default, Clone)] struct AsyncArrowsInClass { @@ -45,7 +45,7 @@ impl Fold for AsyncArrowsInClass { } fn fold_expr(&mut self, n: Expr) -> Expr { - let n = n.fold_children_with(self); + let mut n = n.fold_children_with(self); if !self.in_class_method { return n; } @@ -54,7 +54,7 @@ impl Fold for AsyncArrowsInClass { Expr::Arrow(ref a) => { if a.is_async { let mut v = arrow(self.unresolved_mark); - let n = n.fold_with(&mut v); + n.visit_mut_with(&mut v); self.vars.extend(v.take_vars()); n } else { @@ -106,17 +106,16 @@ impl Fold for AsyncArrowsInClass { #[cfg(test)] mod tests { - use swc_common::chain; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::test; use super::*; - fn tr() -> impl Fold { + fn tr() -> impl Pass { let unresolved = Mark::new(); - chain!( + ( resolver(unresolved, Mark::new(), false), - async_arrows_in_class(unresolved) + async_arrows_in_class(unresolved), ) } diff --git a/crates/swc_ecma_compat_bugfixes/src/edge_default_param.rs b/crates/swc_ecma_compat_bugfixes/src/edge_default_param.rs index 4c04127b090b..73ff3be97d92 100644 --- a/crates/swc_ecma_compat_bugfixes/src/edge_default_param.rs +++ b/crates/swc_ecma_compat_bugfixes/src/edge_default_param.rs @@ -1,5 +1,5 @@ use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// A bugfix pass for Edge. @@ -8,8 +8,8 @@ use swc_trace_macro::swc_trace; /// syntax. This fixes the only arguments-related bug in ES Modules-supporting /// browsers (Edge 16 & 17). Use this plugin instead of /// @babel/plugin-transform-parameters when targeting ES Modules. -pub fn edge_default_param() -> impl Fold + VisitMut { - as_folder(EdgeDefaultParam::default()) +pub fn edge_default_param() -> impl Pass { + visit_mut_pass(EdgeDefaultParam::default()) } #[derive(Default, Clone, Copy)] struct EdgeDefaultParam { @@ -62,16 +62,16 @@ impl VisitMut for EdgeDefaultParam { #[cfg(test)] mod tests { - use swc_common::{chain, Mark}; + use swc_common::Mark; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::test; use super::*; - fn tr() -> impl Fold { - chain!( + fn tr() -> impl Pass { + ( resolver(Mark::new(), Mark::new(), false), - edge_default_param() + edge_default_param(), ) } diff --git a/crates/swc_ecma_compat_bugfixes/src/lib.rs b/crates/swc_ecma_compat_bugfixes/src/lib.rs index 9731cf74aa18..385775e0720d 100644 --- a/crates/swc_ecma_compat_bugfixes/src/lib.rs +++ b/crates/swc_ecma_compat_bugfixes/src/lib.rs @@ -1,5 +1,5 @@ -use swc_common::{chain, Mark}; -use swc_ecma_visit::Fold; +use swc_common::Mark; +use swc_ecma_ast::Pass; pub use self::{ async_arrows_in_class::async_arrows_in_class, edge_default_param::edge_default_param, @@ -12,11 +12,11 @@ mod edge_default_param; mod safari_id_destructuring_collision_in_function_expression; mod template_literal_caching; -pub fn bugfixes(unresolved_mark: Mark) -> impl Fold { - chain!( +pub fn bugfixes(unresolved_mark: Mark) -> impl Pass { + ( async_arrows_in_class(unresolved_mark), edge_default_param(), template_literal_caching(), - safari_id_destructuring_collision_in_function_expression() + safari_id_destructuring_collision_in_function_expression(), ) } diff --git a/crates/swc_ecma_compat_bugfixes/src/safari_id_destructuring_collision_in_function_expression.rs b/crates/swc_ecma_compat_bugfixes/src/safari_id_destructuring_collision_in_function_expression.rs index 425f297863e0..fe0fa5d2e56a 100644 --- a/crates/swc_ecma_compat_bugfixes/src/safari_id_destructuring_collision_in_function_expression.rs +++ b/crates/swc_ecma_compat_bugfixes/src/safari_id_destructuring_collision_in_function_expression.rs @@ -4,11 +4,11 @@ use swc_atoms::JsWord; use swc_common::{collections::AHashSet, SyntaxContext}; use swc_ecma_ast::*; use swc_ecma_transforms_base::hygiene::rename; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn safari_id_destructuring_collision_in_function_expression() -> impl Fold + VisitMut { - as_folder(SafariIdDestructuringCollisionInFunctionExpression::default()) +pub fn safari_id_destructuring_collision_in_function_expression() -> impl Pass { + visit_mut_pass(SafariIdDestructuringCollisionInFunctionExpression::default()) } #[derive(Default, Clone)] @@ -103,17 +103,18 @@ impl VisitMut for SafariIdDestructuringCollisionInFunctionExpression { #[cfg(test)] mod tests { - use swc_common::{chain, Mark}; + use swc_common::Mark; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::{test, HygieneTester}; + use swc_ecma_visit::fold_pass; use super::*; - fn tr() -> impl Fold { - chain!( + fn tr() -> impl Pass { + ( resolver(Mark::new(), Mark::new(), false), - safari_id_destructuring_collision_in_function_expression() + safari_id_destructuring_collision_in_function_expression(), ) } @@ -166,7 +167,7 @@ mod tests { test!( Syntax::default(), - |_| chain!(tr(), HygieneTester), + |_| (tr(), fold_pass(HygieneTester)), issue_4488_1, " export default function _type_of() { diff --git a/crates/swc_ecma_compat_bugfixes/src/template_literal_caching.rs b/crates/swc_ecma_compat_bugfixes/src/template_literal_caching.rs index 10d29e62acbc..1f5cb6a008c3 100644 --- a/crates/swc_ecma_compat_bugfixes/src/template_literal_caching.rs +++ b/crates/swc_ecma_compat_bugfixes/src/template_literal_caching.rs @@ -1,7 +1,7 @@ use swc_common::DUMMY_SP; use swc_ecma_ast::*; use swc_ecma_utils::{prepend_stmt, private_ident, ExprFactory}; -use swc_ecma_visit::{standard_only_fold, Fold, FoldWith}; +use swc_ecma_visit::{fold_pass, standard_only_fold, Fold, FoldWith}; use swc_trace_macro::swc_trace; // Converts destructured parameters with default values to non-shorthand syntax. @@ -19,8 +19,8 @@ use swc_trace_macro::swc_trace; // Object``===Object`` // true, should be false. // // Benchmarks: https://jsperf.com/compiled-tagged-template-performance -pub fn template_literal_caching() -> impl Fold { - TemplateLiteralCaching::default() +pub fn template_literal_caching() -> impl Pass { + fold_pass(TemplateLiteralCaching::default()) } #[derive(Default, Clone)] struct TemplateLiteralCaching { @@ -161,16 +161,16 @@ impl Fold for TemplateLiteralCaching { #[cfg(test)] mod tests { - use swc_common::{chain, Mark}; + use swc_common::Mark; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::test; use super::*; - fn tr() -> impl Fold { - chain!( + fn tr() -> impl Pass { + ( resolver(Mark::new(), Mark::new(), false), - template_literal_caching() + template_literal_caching(), ) } diff --git a/crates/swc_ecma_compat_common/src/regexp.rs b/crates/swc_ecma_compat_common/src/regexp.rs index aea56f5fa5e5..7d5c141b6a57 100644 --- a/crates/swc_ecma_compat_common/src/regexp.rs +++ b/crates/swc_ecma_compat_common/src/regexp.rs @@ -1,10 +1,10 @@ use swc_common::util::take::Take; -use swc_ecma_ast::{CallExpr, Expr, Lit, Regex}; +use swc_ecma_ast::{CallExpr, Expr, Lit, Pass, Regex}; use swc_ecma_utils::{quote_ident, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; -pub fn regexp(config: Config) -> impl Fold + VisitMut { - as_folder(RegExp { config }) +pub fn regexp(config: Config) -> impl Pass { + visit_mut_pass(RegExp { config }) } #[derive(Default, Clone, Copy)] diff --git a/crates/swc_ecma_compat_es2015/src/arrow.rs b/crates/swc_ecma_compat_es2015/src/arrow.rs index 9fa82b2bd090..208d46738632 100644 --- a/crates/swc_ecma_compat_es2015/src/arrow.rs +++ b/crates/swc_ecma_compat_es2015/src/arrow.rs @@ -6,7 +6,7 @@ use swc_ecma_utils::{ function::{init_this, FnEnvHoister}, prepend_stmt, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, InjectVars, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, InjectVars, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// Compile ES2015 arrow functions to ES5 @@ -57,8 +57,8 @@ use swc_trace_macro::swc_trace; /// }; /// console.log(bob.printFriends()); /// ``` -pub fn arrow(unresolved_mark: Mark) -> impl Fold + VisitMut + InjectVars { - as_folder(Arrow { +pub fn arrow(unresolved_mark: Mark) -> impl Pass + VisitMut + InjectVars { + visit_mut_pass(Arrow { in_subclass: false, hoister: FnEnvHoister::new(SyntaxContext::empty().apply_mark(unresolved_mark)), }) diff --git a/crates/swc_ecma_compat_es2015/src/block_scoped_fn.rs b/crates/swc_ecma_compat_es2015/src/block_scoped_fn.rs index b6dfb9dc5c34..1405c3da819d 100644 --- a/crates/swc_ecma_compat_es2015/src/block_scoped_fn.rs +++ b/crates/swc_ecma_compat_es2015/src/block_scoped_fn.rs @@ -1,11 +1,11 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::IdentUsageFinder; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn block_scoped_functions() -> impl Fold + VisitMut { - as_folder(BlockScopedFns) +pub fn block_scoped_functions() -> impl Pass { + visit_mut_pass(BlockScopedFns) } #[derive(Clone, Copy)] diff --git a/crates/swc_ecma_compat_es2015/src/block_scoping/mod.rs b/crates/swc_ecma_compat_es2015/src/block_scoping/mod.rs index 4391039f88ca..40c0f05b82cb 100644 --- a/crates/swc_ecma_compat_es2015/src/block_scoping/mod.rs +++ b/crates/swc_ecma_compat_es2015/src/block_scoping/mod.rs @@ -4,7 +4,6 @@ use indexmap::IndexMap; use smallvec::SmallVec; use swc_atoms::JsWord; use swc_common::{ - chain, collections::{AHashMap, AHashSet}, util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP, @@ -16,7 +15,7 @@ use swc_ecma_utils::{ ExprFactory, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, visit_mut_obj_and_computed, Fold, VisitMut, VisitMutWith, + noop_visit_mut_type, visit_mut_obj_and_computed, visit_mut_pass, VisitMut, VisitMutWith, }; use swc_trace_macro::swc_trace; @@ -35,16 +34,16 @@ mod vars; /// }); /// } /// ``` -pub fn block_scoping(unresolved_mark: Mark) -> impl VisitMut + Fold { - as_folder(chain!( - self::vars::block_scoped_vars(), - BlockScoping { +pub fn block_scoping(unresolved_mark: Mark) -> impl Pass { + ( + visit_mut_pass(self::vars::block_scoped_vars()), + visit_mut_pass(BlockScoping { unresolved_mark, scope: Default::default(), vars: Vec::new(), var_decl_kind: VarDeclKind::Var, - } - )) + }), + ) } type ScopeStack = SmallVec<[ScopeKind; 8]>; diff --git a/crates/swc_ecma_compat_es2015/src/classes/mod.rs b/crates/swc_ecma_compat_es2015/src/classes/mod.rs index bf6136881586..100015bfc3b8 100644 --- a/crates/swc_ecma_compat_es2015/src/classes/mod.rs +++ b/crates/swc_ecma_compat_es2015/src/classes/mod.rs @@ -15,7 +15,7 @@ use swc_ecma_utils::{ ExprFactory, ModuleItemLike, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_trace_macro::swc_trace; @@ -27,8 +27,8 @@ use self::{ mod constructor; mod prop_name; -pub fn classes(config: Config) -> impl Fold + VisitMut { - as_folder(Classes { +pub fn classes(config: Config) -> impl Pass { + visit_mut_pass(Classes { in_strict: false, config, diff --git a/crates/swc_ecma_compat_es2015/src/computed_props.rs b/crates/swc_ecma_compat_es2015/src/computed_props.rs index 16eee883f22c..e63e5c9dc3d6 100644 --- a/crates/swc_ecma_compat_es2015/src/computed_props.rs +++ b/crates/swc_ecma_compat_es2015/src/computed_props.rs @@ -4,7 +4,7 @@ use swc_ecma_ast::*; use swc_ecma_transforms_base::helper; use swc_ecma_utils::{quote_ident, ExprFactory, StmtLike}; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_trace_macro::swc_trace; @@ -39,8 +39,8 @@ use swc_trace_macro::swc_trace; /// /// TODO(kdy1): cache reference like (_f = f, mutatorMap[_f].get = function(){}) /// instead of (mutatorMap[f].get = function(){} -pub fn computed_properties(c: Config) -> impl Fold { - as_folder(ComputedProps { +pub fn computed_properties(c: Config) -> impl Pass { + visit_mut_pass(ComputedProps { c, ..Default::default() }) diff --git a/crates/swc_ecma_compat_es2015/src/destructuring.rs b/crates/swc_ecma_compat_es2015/src/destructuring.rs index fc3d994b854c..7e7c93bafeb8 100644 --- a/crates/swc_ecma_compat_es2015/src/destructuring.rs +++ b/crates/swc_ecma_compat_es2015/src/destructuring.rs @@ -11,7 +11,7 @@ use swc_ecma_utils::{ prop_name_to_expr, quote_ident, ExprFactory, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_trace_macro::swc_trace; @@ -36,8 +36,8 @@ use swc_trace_macro::swc_trace; /// b = _arr2[1], /// rest = _arr2.slice(2); /// ``` -pub fn destructuring(c: Config) -> impl Fold + VisitMut { - as_folder(Destructuring { c }) +pub fn destructuring(c: Config) -> impl Pass { + visit_mut_pass(Destructuring { c }) } struct Destructuring { diff --git a/crates/swc_ecma_compat_es2015/src/duplicate_keys.rs b/crates/swc_ecma_compat_es2015/src/duplicate_keys.rs index 0fe2d99392c9..cfa66eca96d5 100644 --- a/crates/swc_ecma_compat_es2015/src/duplicate_keys.rs +++ b/crates/swc_ecma_compat_es2015/src/duplicate_keys.rs @@ -3,11 +3,11 @@ use swc_common::{collections::AHashSet, Spanned}; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::quote_str; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn duplicate_keys() -> impl Fold + VisitMut { - as_folder(DuplicateKeys) +pub fn duplicate_keys() -> impl Pass { + visit_mut_pass(DuplicateKeys) } struct DuplicateKeys; diff --git a/crates/swc_ecma_compat_es2015/src/for_of.rs b/crates/swc_ecma_compat_es2015/src/for_of.rs index c04ec264126f..133c4c00497b 100644 --- a/crates/swc_ecma_compat_es2015/src/for_of.rs +++ b/crates/swc_ecma_compat_es2015/src/for_of.rs @@ -11,7 +11,7 @@ use swc_ecma_transforms_macros::parallel; use swc_ecma_utils::{ alias_if_required, member_expr, prepend_stmt, private_ident, quote_ident, ExprFactory, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// `@babel/plugin-transform-for-of` @@ -48,8 +48,8 @@ use swc_trace_macro::swc_trace; /// } /// } /// ``` -pub fn for_of(c: Config) -> impl Fold + VisitMut { - as_folder(ForOf { +pub fn for_of(c: Config) -> impl Pass { + visit_mut_pass(ForOf { c, top_level_vars: Default::default(), }) diff --git a/crates/swc_ecma_compat_es2015/src/function_name.rs b/crates/swc_ecma_compat_es2015/src/function_name.rs index 4ee03b5fa847..633d248bde5d 100644 --- a/crates/swc_ecma_compat_es2015/src/function_name.rs +++ b/crates/swc_ecma_compat_es2015/src/function_name.rs @@ -2,7 +2,7 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::{private_ident, IdentUsageFinder}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// `@babel/plugin-transform-function-name` @@ -22,8 +22,8 @@ use swc_trace_macro::swc_trace; /// } /// var Foo = (class Foo {}); /// ``` -pub fn function_name() -> impl 'static + Copy + Fold + VisitMut { - as_folder(FnName) +pub fn function_name() -> impl Pass { + visit_mut_pass(FnName) } #[derive(Clone, Copy)] diff --git a/crates/swc_ecma_compat_es2015/src/generator.rs b/crates/swc_ecma_compat_es2015/src/generator.rs index 346e0c660f91..6303ab11178d 100644 --- a/crates/swc_ecma_compat_es2015/src/generator.rs +++ b/crates/swc_ecma_compat_es2015/src/generator.rs @@ -17,16 +17,16 @@ use swc_ecma_utils::{ function::FnEnvHoister, private_ident, prop_name_to_expr_value, quote_ident, ExprFactory, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use tracing::debug; /// Generator based on tsc generator at /~https://github.com/microsoft/TypeScript/blob/162224763681465b417274383317ca9a0a573835/src/compiler/transformers/generators.ts -pub fn generator(unresolved_mark: Mark, _comments: C) -> impl VisitMut + Fold +pub fn generator(unresolved_mark: Mark, _comments: C) -> impl Pass where C: Comments, { - as_folder(Wrapper { + visit_mut_pass(Wrapper { unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), }) } diff --git a/crates/swc_ecma_compat_es2015/src/instanceof.rs b/crates/swc_ecma_compat_es2015/src/instanceof.rs index 80557b182f19..f777e97a2164 100644 --- a/crates/swc_ecma_compat_es2015/src/instanceof.rs +++ b/crates/swc_ecma_compat_es2015/src/instanceof.rs @@ -2,7 +2,7 @@ use swc_common::{util::take::Take, Span, Spanned}; use swc_ecma_ast::*; use swc_ecma_transforms_base::{helper, perf::Parallel}; use swc_ecma_utils::ExprFactory; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// `@babel/plugin-transform-instanceof` @@ -30,8 +30,8 @@ use swc_trace_macro::swc_trace; /// /// _instanceof(foo, Bar); /// ``` -pub fn instance_of() -> impl Fold + VisitMut { - as_folder(InstanceOf) +pub fn instance_of() -> impl Pass { + visit_mut_pass(InstanceOf) } struct InstanceOf; diff --git a/crates/swc_ecma_compat_es2015/src/lib.rs b/crates/swc_ecma_compat_es2015/src/lib.rs index d1a10b3de8cc..54e9ff568ebb 100644 --- a/crates/swc_ecma_compat_es2015/src/lib.rs +++ b/crates/swc_ecma_compat_es2015/src/lib.rs @@ -2,9 +2,9 @@ #![allow(clippy::boxed_local)] use serde::Deserialize; -use swc_common::{chain, comments::Comments, pass::Optional, Mark}; +use swc_common::{comments::Comments, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_compat_common::regexp::{self, regexp}; -use swc_ecma_visit::Fold; pub use self::{ arrow::arrow, block_scoped_fn::block_scoped_functions, block_scoping::block_scoping, @@ -36,8 +36,8 @@ mod sticky_regex; pub mod template_literal; mod typeof_symbol; -fn exprs(unresolved_mark: Mark) -> impl Fold { - chain!( +fn exprs(unresolved_mark: Mark) -> impl Pass { + ( arrow(unresolved_mark), duplicate_keys(), sticky_regex(), @@ -55,39 +55,47 @@ fn exprs(unresolved_mark: Mark) -> impl Fold { /// Used to generate `require` calls. /// See the documentation of [regenerator](self::regenerator::regenerator) for /// more details. -pub fn es2015(unresolved_mark: Mark, comments: Option, c: Config) -> impl Fold +pub fn es2015(unresolved_mark: Mark, comments: Option, c: Config) -> impl Pass where C: Comments + Clone, { - chain!( - regexp(regexp::Config { - dot_all_regex: false, - has_indices: false, - lookbehind_assertion: false, - named_capturing_groups_regex: false, - sticky_regex: true, - unicode_property_regex: false, - unicode_regex: true, - unicode_sets_regex: false, - }), - block_scoped_functions(), - template_literal(c.template_literal), - classes(c.classes), - new_target(), - spread(c.spread), + ( + ( + regexp(regexp::Config { + dot_all_regex: false, + has_indices: false, + lookbehind_assertion: false, + named_capturing_groups_regex: false, + sticky_regex: true, + unicode_property_regex: false, + unicode_regex: true, + unicode_sets_regex: false, + }), + block_scoped_functions(), + template_literal(c.template_literal), + classes(c.classes), + new_target(), + spread(c.spread), + ), // /~https://github.com/Microsoft/TypeScript/issues/5441 - Optional::new(object_super(), !c.typescript), + if !c.typescript { + Some(object_super()) + } else { + None + }, shorthand(), function_name(), for_of(c.for_of), // Should come before parameters // See: /~https://github.com/swc-project/swc/issues/1036 parameters(c.parameters, unresolved_mark), - exprs(unresolved_mark), - computed_properties(c.computed_props), - destructuring(c.destructuring), - block_scoping(unresolved_mark), - generator::generator(unresolved_mark, comments), + ( + exprs(unresolved_mark), + computed_properties(c.computed_props), + destructuring(c.destructuring), + block_scoping(unresolved_mark), + generator::generator(unresolved_mark, comments.clone()), + ), ) } @@ -174,7 +182,7 @@ class HomePage extends React.Component {} test!( ::swc_ecma_parser::Syntax::default(), - |_| chain!( + |_| ( block_scoped_functions(), resolver(Mark::new(), Mark::new(), false) ), @@ -192,7 +200,7 @@ function foo(scope) { // test!( // ::swc_ecma_parser::Syntax::default(), - // |_| chain!( + // |_| ( // resolver(), // class_properties(), // // Optional::new(compat::es2018(), target <= EsVersion::Es2018), diff --git a/crates/swc_ecma_compat_es2015/src/new_target.rs b/crates/swc_ecma_compat_es2015/src/new_target.rs index d47bb3eacf0f..a4c5bf81a2c0 100644 --- a/crates/swc_ecma_compat_es2015/src/new_target.rs +++ b/crates/swc_ecma_compat_es2015/src/new_target.rs @@ -5,12 +5,12 @@ use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{should_work, Check}; use swc_ecma_utils::{private_ident, quote_ident, ExprFactory}; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, }; use swc_trace_macro::swc_trace; -pub fn new_target() -> impl Fold + VisitMut + CompilerPass { - as_folder(NewTarget { +pub fn new_target() -> impl Pass { + visit_mut_pass(NewTarget { ctx: Ctx::Constructor, }) } @@ -138,7 +138,7 @@ impl VisitMut for NewTarget { } impl CompilerPass for NewTarget { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { Cow::Borrowed("new-target") } } diff --git a/crates/swc_ecma_compat_es2015/src/object_super.rs b/crates/swc_ecma_compat_es2015/src/object_super.rs index f182a5ecd617..85b4fea6d3f1 100644 --- a/crates/swc_ecma_compat_es2015/src/object_super.rs +++ b/crates/swc_ecma_compat_es2015/src/object_super.rs @@ -6,15 +6,15 @@ use swc_ecma_transforms_base::helper; use swc_ecma_utils::{ alias_ident_for, is_rest_arguments, prepend_stmt, private_ident, quote_ident, ExprFactory, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; struct ObjectSuper { extra_vars: Vec, } -pub fn object_super() -> impl Fold + VisitMut { - as_folder(ObjectSuper { +pub fn object_super() -> impl Pass { + visit_mut_pass(ObjectSuper { extra_vars: Vec::new(), }) } @@ -484,7 +484,7 @@ impl SuperReplacer { } #[cfg(test)] mod tests { - use swc_common::{chain, Mark}; + use swc_common::Mark; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::test; @@ -496,7 +496,7 @@ mod tests { |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_super(), shorthand(), @@ -513,7 +513,7 @@ mod tests { test!( ::swc_ecma_parser::Syntax::default(), |_| { - chain!( + ( resolver(Mark::new(), Mark::new(), false), object_super(), shorthand(), @@ -530,7 +530,7 @@ mod tests { test!( ::swc_ecma_parser::Syntax::default(), |_| { - chain!( + ( resolver(Mark::new(), Mark::new(), false), object_super(), shorthand(), @@ -547,7 +547,7 @@ mod tests { test!( ::swc_ecma_parser::Syntax::default(), |_| { - chain!( + ( resolver(Mark::new(), Mark::new(), false), object_super(), shorthand(), @@ -572,7 +572,7 @@ mod tests { ..Default::default() }), |_| { - chain!( + ( resolver(Mark::new(), Mark::new(), false), object_super(), shorthand(), diff --git a/crates/swc_ecma_compat_es2015/src/parameters.rs b/crates/swc_ecma_compat_es2015/src/parameters.rs index 91078a1d2233..39a662b6d4ec 100644 --- a/crates/swc_ecma_compat_es2015/src/parameters.rs +++ b/crates/swc_ecma_compat_es2015/src/parameters.rs @@ -10,13 +10,13 @@ use swc_ecma_utils::{ function::{init_this, FnEnvHoister}, member_expr, prepend_stmt, prepend_stmts, private_ident, quote_ident, ExprFactory, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; use tracing::trace; -pub fn parameters(c: Config, unresolved_mark: Mark) -> impl 'static + Fold { +pub fn parameters(c: Config, unresolved_mark: Mark) -> impl 'static + Pass { let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark); - as_folder(Params { + visit_mut_pass(Params { c, unresolved_ctxt, hoister: FnEnvHoister::new(unresolved_ctxt), diff --git a/crates/swc_ecma_compat_es2015/src/shorthand_property.rs b/crates/swc_ecma_compat_es2015/src/shorthand_property.rs index f710e2bfe694..75af36d78914 100644 --- a/crates/swc_ecma_compat_es2015/src/shorthand_property.rs +++ b/crates/swc_ecma_compat_es2015/src/shorthand_property.rs @@ -1,7 +1,7 @@ use swc_common::util::take::Take; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// Compile ES2015 shorthand properties to ES5 @@ -38,8 +38,8 @@ use swc_trace_macro::swc_trace; /// } /// }; /// ``` -pub fn shorthand() -> impl 'static + Fold + VisitMut { - as_folder(Shorthand) +pub fn shorthand() -> impl Pass { + visit_mut_pass(Shorthand) } #[derive(Clone, Copy)] diff --git a/crates/swc_ecma_compat_es2015/src/spread.rs b/crates/swc_ecma_compat_es2015/src/spread.rs index f4276c53504a..8592c7cf42f3 100644 --- a/crates/swc_ecma_compat_es2015/src/spread.rs +++ b/crates/swc_ecma_compat_es2015/src/spread.rs @@ -9,12 +9,12 @@ use swc_ecma_utils::{ alias_ident_for, member_expr, prepend_stmt, quote_ident, ExprFactory, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_trace_macro::swc_trace; -pub fn spread(c: Config) -> impl Fold + VisitMut { - as_folder(Spread { +pub fn spread(c: Config) -> impl Pass { + visit_mut_pass(Spread { c, vars: Default::default(), }) diff --git a/crates/swc_ecma_compat_es2015/src/sticky_regex.rs b/crates/swc_ecma_compat_es2015/src/sticky_regex.rs index ee76f7936df8..f773f197d760 100644 --- a/crates/swc_ecma_compat_es2015/src/sticky_regex.rs +++ b/crates/swc_ecma_compat_es2015/src/sticky_regex.rs @@ -1,7 +1,7 @@ use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::{quote_ident, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// Compile ES2015 sticky regex to an ES5 RegExp constructor @@ -18,8 +18,8 @@ use swc_trace_macro::swc_trace; /// ```js /// new RegExp("o+", "y") /// ``` -pub fn sticky_regex() -> impl 'static + Fold + VisitMut { - as_folder(StickyRegex) +pub fn sticky_regex() -> impl Pass { + visit_mut_pass(StickyRegex) } struct StickyRegex; diff --git a/crates/swc_ecma_compat_es2015/src/template_literal.rs b/crates/swc_ecma_compat_es2015/src/template_literal.rs index 3f423ead6b68..a98b169d1c36 100644 --- a/crates/swc_ecma_compat_es2015/src/template_literal.rs +++ b/crates/swc_ecma_compat_es2015/src/template_literal.rs @@ -6,11 +6,11 @@ use swc_common::{util::take::Take, BytePos, Spanned, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::{helper, perf::Parallel}; use swc_ecma_utils::{is_literal, prepend_stmts, private_ident, quote_ident, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn template_literal(c: Config) -> impl Fold + VisitMut { - as_folder(TemplateLiteral { +pub fn template_literal(c: Config) -> impl Pass { + visit_mut_pass(TemplateLiteral { c, ..Default::default() }) diff --git a/crates/swc_ecma_compat_es2015/src/typeof_symbol.rs b/crates/swc_ecma_compat_es2015/src/typeof_symbol.rs index d34e91e812f3..1e8c1b64011f 100644 --- a/crates/swc_ecma_compat_es2015/src/typeof_symbol.rs +++ b/crates/swc_ecma_compat_es2015/src/typeof_symbol.rs @@ -2,11 +2,11 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::{helper, perf::Parallel}; use swc_ecma_utils::{quote_str, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn typeof_symbol() -> impl VisitMut + Fold { - as_folder(TypeOfSymbol) +pub fn typeof_symbol() -> impl Pass { + visit_mut_pass(TypeOfSymbol) } #[derive(Clone, Copy)] diff --git a/crates/swc_ecma_compat_es2016/src/exponentiation.rs b/crates/swc_ecma_compat_es2016/src/exponentiation.rs index 84080545cb3a..2f515da4a2d5 100644 --- a/crates/swc_ecma_compat_es2016/src/exponentiation.rs +++ b/crates/swc_ecma_compat_es2016/src/exponentiation.rs @@ -3,7 +3,7 @@ use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{ParExplode, Parallel}; use swc_ecma_transforms_macros::parallel; use swc_ecma_utils::{member_expr, private_ident, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; /// `@babel/plugin-transform-exponentiation-operator` @@ -25,8 +25,8 @@ use swc_trace_macro::swc_trace; /// /// x = Math.pow(x, 3); /// ``` -pub fn exponentiation() -> impl Fold + VisitMut { - as_folder(Exponentiation::default()) +pub fn exponentiation() -> impl Pass { + visit_mut_pass(Exponentiation::default()) } #[derive(Default)] diff --git a/crates/swc_ecma_compat_es2016/src/lib.rs b/crates/swc_ecma_compat_es2016/src/lib.rs index 570d698b9947..b7e53f0f3d98 100644 --- a/crates/swc_ecma_compat_es2016/src/lib.rs +++ b/crates/swc_ecma_compat_es2016/src/lib.rs @@ -1,12 +1,12 @@ #![allow(clippy::boxed_local)] #![allow(clippy::vec_box)] -use swc_ecma_visit::Fold; +use swc_ecma_ast::Pass; pub use self::exponentiation::exponentiation; mod exponentiation; -pub fn es2016() -> impl Fold { +pub fn es2016() -> impl Pass { exponentiation() } diff --git a/crates/swc_ecma_compat_es2017/src/async_to_generator.rs b/crates/swc_ecma_compat_es2017/src/async_to_generator.rs index 1c62f0d86172..2592c8eac62f 100644 --- a/crates/swc_ecma_compat_es2017/src/async_to_generator.rs +++ b/crates/swc_ecma_compat_es2017/src/async_to_generator.rs @@ -11,7 +11,7 @@ use swc_ecma_utils::{ prepend_stmt, private_ident, quote_ident, ExprFactory, Remapper, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_trace_macro::swc_trace; @@ -35,8 +35,8 @@ use swc_trace_macro::swc_trace; /// yield bar(); /// }); /// ``` -pub fn async_to_generator(c: Config, unresolved_mark: Mark) -> impl Fold + VisitMut { - as_folder(AsyncToGenerator { +pub fn async_to_generator(c: Config, unresolved_mark: Mark) -> impl Pass { + visit_mut_pass(AsyncToGenerator { c, in_subclass: false, unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), diff --git a/crates/swc_ecma_compat_es2017/src/lib.rs b/crates/swc_ecma_compat_es2017/src/lib.rs index bded16d95c0e..208e9ed4f28f 100644 --- a/crates/swc_ecma_compat_es2017/src/lib.rs +++ b/crates/swc_ecma_compat_es2017/src/lib.rs @@ -1,12 +1,12 @@ use serde::Deserialize; use swc_common::Mark; -use swc_ecma_visit::Fold; +use swc_ecma_ast::Pass; pub use self::async_to_generator::async_to_generator; pub mod async_to_generator; -pub fn es2017(c: Config, unresolved_mark: Mark) -> impl Fold { +pub fn es2017(c: Config, unresolved_mark: Mark) -> impl Pass { async_to_generator(c.async_to_generator, unresolved_mark) } diff --git a/crates/swc_ecma_compat_es2018/src/lib.rs b/crates/swc_ecma_compat_es2018/src/lib.rs index 041032b2102c..2495f17e23bf 100644 --- a/crates/swc_ecma_compat_es2018/src/lib.rs +++ b/crates/swc_ecma_compat_es2018/src/lib.rs @@ -2,9 +2,8 @@ #![allow(clippy::boxed_local)] use serde::Deserialize; -use swc_common::chain; +use swc_ecma_ast::Pass; use swc_ecma_compat_common::regexp::{self, regexp}; -use swc_ecma_visit::Fold; pub use self::object_rest_spread::object_rest_spread; @@ -12,8 +11,8 @@ mod object_rest; pub mod object_rest_spread; mod object_spread; -pub fn es2018(c: Config) -> impl Fold { - chain!( +pub fn es2018(c: Config) -> impl Pass { + ( regexp(regexp::Config { dot_all_regex: true, has_indices: false, @@ -24,7 +23,7 @@ pub fn es2018(c: Config) -> impl Fold { unicode_regex: false, unicode_sets_regex: false, }), - object_rest_spread(c.object_rest_spread) + object_rest_spread(c.object_rest_spread), ) } diff --git a/crates/swc_ecma_compat_es2018/src/object_rest_spread.rs b/crates/swc_ecma_compat_es2018/src/object_rest_spread.rs index 2024e160e849..a591ad19d766 100644 --- a/crates/swc_ecma_compat_es2018/src/object_rest_spread.rs +++ b/crates/swc_ecma_compat_es2018/src/object_rest_spread.rs @@ -1,20 +1,20 @@ use serde::Deserialize; -use swc_common::chain; -use swc_ecma_visit::{as_folder, Fold, VisitMut}; +use swc_ecma_ast::Pass; +use swc_ecma_visit::visit_mut_pass; -use super::{object_rest::ObjectRest, object_spread::ObjectSpread}; +use crate::{object_rest::ObjectRest, object_spread::ObjectSpread}; // TODO: currently swc behaves like babel with // `ignoreFunctionLength` on /// `@babel/plugin-proposal-object-rest-spread` -pub fn object_rest_spread(config: Config) -> impl Fold + VisitMut { - chain!( - as_folder(ObjectRest { +pub fn object_rest_spread(config: Config) -> impl Pass { + ( + visit_mut_pass(ObjectRest { config, ..Default::default() }), - as_folder(ObjectSpread { config }) + visit_mut_pass(ObjectSpread { config }), ) } diff --git a/crates/swc_ecma_compat_es2019/src/lib.rs b/crates/swc_ecma_compat_es2019/src/lib.rs index 417b961cb318..f9beb1b7595f 100644 --- a/crates/swc_ecma_compat_es2019/src/lib.rs +++ b/crates/swc_ecma_compat_es2019/src/lib.rs @@ -1,9 +1,9 @@ -use swc_ecma_visit::Fold; +use swc_ecma_ast::Pass; pub use self::optional_catch_binding::optional_catch_binding; mod optional_catch_binding; -pub fn es2019() -> impl Fold { +pub fn es2019() -> impl Pass { optional_catch_binding() } diff --git a/crates/swc_ecma_compat_es2019/src/optional_catch_binding.rs b/crates/swc_ecma_compat_es2019/src/optional_catch_binding.rs index 354a7520d0c5..5eeede447ba4 100644 --- a/crates/swc_ecma_compat_es2019/src/optional_catch_binding.rs +++ b/crates/swc_ecma_compat_es2019/src/optional_catch_binding.rs @@ -1,12 +1,12 @@ use swc_ecma_ast::*; use swc_ecma_utils::private_ident; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; struct OptionalCatchBinding; -pub fn optional_catch_binding() -> impl Fold + VisitMut { - as_folder(OptionalCatchBinding) +pub fn optional_catch_binding() -> impl Pass { + visit_mut_pass(OptionalCatchBinding) } #[swc_trace] @@ -25,17 +25,18 @@ impl VisitMut for OptionalCatchBinding { #[cfg(test)] mod tests { - use swc_common::{chain, Mark}; + use swc_common::Mark; + use swc_ecma_ast::Pass; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::test; - use swc_ecma_visit::{as_folder, Fold}; + use swc_ecma_visit::visit_mut_pass; use crate::optional_catch_binding::OptionalCatchBinding; - pub fn tr() -> impl Fold { - chain!( + pub fn tr() -> impl Pass { + ( resolver(Mark::new(), Mark::new(), false), - as_folder(OptionalCatchBinding) + visit_mut_pass(OptionalCatchBinding), ) } diff --git a/crates/swc_ecma_compat_es2020/src/export_namespace_from.rs b/crates/swc_ecma_compat_es2020/src/export_namespace_from.rs index 6a5b9c890aa5..5bc25196bb7c 100644 --- a/crates/swc_ecma_compat_es2020/src/export_namespace_from.rs +++ b/crates/swc_ecma_compat_es2020/src/export_namespace_from.rs @@ -1,11 +1,11 @@ use swc_atoms::JsWord; use swc_ecma_ast::*; use swc_ecma_utils::private_ident; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut}; use swc_trace_macro::swc_trace; -pub fn export_namespace_from() -> impl Fold + VisitMut { - as_folder(ExportNamespaceFrom) +pub fn export_namespace_from() -> impl Pass { + visit_mut_pass(ExportNamespaceFrom) } struct ExportNamespaceFrom; diff --git a/crates/swc_ecma_compat_es2020/src/lib.rs b/crates/swc_ecma_compat_es2020/src/lib.rs index c116b3c6ded3..67a811850b69 100644 --- a/crates/swc_ecma_compat_es2020/src/lib.rs +++ b/crates/swc_ecma_compat_es2020/src/lib.rs @@ -1,6 +1,6 @@ use serde::Deserialize; -use swc_common::{chain, Mark}; -use swc_ecma_visit::Fold; +use swc_common::Mark; +use swc_ecma_ast::Pass; pub use self::{ export_namespace_from::export_namespace_from, nullish_coalescing::nullish_coalescing, @@ -11,8 +11,8 @@ mod export_namespace_from; pub mod nullish_coalescing; pub mod optional_chaining; -pub fn es2020(config: Config, unresolved_mark: Mark) -> impl Fold { - chain!( +pub fn es2020(config: Config, unresolved_mark: Mark) -> impl Pass { + ( nullish_coalescing(config.nullish_coalescing), optional_chaining(config.optional_chaining, unresolved_mark), export_namespace_from(), diff --git a/crates/swc_ecma_compat_es2020/src/nullish_coalescing.rs b/crates/swc_ecma_compat_es2020/src/nullish_coalescing.rs index 0014262eef2b..6b1e8dcdcd68 100644 --- a/crates/swc_ecma_compat_es2020/src/nullish_coalescing.rs +++ b/crates/swc_ecma_compat_es2020/src/nullish_coalescing.rs @@ -4,11 +4,11 @@ use serde::Deserialize; use swc_common::{util::take::Take, Span, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{alias_ident_for_simple_assign_tatget, alias_if_required, StmtLike}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn nullish_coalescing(c: Config) -> impl Fold + VisitMut + 'static { - as_folder(NullishCoalescing { +pub fn nullish_coalescing(c: Config) -> impl Pass + 'static { + visit_mut_pass(NullishCoalescing { c, ..Default::default() }) diff --git a/crates/swc_ecma_compat_es2020/src/optional_chaining.rs b/crates/swc_ecma_compat_es2020/src/optional_chaining.rs index 66c74a203cdf..e2a2fc7a5688 100644 --- a/crates/swc_ecma_compat_es2020/src/optional_chaining.rs +++ b/crates/swc_ecma_compat_es2020/src/optional_chaining.rs @@ -1,10 +1,11 @@ use serde::Deserialize; use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_compat_es2022::optional_chaining_impl::optional_chaining_impl; -use swc_ecma_visit::{as_folder, Fold, VisitMut}; +use swc_ecma_visit::visit_mut_pass; -pub fn optional_chaining(c: Config, unresolved_mark: Mark) -> impl Fold + VisitMut { - as_folder(optional_chaining_impl( +pub fn optional_chaining(c: Config, unresolved_mark: Mark) -> impl Pass { + visit_mut_pass(optional_chaining_impl( swc_ecma_compat_es2022::optional_chaining_impl::Config { no_document_all: c.no_document_all, pure_getter: c.pure_getter, diff --git a/crates/swc_ecma_compat_es2021/src/lib.rs b/crates/swc_ecma_compat_es2021/src/lib.rs index 5c7cb4944659..061287b9992e 100644 --- a/crates/swc_ecma_compat_es2021/src/lib.rs +++ b/crates/swc_ecma_compat_es2021/src/lib.rs @@ -1,9 +1,9 @@ -use swc_ecma_visit::{Fold, VisitMut}; +use swc_ecma_ast::Pass; pub use self::logical_assignments::logical_assignments; mod logical_assignments; -pub fn es2021() -> impl Fold + VisitMut { +pub fn es2021() -> impl Pass { logical_assignments() } diff --git a/crates/swc_ecma_compat_es2021/src/logical_assignments.rs b/crates/swc_ecma_compat_es2021/src/logical_assignments.rs index 22b0f8856af2..657d7d266a62 100644 --- a/crates/swc_ecma_compat_es2021/src/logical_assignments.rs +++ b/crates/swc_ecma_compat_es2021/src/logical_assignments.rs @@ -4,11 +4,11 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::{alias_ident_for, prepend_stmt}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; -pub fn logical_assignments() -> impl Fold + VisitMut { - as_folder(Operators::default()) +pub fn logical_assignments() -> impl Pass { + visit_mut_pass(Operators::default()) } #[derive(Debug, Default)] diff --git a/crates/swc_ecma_compat_es2022/src/class_properties/mod.rs b/crates/swc_ecma_compat_es2022/src/class_properties/mod.rs index 48ad1b8a2e2f..04204fd3247a 100644 --- a/crates/swc_ecma_compat_es2022/src/class_properties/mod.rs +++ b/crates/swc_ecma_compat_es2022/src/class_properties/mod.rs @@ -12,7 +12,7 @@ use swc_ecma_utils::{ ModuleItemLike, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_trace_macro::swc_trace; @@ -40,8 +40,8 @@ mod used_name; /// # Impl note /// /// We use custom helper to handle export default class -pub fn class_properties(config: Config, unresolved_mark: Mark) -> impl Fold + VisitMut { - as_folder(ClassProperties { +pub fn class_properties(config: Config, unresolved_mark: Mark) -> impl Pass { + visit_mut_pass(ClassProperties { c: config, private: PrivateRecord::new(), extra: ClassExtra::default(), diff --git a/crates/swc_ecma_compat_es2022/src/lib.rs b/crates/swc_ecma_compat_es2022/src/lib.rs index 28b937f8d7be..6c8178140edc 100644 --- a/crates/swc_ecma_compat_es2022/src/lib.rs +++ b/crates/swc_ecma_compat_es2022/src/lib.rs @@ -1,8 +1,8 @@ #![allow(clippy::vec_box)] -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_compat_common::regexp::{self, regexp}; -use swc_ecma_visit::Fold; pub use self::{ class_properties::class_properties, private_in_object::private_in_object, @@ -14,8 +14,8 @@ pub mod optional_chaining_impl; pub mod private_in_object; pub mod static_blocks; -pub fn es2022(config: Config, unresolved_mark: Mark) -> impl Fold { - chain!( +pub fn es2022(config: Config, unresolved_mark: Mark) -> impl Pass { + ( regexp(regexp::Config { dot_all_regex: true, has_indices: true, diff --git a/crates/swc_ecma_compat_es2022/src/private_in_object.rs b/crates/swc_ecma_compat_es2022/src/private_in_object.rs index f0bfe4a97062..d6f41993b0c5 100644 --- a/crates/swc_ecma_compat_es2022/src/private_in_object.rs +++ b/crates/swc_ecma_compat_es2022/src/private_in_object.rs @@ -9,15 +9,14 @@ use swc_common::{ DUMMY_SP, }; use swc_ecma_ast::*; -use swc_ecma_transforms_base::pass::JsPass; use swc_ecma_utils::{default_constructor, prepend_stmt, private_ident, quote_ident, ExprFactory}; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; /// /~https://github.com/tc39/proposal-private-fields-in-in -pub fn private_in_object() -> impl JsPass { - as_folder(PrivateInObject::default()) +pub fn private_in_object() -> impl Pass { + visit_mut_pass(PrivateInObject::default()) } #[derive(Debug)] @@ -107,7 +106,7 @@ struct ClassData { } impl CompilerPass for PrivateInObject { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { Cow::Borrowed("private-in-object") } } diff --git a/crates/swc_ecma_compat_es2022/src/static_blocks.rs b/crates/swc_ecma_compat_es2022/src/static_blocks.rs index 1d4f109ab831..f7c41dc3a94e 100644 --- a/crates/swc_ecma_compat_es2022/src/static_blocks.rs +++ b/crates/swc_ecma_compat_es2022/src/static_blocks.rs @@ -2,15 +2,15 @@ use swc_atoms::JsWord; use swc_common::{collections::AHashSet, util::take::Take, Mark, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::ExprFactory; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; struct ClassStaticBlock { static_block_mark: Mark, } -pub fn static_blocks(static_block_mark: Mark) -> impl Fold + VisitMut { - as_folder(ClassStaticBlock { static_block_mark }) +pub fn static_blocks(static_block_mark: Mark) -> impl Pass { + visit_mut_pass(ClassStaticBlock { static_block_mark }) } #[swc_trace] diff --git a/crates/swc_ecma_compat_es3/src/lib.rs b/crates/swc_ecma_compat_es3/src/lib.rs index c8556f53aad9..a93ccd6de627 100644 --- a/crates/swc_ecma_compat_es3/src/lib.rs +++ b/crates/swc_ecma_compat_es3/src/lib.rs @@ -1,5 +1,4 @@ -use swc_common::chain; -use swc_ecma_visit::Fold; +use swc_ecma_ast::Pass; pub use self::{ member_expr_lits::member_expression_literals, prop_lits::property_literals, @@ -11,10 +10,10 @@ mod prop_lits; mod reserved_word; /// Make output es3-compatible. -pub fn es3(preserve_import: bool) -> impl Fold { - chain!( +pub fn es3(preserve_import: bool) -> impl Pass { + ( property_literals(), member_expression_literals(), - reserved_words(preserve_import) + reserved_words(preserve_import), ) } diff --git a/crates/swc_ecma_compat_es3/src/member_expr_lits.rs b/crates/swc_ecma_compat_es3/src/member_expr_lits.rs index ef43cce475c8..b29b0bde5de2 100644 --- a/crates/swc_ecma_compat_es3/src/member_expr_lits.rs +++ b/crates/swc_ecma_compat_es3/src/member_expr_lits.rs @@ -1,6 +1,6 @@ use swc_ecma_ast::*; use swc_ecma_utils::is_valid_ident; -use swc_ecma_visit::{standard_only_fold, Fold, FoldWith}; +use swc_ecma_visit::{fold_pass, standard_only_fold, Fold, FoldWith}; use swc_trace_macro::swc_trace; /// babel: `transform-member-expression-literals` @@ -20,8 +20,8 @@ use swc_trace_macro::swc_trace; /// obj["const"] = "isKeyword"; /// obj["var"] = "isKeyword"; /// ``` -pub fn member_expression_literals() -> impl Fold { - MemberExprLit +pub fn member_expression_literals() -> impl Pass { + fold_pass(MemberExprLit) } #[derive(Default, Clone, Copy)] struct MemberExprLit; @@ -31,7 +31,7 @@ impl Fold for MemberExprLit { standard_only_fold!(); fn fold_member_expr(&mut self, e: MemberExpr) -> MemberExpr { - let e = e.fold_children_with(self); + let e: MemberExpr = e.fold_children_with(self); if let MemberProp::Ident(i) = e.prop { if i.sym.is_reserved() || i.sym.is_reserved_in_strict_mode(true) @@ -71,7 +71,7 @@ mod tests { test!( ::swc_ecma_parser::Syntax::default(), - |_| MemberExprLit, + |_| fold_pass(MemberExprLit), basic, r#"obj["foo"] = "isValid"; @@ -82,14 +82,14 @@ obj["var"] = "isKeyword";"#, test!( ::swc_ecma_parser::Syntax::default(), - |_| MemberExprLit, + |_| fold_pass(MemberExprLit), issue_206, "const number = foo[bar1][baz1]" ); test!( ::swc_ecma_parser::Syntax::default(), - |_| MemberExprLit, + |_| fold_pass(MemberExprLit), issue_211, "_query[idx]=$this.attr('data-ref');" ); diff --git a/crates/swc_ecma_compat_es3/src/prop_lits.rs b/crates/swc_ecma_compat_es3/src/prop_lits.rs index bf22ba042dc1..81d2e213eadd 100644 --- a/crates/swc_ecma_compat_es3/src/prop_lits.rs +++ b/crates/swc_ecma_compat_es3/src/prop_lits.rs @@ -1,6 +1,6 @@ use swc_ecma_ast::*; use swc_ecma_utils::is_valid_ident; -use swc_ecma_visit::{standard_only_fold, Fold, FoldWith}; +use swc_ecma_visit::{fold_pass, standard_only_fold, Fold, FoldWith}; use swc_trace_macro::swc_trace; /// babel: `transform-property-literals` @@ -30,8 +30,8 @@ use swc_trace_macro::swc_trace; /// foo: 1 /// }; /// ``` -pub fn property_literals() -> impl Fold { - PropertyLiteral +pub fn property_literals() -> impl Pass { + fold_pass(PropertyLiteral) } struct PropertyLiteral; @@ -78,7 +78,7 @@ mod tests { test!( ::swc_ecma_parser::Syntax::default(), - |_| PropertyLiteral, + |_| fold_pass(PropertyLiteral), babel_basic, r#"var foo = { // changed @@ -95,7 +95,7 @@ mod tests { test!( ::swc_ecma_parser::Syntax::default(), - |_| PropertyLiteral, + |_| fold_pass(PropertyLiteral), str_lit, r#"'use strict'; var x = { diff --git a/crates/swc_ecma_compat_es3/src/reserved_word.rs b/crates/swc_ecma_compat_es3/src/reserved_word.rs index ada479f5d787..280c1575528b 100644 --- a/crates/swc_ecma_compat_es3/src/reserved_word.rs +++ b/crates/swc_ecma_compat_es3/src/reserved_word.rs @@ -1,6 +1,6 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// babel: `@babel/plugin-transform-reserved-words` /// @@ -19,8 +19,8 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWit /// var _abstract = 1; /// var x = _abstract + 1; /// ``` -pub fn reserved_words(preserve_import: bool) -> impl VisitMut + Fold { - as_folder(ReservedWord { preserve_import }) +pub fn reserved_words(preserve_import: bool) -> impl Pass { + visit_mut_pass(ReservedWord { preserve_import }) } struct ReservedWord { pub preserve_import: bool, diff --git a/crates/swc_ecma_ext_transforms/src/jest.rs b/crates/swc_ecma_ext_transforms/src/jest.rs index d0f792b3e802..b5c13bbd4853 100644 --- a/crates/swc_ecma_ext_transforms/src/jest.rs +++ b/crates/swc_ecma_ext_transforms/src/jest.rs @@ -2,7 +2,7 @@ use phf::phf_set; use swc_common::util::take::Take; use swc_ecma_ast::*; use swc_ecma_utils::{prepend_stmts, StmtLike}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; static HOIST_METHODS: phf::Set<&str> = phf_set![ "mock", @@ -12,8 +12,8 @@ static HOIST_METHODS: phf::Set<&str> = phf_set![ "deepUnmock" ]; -pub fn jest() -> impl Fold + VisitMut { - as_folder(Jest::default()) +pub fn jest() -> impl Pass { + visit_mut_pass(Jest::default()) } #[derive(Default)] diff --git a/crates/swc_ecma_lints/examples/all.rs b/crates/swc_ecma_lints/examples/all.rs index 9504c5246456..68553302521b 100644 --- a/crates/swc_ecma_lints/examples/all.rs +++ b/crates/swc_ecma_lints/examples/all.rs @@ -3,7 +3,6 @@ use swc_ecma_ast::*; use swc_ecma_lints::{rule::Rule, rules::LintParams}; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; -use swc_ecma_visit::VisitMutWith; fn main() { // testing::run_test creates Lrc and `&Handler` for you @@ -33,7 +32,7 @@ fn main() { let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark); let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark); - program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); + program.mutate(resolver(unresolved_mark, top_level_mark, false)); let mut rules = swc_ecma_lints::rules::all(LintParams { program: &program, diff --git a/crates/swc_ecma_lints/src/rules/mod.rs b/crates/swc_ecma_lints/src/rules/mod.rs index 94ba9a69881e..0745f79e6dbc 100644 --- a/crates/swc_ecma_lints/src/rules/mod.rs +++ b/crates/swc_ecma_lints/src/rules/mod.rs @@ -2,7 +2,6 @@ use std::sync::Arc; use swc_common::{SourceMap, SyntaxContext}; use swc_ecma_ast::*; -use swc_ecma_visit::{noop_fold_type, Fold}; use crate::{config::LintConfig, rule::Rule}; @@ -217,34 +216,25 @@ pub fn all(lint_params: LintParams) -> Vec> { rules } -pub fn lint_to_fold(r: R) -> impl Fold +pub fn lint_to_fold(r: R) -> impl Pass where R: Rule, { - LintFolder(r) + LintPass(r) } -struct LintFolder(R) +struct LintPass(R) where R: Rule; -impl Fold for LintFolder +impl Pass for LintPass where R: Rule, { - noop_fold_type!(); - - #[inline(always)] - fn fold_module(&mut self, program: Module) -> Module { - self.0.lint_module(&program); - - program - } - - #[inline(always)] - fn fold_script(&mut self, program: Script) -> Script { - self.0.lint_script(&program); - - program + fn process(&mut self, program: &mut Program) { + match program { + Program::Module(m) => self.0.lint_module(m), + Program::Script(s) => self.0.lint_script(s), + } } } diff --git a/crates/swc_ecma_lints/tests/fixture.rs b/crates/swc_ecma_lints/tests/fixture.rs index 57a1d79c65be..bf09c5fa0b8d 100644 --- a/crates/swc_ecma_lints/tests/fixture.rs +++ b/crates/swc_ecma_lints/tests/fixture.rs @@ -9,7 +9,6 @@ use swc_ecma_lints::{ }; use swc_ecma_parser::{lexer::Lexer, Parser, Syntax}; use swc_ecma_transforms_base::resolver; -use swc_ecma_visit::VisitMutWith; #[testing::fixture("tests/pass/**/input.js")] #[testing::fixture("tests/pass/**/input.ts")] @@ -42,7 +41,7 @@ fn pass(input: PathBuf) { let need_ts = input.extension().unwrap() == "ts" || input.extension().unwrap() == "tsx"; - program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, need_ts)); + program.mutate(resolver(unresolved_mark, top_level_mark, need_ts)); let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark); let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark); diff --git a/crates/swc_ecma_minifier/benches/full.rs b/crates/swc_ecma_minifier/benches/full.rs index 02f6b22c371a..2c4f9a588a3a 100644 --- a/crates/swc_ecma_minifier/benches/full.rs +++ b/crates/swc_ecma_minifier/benches/full.rs @@ -7,6 +7,7 @@ use std::fs::read_to_string; use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion}; use swc_allocator::Allocator; use swc_common::{errors::HANDLER, sync::Lrc, FileName, Mark, SourceMap}; +use swc_ecma_ast::Program; use swc_ecma_codegen::text_writer::JsWriter; use swc_ecma_minifier::{ optimize, @@ -14,7 +15,6 @@ use swc_ecma_minifier::{ }; use swc_ecma_parser::parse_file_as_module; use swc_ecma_transforms_base::{fixer::fixer, resolver}; -use swc_ecma_visit::FoldWith; pub fn bench_files(c: &mut Criterion) { let mut group = c.benchmark_group("es/minifier/libs"); @@ -69,11 +69,12 @@ fn run(src: &str) { .map_err(|err| { err.into_diagnostic(&handler).emit(); }) - .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))) + .map(Program::Module) + .map(|module| module.apply(resolver(unresolved_mark, top_level_mark, false))) .unwrap(); let output = optimize( - program.into(), + program, cm.clone(), None, None, @@ -97,10 +98,9 @@ fn run(src: &str) { top_level_mark, mangle_name_cache: None, }, - ) - .expect_module(); + ); - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(fixer(None)); let code = print(cm, &[output], true); diff --git a/crates/swc_ecma_minifier/examples/compress.rs b/crates/swc_ecma_minifier/examples/compress.rs index af4805c6558c..f72c99a9f63b 100644 --- a/crates/swc_ecma_minifier/examples/compress.rs +++ b/crates/swc_ecma_minifier/examples/compress.rs @@ -9,6 +9,7 @@ extern crate swc_malloc; use std::{env::args, fs, path::Path}; use swc_common::{sync::Lrc, Mark, SourceMap}; +use swc_ecma_ast::Program; use swc_ecma_codegen::text_writer::JsWriter; use swc_ecma_minifier::{ optimize, @@ -19,7 +20,6 @@ use swc_ecma_transforms_base::{ fixer::{fixer, paren_remover}, resolver, }; -use swc_ecma_visit::FoldWith; fn main() { let file = args().nth(1).expect("should provide a path to file"); @@ -42,12 +42,13 @@ fn main() { .map_err(|err| { err.into_diagnostic(&handler).emit(); }) - .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))) - .map(|module| module.fold_with(&mut paren_remover(None))) + .map(Program::Module) + .map(|module| module.apply(resolver(unresolved_mark, top_level_mark, false))) + .map(|module| module.apply(paren_remover(None))) .unwrap(); let output = optimize( - program.into(), + program, cm.clone(), None, None, @@ -61,10 +62,9 @@ fn main() { top_level_mark, mangle_name_cache: None, }, - ) - .expect_module(); + ); - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(fixer(None)); let code = print(cm, &[output], false); diff --git a/crates/swc_ecma_minifier/examples/minifier.rs b/crates/swc_ecma_minifier/examples/minifier.rs index eeb423488a15..4cd71a427f75 100644 --- a/crates/swc_ecma_minifier/examples/minifier.rs +++ b/crates/swc_ecma_minifier/examples/minifier.rs @@ -5,6 +5,7 @@ extern crate swc_malloc; use std::{env::args, fs, path::Path, sync::Arc}; use swc_common::{errors::HANDLER, sync::Lrc, Mark, SourceMap}; +use swc_ecma_ast::Program; use swc_ecma_codegen::text_writer::{omit_trailing_semi, JsWriter}; use swc_ecma_minifier::{ optimize, @@ -15,7 +16,6 @@ use swc_ecma_transforms_base::{ fixer::{fixer, paren_remover}, resolver, }; -use swc_ecma_visit::FoldWith; fn main() { let file = args().nth(1).expect("should provide a path to file"); @@ -39,12 +39,13 @@ fn main() { .map_err(|err| { err.into_diagnostic(&handler).emit(); }) - .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))) - .map(|module| module.fold_with(&mut paren_remover(None))) + .map(Program::Module) + .map(|module| module.apply(resolver(unresolved_mark, top_level_mark, false))) + .map(|module| module.apply(paren_remover(None))) .unwrap(); let output = optimize( - program.into(), + program, cm.clone(), None, None, @@ -62,10 +63,9 @@ fn main() { // Mangle name cache example. You may not need this. mangle_name_cache: Some(Arc::new(SimpleMangleCache::default())), }, - ) - .expect_module(); + ); - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(fixer(None)); let code = print(cm, &[output], true); diff --git a/crates/swc_ecma_minifier/examples/minify-all.rs b/crates/swc_ecma_minifier/examples/minify-all.rs index 215eaaa4fd2c..e7ab881ddb89 100644 --- a/crates/swc_ecma_minifier/examples/minify-all.rs +++ b/crates/swc_ecma_minifier/examples/minify-all.rs @@ -7,6 +7,7 @@ use std::{env, fs, path::PathBuf, time::Instant}; use anyhow::Result; use rayon::prelude::*; use swc_common::{errors::HANDLER, sync::Lrc, Mark, SourceMap, GLOBALS}; +use swc_ecma_ast::Program; use swc_ecma_codegen::text_writer::JsWriter; use swc_ecma_minifier::{ optimize, @@ -17,7 +18,6 @@ use swc_ecma_transforms_base::{ fixer::{fixer, paren_remover}, resolver, }; -use swc_ecma_visit::FoldWith; use walkdir::WalkDir; fn main() { @@ -48,18 +48,15 @@ fn main() { .map_err(|err| { err.into_diagnostic(&handler).emit(); }) + .map(Program::Module) .map(|module| { - module.fold_with(&mut resolver( - unresolved_mark, - top_level_mark, - false, - )) + module.apply(&mut resolver(unresolved_mark, top_level_mark, false)) }) - .map(|module| module.fold_with(&mut paren_remover(None))) + .map(|module| module.apply(&mut paren_remover(None))) .unwrap(); let output = optimize( - program.into(), + program, cm.clone(), None, None, @@ -76,10 +73,9 @@ fn main() { top_level_mark, mangle_name_cache: None, }, - ) - .expect_module(); + ); - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(&mut fixer(None)); let code = print(cm.clone(), &[output], true); diff --git a/crates/swc_ecma_minifier/src/compress/mod.rs b/crates/swc_ecma_minifier/src/compress/mod.rs index af294241e8ba..bddeb124447a 100644 --- a/crates/swc_ecma_minifier/src/compress/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/mod.rs @@ -6,16 +6,13 @@ use std::{borrow::Cow, fmt::Write, time::Instant}; #[cfg(feature = "pretty_assertions")] use pretty_assertions::assert_eq; -use swc_common::{ - chain, - pass::{CompilerPass, Optional, Repeated}, -}; +use swc_common::pass::{CompilerPass, Optional, Repeated}; use swc_ecma_ast::*; 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, noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith, VisitWith}; use swc_timer::timer; use tracing::{debug, error}; @@ -54,15 +51,15 @@ where mode, }; - chain!( - as_folder(compressor), + ( + visit_mut_pass(compressor), Optional { enabled: options.evaluate || options.side_effects, - visitor: as_folder(expr_simplifier( + visitor: visit_mut_pass(expr_simplifier( marks.unresolved_mark, - ExprSimplifierConfig {} - )) - } + ExprSimplifierConfig {}, + )), + }, ) } @@ -79,7 +76,7 @@ struct Compressor<'a> { } impl CompilerPass for Compressor<'_> { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { "compressor".into() } } diff --git a/crates/swc_ecma_minifier/src/compress/util/mod.rs b/crates/swc_ecma_minifier/src/compress/util/mod.rs index b6397b1fcc97..c185e0d588f1 100644 --- a/crates/swc_ecma_minifier/src/compress/util/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/util/mod.rs @@ -2,11 +2,7 @@ use std::{cmp::Ordering, f64}; use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; -#[cfg(feature = "debug")] -use swc_ecma_transforms_base::fixer::fixer; use swc_ecma_utils::{number::JsNumber, ExprCtx, ExprExt, IdentUsageFinder, Value}; -#[cfg(feature = "debug")] -use swc_ecma_visit::{as_folder, FoldWith}; use swc_ecma_visit::{ noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; @@ -351,29 +347,11 @@ pub(crate) fn negate_cost( } })(); - // Print more info while testing negate_cost - #[cfg(test)] - { - trace_op!( - "negation cost of `{}` = {}", - dump(&e.clone().fold_with(&mut as_folder(fixer(None))), true), - cost, - ); - } - cost } let cost = cost(expr_ctx, e, in_bool_ctx, None, is_ret_val_ignored); - trace_op!( - "negation cost of `{}` = {}\nin_book_ctx={:?}\nis_ret_val_ignored={:?}", - dump(&e.clone().fold_with(&mut as_folder(fixer(None))), false), - cost, - in_bool_ctx, - is_ret_val_ignored - ); - cost } diff --git a/crates/swc_ecma_minifier/src/compress/util/tests.rs b/crates/swc_ecma_minifier/src/compress/util/tests.rs index 1bd5dd5ede11..78bafe32e8d6 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::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use tracing::{info, warn}; use super::negate_cost; @@ -40,8 +40,8 @@ fn assert_negate_cost(s: &str, in_bool_ctx: bool, is_ret_val_ignored: bool, expe e.visit_mut_with(&mut UnwrapParen); let input = { - let e = e.clone(); - let e = e.fold_with(&mut fixer(None)); + let mut e = e.clone(); + e.visit_mut_with(&mut fixer(None)); dump(&e, true) }; @@ -54,7 +54,7 @@ fn assert_negate_cost(s: &str, in_bool_ctx: bool, is_ret_val_ignored: bool, expe let real = { let mut real = e.clone(); negate(&expr_ctx, &mut real, in_bool_ctx, is_ret_val_ignored); - let real = real.fold_with(&mut fixer(None)); + real.visit_mut_with(&mut fixer(None)); dump(&real, true) }; diff --git a/crates/swc_ecma_minifier/src/debug.rs b/crates/swc_ecma_minifier/src/debug.rs index 13ff42c63fdc..72854e825bac 100644 --- a/crates/swc_ecma_minifier/src/debug.rs +++ b/crates/swc_ecma_minifier/src/debug.rs @@ -9,7 +9,7 @@ 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::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use tracing::debug; pub(crate) struct Debugger {} @@ -79,10 +79,9 @@ pub(crate) fn invoke_module(module: &Module) { return; } - let module = module - .clone() - .fold_with(&mut hygiene()) - .fold_with(&mut fixer(None)); + let module = Program::Module(module.clone()) + .apply(hygiene()) + .apply(fixer(None)); let module = drop_span(module); let mut buf = Vec::new(); @@ -96,7 +95,7 @@ pub(crate) fn invoke_module(module: &Module) { wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)), }; - emitter.emit_module(&module).unwrap(); + emitter.emit_program(&module).unwrap(); } let code = String::from_utf8(buf).unwrap(); @@ -171,10 +170,9 @@ pub(crate) fn invoke_script(script: &Script) { return; } - let script = script - .clone() - .fold_with(&mut hygiene()) - .fold_with(&mut fixer(None)); + let script = Program::Script(script.clone()) + .apply(hygiene()) + .apply(fixer(None)); let script = drop_span(script); let mut buf = Vec::new(); @@ -188,7 +186,7 @@ pub(crate) fn invoke_script(script: &Script) { wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)), }; - emitter.emit_script(&script).unwrap(); + emitter.emit_program(&script).unwrap(); } let code = String::from_utf8(buf).unwrap(); diff --git a/crates/swc_ecma_minifier/src/pass/global_defs.rs b/crates/swc_ecma_minifier/src/pass/global_defs.rs index 84ea6df30d59..137830989ac7 100644 --- a/crates/swc_ecma_minifier/src/pass/global_defs.rs +++ b/crates/swc_ecma_minifier/src/pass/global_defs.rs @@ -28,7 +28,7 @@ struct GlobalDefs { } impl CompilerPass for GlobalDefs { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { Cow::Borrowed("global-defs") } } diff --git a/crates/swc_ecma_minifier/src/util/unit.rs b/crates/swc_ecma_minifier/src/util/unit.rs index c3a5ac545174..703504803c21 100644 --- a/crates/swc_ecma_minifier/src/util/unit.rs +++ b/crates/swc_ecma_minifier/src/util/unit.rs @@ -5,13 +5,9 @@ use std::fmt::Debug; use swc_ecma_ast::*; use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene}; use swc_ecma_utils::DropSpan; -#[cfg(debug_assertions)] -use swc_ecma_visit::VisitWith; -use swc_ecma_visit::{as_folder, FoldWith, VisitMut, VisitMutWith}; +use swc_ecma_visit::{visit_mut_pass, VisitMut, VisitMutWith}; use crate::debug::dump; -#[cfg(debug_assertions)] -use crate::debug::AssertValid; /// Indicates a unit of minifaction. pub(crate) trait CompileUnit: @@ -51,11 +47,10 @@ impl CompileUnit for Module { tracing::subscriber::set_default(tracing::subscriber::NoSubscriber::default()); dump( - &self - .clone() - .fold_with(&mut fixer(None)) - .fold_with(&mut hygiene()) - .fold_with(&mut as_folder(DropSpan {})), + &Program::Module(self.clone()) + .apply(fixer(None)) + .apply(hygiene()) + .apply(visit_mut_pass(DropSpan {})), true, ) } @@ -80,11 +75,10 @@ impl CompileUnit for Script { tracing::subscriber::set_default(tracing::subscriber::NoSubscriber::default()); dump( - &self - .clone() - .fold_with(&mut fixer(None)) - .fold_with(&mut hygiene()) - .fold_with(&mut as_folder(DropSpan {})), + &Program::Script(self.clone()) + .apply(fixer(None)) + .apply(hygiene()) + .apply(visit_mut_pass(DropSpan {})), true, ) } @@ -98,34 +92,3 @@ impl CompileUnit for Script { crate::debug::invoke_script(self); } } - -impl CompileUnit for FnExpr { - fn is_module() -> bool { - false - } - - fn force_dump(&self) -> String { - let _noop_sub = - tracing::subscriber::set_default(tracing::subscriber::NoSubscriber::default()); - - dump( - &self - .clone() - .fold_with(&mut fixer(None)) - .fold_with(&mut hygiene()) - .fold_with(&mut as_folder(DropSpan {})), - true, - ) - } - - fn apply(&mut self, visitor: &mut V) - where - V: VisitMut, - { - self.visit_mut_with(&mut *visitor); - #[cfg(debug_assertions)] - { - self.visit_with(&mut AssertValid); - } - } -} diff --git a/crates/swc_ecma_minifier/tests/compress.rs b/crates/swc_ecma_minifier/tests/compress.rs index 7b3696c05a9d..d0695854ea11 100644 --- a/crates/swc_ecma_minifier/tests/compress.rs +++ b/crates/swc_ecma_minifier/tests/compress.rs @@ -45,7 +45,7 @@ use swc_ecma_transforms_base::{ resolver, }; use swc_ecma_utils::drop_span; -use swc_ecma_visit::{FoldWith, VisitMut, VisitMutWith}; +use swc_ecma_visit::{VisitMut, VisitMutWith}; use testing::{assert_eq, unignore_fixture, DebugUsingDisplay, NormalizedOutput}; fn load_txt(filename: &str) -> Vec { @@ -261,7 +261,7 @@ fn run( output.visit_mut_with(&mut hygiene()) } - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(&mut fixer(None)); let end = Instant::now(); tracing::info!( @@ -539,7 +539,7 @@ fn fixture(input: PathBuf) { let expected = parser.parse_program().map_err(|err| { err.into_diagnostic(&handler).emit(); })?; - let mut expected = expected.fold_with(&mut fixer(None)); + let mut expected = expected.apply(&mut fixer(None)); expected = drop_span(expected); match &mut expected { @@ -595,7 +595,7 @@ fn fixture(input: PathBuf) { err.into_diagnostic(&handler).emit(); }) .ok()?; - let mut expected = expected.fold_with(&mut fixer(None)); + let mut expected = expected.apply(fixer(None)); expected = drop_span(expected); match &mut expected { Program::Module(m) => { diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 750c79b59493..f993d64ec9d1 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11,7 +11,7 @@ use swc_common::{ sync::Lrc, FileName, Mark, SourceMap, }; -use swc_ecma_ast::Module; +use swc_ecma_ast::Program; use swc_ecma_codegen::{ text_writer::{omit_trailing_semi, JsWriter, WriteJs}, Emitter, @@ -26,7 +26,7 @@ use swc_ecma_minifier::{ use swc_ecma_parser::{parse_file_as_module, EsSyntax, Syntax}; use swc_ecma_testing::{exec_node_js, JsExecOptions}; use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver}; -use swc_ecma_visit::{FoldWith, VisitMutWith}; +use swc_ecma_visit::VisitMutWith; use testing::DebugUsingDisplay; use tracing::{info, span, Level}; @@ -97,7 +97,7 @@ fn run( input: &str, config: Option<&str>, mangle: Option, -) -> Option { +) -> Option { let _ = rayon::ThreadPoolBuilder::new() .thread_name(|i| format!("rayon-{}", i + 1)) .build_global(); @@ -125,7 +125,8 @@ fn run( .map_err(|err| { err.into_diagnostic(handler).emit(); }) - .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))); + .map(Program::Module) + .map(|module| module.apply(&mut resolver(unresolved_mark, top_level_mark, false))); // Ignore parser errors. // @@ -138,7 +139,7 @@ fn run( let run_hygiene = mangle.is_none(); let mut output = optimize( - program.into(), + program, cm, Some(&comments), None, @@ -152,14 +153,13 @@ fn run( top_level_mark, mangle_name_cache: None, }, - ) - .expect_module(); + ); if run_hygiene { output.visit_mut_with(&mut hygiene()); } - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(&mut fixer(None)); Some(output) } diff --git a/crates/swc_ecma_minifier/tests/terser_exec.rs b/crates/swc_ecma_minifier/tests/terser_exec.rs index 89a2beb4fb28..fd13cab795de 100644 --- a/crates/swc_ecma_minifier/tests/terser_exec.rs +++ b/crates/swc_ecma_minifier/tests/terser_exec.rs @@ -37,7 +37,7 @@ use swc_ecma_transforms_base::{ hygiene::hygiene, resolver, }; -use swc_ecma_visit::{FoldWith, VisitMutWith}; +use swc_ecma_visit::VisitMutWith; use testing::assert_eq; #[testing::fixture( @@ -245,7 +245,7 @@ fn run(cm: Lrc, handler: &Handler, input: &Path, config: &str) -> Opt output.visit_mut_with(&mut hygiene()); - let output = output.fold_with(&mut fixer(None)); + let output = output.apply(&mut fixer(None)); let end = Instant::now(); tracing::info!( diff --git a/crates/swc_ecma_preset_env/benches/polyfills.rs b/crates/swc_ecma_preset_env/benches/polyfills.rs index ad3c5b4b6710..ca718fb3b3e9 100644 --- a/crates/swc_ecma_preset_env/benches/polyfills.rs +++ b/crates/swc_ecma_preset_env/benches/polyfills.rs @@ -1,9 +1,9 @@ use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; use swc_common::{comments::SingleThreadedComments, FileName, Mark}; +use swc_ecma_ast::Program; use swc_ecma_parser::{Parser, StringInput, Syntax}; use swc_ecma_preset_env::{preset_env, Config}; use swc_ecma_transforms::helpers::{Helpers, HELPERS}; -use swc_ecma_visit::FoldWith; fn run(b: &mut Bencher, src: &str, config: Config) { let _ = ::testing::run_test(false, |cm, handler| { @@ -28,7 +28,7 @@ fn run(b: &mut Bencher, src: &str, config: Config) { &mut Default::default(), ); - b.iter(|| black_box(module.clone().fold_with(&mut folder))); + b.iter(|| black_box(Program::Module(module.clone()).apply(&mut folder))); Ok(()) }) }); diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index f36724930927..591efc200a1b 100644 --- a/crates/swc_ecma_preset_env/src/lib.rs +++ b/crates/swc_ecma_preset_env/src/lib.rs @@ -9,7 +9,8 @@ pub use preset_env_base::{query::Targets, version::Version, BrowserData, Version use serde::Deserialize; use swc_atoms::{js_word, JsWord}; use swc_common::{ - chain, collections::AHashSet, comments::Comments, FromVariant, Mark, SyntaxContext, DUMMY_SP, + collections::AHashSet, comments::Comments, pass::Optional, FromVariant, Mark, SyntaxContext, + DUMMY_SP, }; use swc_ecma_ast::*; use swc_ecma_transforms::{ @@ -21,11 +22,10 @@ use swc_ecma_transforms::{ regexp::{self, regexp}, }, feature::FeatureFlag, - pass::{noop, Optional}, Assumptions, }; use swc_ecma_utils::{prepend_stmts, ExprFactory}; -use swc_ecma_visit::{as_folder, Fold, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{visit_mut_pass, VisitMut, VisitMutWith, VisitWith}; pub use self::transform_data::Feature; @@ -42,7 +42,7 @@ pub fn preset_env( c: Config, assumptions: Assumptions, feature_set: &mut FeatureFlag, -) -> impl Fold +) -> impl Pass where C: Comments + Clone, { @@ -53,7 +53,7 @@ where let (include, included_modules) = FeatureOrModule::split(c.include); let (exclude, excluded_modules) = FeatureOrModule::split(c.exclude); - let pass = noop(); + let pass = noop_pass(); macro_rules! should_enable { ($feature:ident, $default:expr) => {{ @@ -82,16 +82,16 @@ where if c.debug { println!("{}: {:?}", f.as_str(), enable); } - chain!($prev, Optional::new($pass, enable)) + ($prev, Optional::new($pass, enable)) }}; } - let pass = chain!( + let pass = ( pass, Optional::new( class_fields_use_set(assumptions.pure_getters), - assumptions.set_public_class_fields - ) + assumptions.set_public_class_fields, + ), ); let pass = { @@ -108,7 +108,7 @@ where || enable_unicode_property_regex || enable_unicode_regex; - chain!( + ( pass, Optional::new( regexp(regexp::Config { @@ -123,8 +123,8 @@ where unicode_regex: enable_unicode_regex, unicode_sets_regex: enable_unicode_sets_regex, }), - enable - ) + enable, + ), ) }; @@ -338,22 +338,22 @@ where println!("Targets: {:?}", targets); } - chain!( + ( pass, - as_folder(Polyfills { + visit_mut_pass(Polyfills { mode: c.mode, regenerator: should_enable!(Regenerator, true), corejs: c.core_js.unwrap_or(Version { major: 3, minor: 0, - patch: 0 + patch: 0, }), shipped_proposals: c.shipped_proposals, targets, includes: included_modules, excludes: excluded_modules, unresolved_mark, - }) + }), ) } diff --git a/crates/swc_ecma_preset_env/tests/test.rs b/crates/swc_ecma_preset_env/tests/test.rs index 74aac848570a..a4401e9b2ff5 100644 --- a/crates/swc_ecma_preset_env/tests/test.rs +++ b/crates/swc_ecma_preset_env/tests/test.rs @@ -13,8 +13,8 @@ use pretty_assertions::assert_eq; use serde::Deserialize; use serde_json::Value; use swc_common::{ - chain, collections::AHashMap, comments::SingleThreadedComments, errors::HANDLER, - input::StringInput, FromVariant, Mark, + collections::AHashMap, comments::SingleThreadedComments, errors::HANDLER, input::StringInput, + FromVariant, Mark, }; use swc_ecma_ast::*; use swc_ecma_codegen::Emitter; @@ -22,7 +22,7 @@ use swc_ecma_parser::{Parser, Syntax}; use swc_ecma_preset_env::{preset_env, Config, FeatureOrModule, Mode, Targets, Version}; use swc_ecma_transforms::{fixer, helpers}; use swc_ecma_utils::drop_span; -use swc_ecma_visit::{as_folder, FoldWith, VisitMut}; +use swc_ecma_visit::{visit_mut_pass, VisitMut}; use testing::{NormalizedOutput, Tester}; /// options.json file @@ -121,7 +121,7 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { Tester::new() .print_errors(|cm, handler| { - let mut pass = chain!( + let pass = ( preset_env( Mark::fresh(Mark::root()), Some(SingleThreadedComments::default()), @@ -152,10 +152,10 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { Default::default(), &mut Default::default(), ), - fixer(None) + fixer(None), ); - let print = |m: &Module| { + let print = |m: &Program| { let mut buf = Vec::new(); { let mut emitter = Emitter { @@ -170,7 +170,7 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { )), }; - emitter.emit_module(m).expect("failed to emit module"); + emitter.emit_program(m).expect("failed to emit module"); } String::from_utf8(buf).expect("invalid utf8 character detected") }; @@ -193,7 +193,7 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { } let actual = helpers::HELPERS.set(&Default::default(), || { - HANDLER.set(&handler, || module.fold_with(&mut pass)) + HANDLER.set(&handler, || Program::Module(module).apply(pass)) }); // debug mode? @@ -256,13 +256,13 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { _ => Ordering::Equal, }); - m + Program::Module(m) }; let expected_src = print(&expected); - if drop_span(actual.fold_with(&mut as_folder(Normalizer))) - == drop_span(expected.fold_with(&mut as_folder(Normalizer))) + if drop_span(actual.apply(&mut visit_mut_pass(Normalizer))) + == drop_span(expected.apply(&mut visit_mut_pass(Normalizer))) { return Ok(()); } diff --git a/crates/swc_ecma_transforms/src/lib.rs b/crates/swc_ecma_transforms/src/lib.rs index e59c20e2a5b3..82d82503e847 100644 --- a/crates/swc_ecma_transforms/src/lib.rs +++ b/crates/swc_ecma_transforms/src/lib.rs @@ -3,7 +3,7 @@ #![deny(unused)] pub use swc_ecma_transforms_base::{ - assumptions::Assumptions, feature, fixer, helpers, hygiene, pass, perf, resolver, + assumptions::Assumptions, feature, fixer, helpers, hygiene, perf, resolver, }; // TODO: May remove these reexports once swc_core directly reexports all #[cfg(feature = "swc_ecma_transforms_compat")] diff --git a/crates/swc_ecma_transforms/tests/decorators.rs b/crates/swc_ecma_transforms/tests/decorators.rs index 03d0082d40cc..d84ff6333d23 100644 --- a/crates/swc_ecma_transforms/tests/decorators.rs +++ b/crates/swc_ecma_transforms/tests/decorators.rs @@ -7,7 +7,8 @@ use std::{fs, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -19,7 +20,6 @@ use swc_ecma_transforms_module::common_js; use swc_ecma_transforms_proposal::{decorators, decorators::Config}; use swc_ecma_transforms_testing::{test, test_exec, test_fixture, Tester}; use swc_ecma_transforms_typescript::{strip, typescript}; -use swc_ecma_visit::Fold; fn ts() -> Syntax { Syntax::Typescript(TsSyntax { @@ -36,11 +36,11 @@ fn syntax(decorators_before_export: bool) -> Syntax { }) } -fn tr(_: &Tester) -> impl Fold { +fn tr(_: &Tester) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(Default::default()), class_fields_use_set(true), @@ -48,7 +48,7 @@ fn tr(_: &Tester) -> impl Fold { ) } -fn ts_transform(t: &Tester) -> impl Fold { +fn ts_transform(t: &Tester) -> impl Pass { simple_strip( t, Config { @@ -58,11 +58,11 @@ fn ts_transform(t: &Tester) -> impl Fold { ) } -fn simple_strip(_: &Tester, config: Config) -> impl Fold { +fn simple_strip(_: &Tester, config: Config) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( decorators(config), resolver(unresolved_mark, top_level_mark, false), typescript( @@ -71,7 +71,7 @@ fn simple_strip(_: &Tester, config: Config) -> impl Fold { ..Default::default() }, unresolved_mark, - top_level_mark + top_level_mark, ), class_fields_use_set(true), class_properties( @@ -79,13 +79,13 @@ fn simple_strip(_: &Tester, config: Config) -> impl Fold { set_public_fields: true, ..Default::default() }, - unresolved_mark - ) + unresolved_mark, + ), ) } /// Folder for `transformation_*` tests -fn transformation(t: &Tester) -> impl Fold { +fn transformation(t: &Tester) -> impl Pass { simple_strip(t, Default::default()) } @@ -1635,7 +1635,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -1670,7 +1670,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -1704,7 +1704,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -1825,7 +1825,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -1858,7 +1858,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -1911,7 +1911,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2050,7 +2050,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2083,7 +2083,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2206,7 +2206,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2240,7 +2240,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2359,7 +2359,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2391,7 +2391,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2532,7 +2532,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2582,7 +2582,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2617,7 +2617,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2743,7 +2743,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2779,7 +2779,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2832,7 +2832,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -2967,7 +2967,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -3105,7 +3105,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -3242,7 +3242,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -3277,7 +3277,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, @@ -3326,7 +3326,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -3542,14 +3542,14 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), decorators(decorators::Config { legacy: true, ..Default::default() }), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, decorators_legacy_interop_local_define_property, @@ -3579,7 +3579,7 @@ fn issue_395_syntax() -> ::swc_ecma_parser::Syntax { test!( issue_395_syntax(), - |_| chain!( + |_| ( decorators(Default::default()), common_js( Default::default(), @@ -3608,7 +3608,7 @@ class Demo { test!( issue_395_syntax(), - |_| chain!( + |_| ( decorators(Default::default()), common_js::common_js( Default::default(), @@ -3642,7 +3642,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -3680,7 +3680,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -3724,14 +3724,14 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), function_name(), classes(Default::default()), decorators(decorators::Config { legacy: true, ..Default::default() - }) + }), ) }, function_name_object, @@ -3764,14 +3764,14 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), function_name(), classes(Default::default()), decorators(decorators::Config { legacy: true, ..Default::default() - }) + }), ) }, function_name_export, @@ -3808,7 +3808,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -3836,7 +3836,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -3874,14 +3874,14 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), function_name(), classes(Default::default()), decorators(decorators::Config { legacy: true, ..Default::default() - }) + }), ) }, function_name_eval, @@ -3953,7 +3953,7 @@ fn fixture_exec(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(Config { legacy: true, @@ -3978,13 +3978,13 @@ fn legacy_only(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(Config { legacy: true, emit_metadata: false, use_define_for_class_fields: false, - }) + }), ) }, &input, @@ -4003,13 +4003,13 @@ fn legacy_metadata(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(Config { legacy: true, emit_metadata: true, use_define_for_class_fields: false, - }) + }), ) }, &input, diff --git a/crates/swc_ecma_transforms/tests/deno.rs b/crates/swc_ecma_transforms/tests/deno.rs index 4b00541d3078..2f2cc81062e9 100644 --- a/crates/swc_ecma_transforms/tests/deno.rs +++ b/crates/swc_ecma_transforms/tests/deno.rs @@ -4,7 +4,7 @@ ))] use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; use swc_ecma_parser::Syntax; use swc_ecma_transforms::{fixer, helpers::inject_helpers, hygiene, resolver}; use swc_ecma_transforms_proposal::{ @@ -28,7 +28,7 @@ fn run_test(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorator_2022_03(), explicit_resource_management(), @@ -44,7 +44,7 @@ fn run_test(input: PathBuf) { ts_enum_is_mutable: true, }, unresolved_mark, - top_level_mark + top_level_mark, ), fixer(None), hygiene(), diff --git a/crates/swc_ecma_transforms/tests/es2015_function_name.rs b/crates/swc_ecma_transforms/tests/es2015_function_name.rs index 6945bc0238e2..acd9ddaa9760 100644 --- a/crates/swc_ecma_transforms/tests/es2015_function_name.rs +++ b/crates/swc_ecma_transforms/tests/es2015_function_name.rs @@ -5,7 +5,8 @@ feature = "swc_ecma_transforms_proposal", ))] -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -15,20 +16,19 @@ use swc_ecma_transforms_compat::{ use swc_ecma_transforms_module::common_js::common_js; use swc_ecma_transforms_proposal::decorators; use swc_ecma_transforms_testing::test; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() } -fn tr() -> impl Fold { +fn tr() -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), function_name(), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) } @@ -198,7 +198,7 @@ export const y = function () {}; test!( ignore, syntax(), - |_| chain!(arrow(Mark::new()), function_name()), + |_| (arrow(Mark::new()), function_name()), function_name_with_arrow_functions_transform, r#" const x = () => x; @@ -215,7 +215,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), function_name(), classes(Default::default()), @@ -227,8 +227,8 @@ test!( Default::default(), unresolved_mark, Default::default(), - Default::default() - ) + Default::default(), + ), ) }, function_name_modules_3, @@ -251,7 +251,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -276,7 +276,7 @@ test!( syntax(), |_| { let unresolved_mark = Mark::new(); - chain!( + ( arrow(unresolved_mark), shorthand(), function_name(), @@ -285,7 +285,7 @@ test!( unresolved_mark, Default::default(), Default::default(), - ) + ), ) }, function_name_export_default_arrow_renaming, @@ -390,14 +390,14 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, ..Default::default() }), function_name(), - classes(Default::default()) + classes(Default::default()), ) }, function_name_self_reference, @@ -415,7 +415,7 @@ f = null; test!( ignore, syntax(), - |_| chain!(arrow(Mark::new()), function_name()), + |_| (arrow(Mark::new()), function_name()), function_name_with_arrow_functions_transform_spec, r#" // These are actually handled by transform-arrow-functions @@ -433,7 +433,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -454,7 +454,7 @@ test!( test!( ignore, syntax(), - |_| chain!(arrow(Mark::new()), shorthand(), function_name()), + |_| (arrow(Mark::new()), shorthand(), function_name()), function_name_export_default_arrow_renaming_module_es6, r#" export default (a) => { @@ -494,7 +494,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -532,7 +532,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -565,7 +565,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -611,7 +611,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -644,7 +644,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, @@ -657,7 +657,7 @@ test!( unresolved_mark, Default::default(), Default::default(), - ) + ), ) }, function_name_modules_2, @@ -684,7 +684,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), decorators(decorators::Config { legacy: true, diff --git a/crates/swc_ecma_transforms_base/benches/base.rs b/crates/swc_ecma_transforms_base/benches/base.rs index 95309fa0c40e..b263f3224faf 100644 --- a/crates/swc_ecma_transforms_base/benches/base.rs +++ b/crates/swc_ecma_transforms_base/benches/base.rs @@ -3,10 +3,10 @@ extern crate swc_malloc; use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; -use swc_common::{chain, errors::HANDLER, FileName, Mark}; +use swc_common::{errors::HANDLER, FileName, Mark}; +use swc_ecma_ast::Program; use swc_ecma_parser::{Parser, StringInput, Syntax}; use swc_ecma_transforms_base::helpers; -use swc_ecma_visit::FoldWith; static SOURCE: &str = include_str!("../../swc_ecma_minifier/benches/full/typescript.js"); @@ -24,11 +24,9 @@ macro_rules! tr { ); let module = parser.parse_module().map_err(|_| ()).unwrap(); helpers::HELPERS.set(&Default::default(), || { - let mut tr = $tr(); - $b.iter(|| { - let module = module.clone(); - black_box(module.fold_with(&mut tr)) + let module = Program::Module(module.clone()); + black_box(module.apply($tr())) }); Ok(()) }) @@ -54,7 +52,7 @@ fn hygiene(b: &mut Bencher) { } fn resolver_with_hygiene(b: &mut Bencher) { - tr!(b, || chain!( + tr!(b, || ( swc_ecma_transforms_base::resolver(Mark::new(), Mark::new(), false), swc_ecma_transforms_base::hygiene::hygiene() )); diff --git a/crates/swc_ecma_transforms_base/benches/deps.rs b/crates/swc_ecma_transforms_base/benches/deps.rs index f912b1b2110f..0ff1e1c611de 100644 --- a/crates/swc_ecma_transforms_base/benches/deps.rs +++ b/crates/swc_ecma_transforms_base/benches/deps.rs @@ -2,9 +2,8 @@ use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Benc use swc_common::{FileName, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_parser::{lexer::Lexer, parse_file_as_module, Parser, StringInput, Syntax}; -use swc_ecma_transforms_base::pass::noop; use swc_ecma_utils::ExprFactory; -use swc_ecma_visit::{FoldWith, Visit, VisitWith}; +use swc_ecma_visit::{Fold, FoldWith, Visit, VisitWith}; static SOURCE: &str = include_str!("assets/AjaxObservable.ts"); @@ -55,9 +54,9 @@ fn fold_empty(b: &mut Bencher) { e.into_diagnostic(handler).emit(); } - let mut folder = noop(); + let mut folder = noop_pass(); - b.iter(|| black_box(module.clone().fold_with(&mut folder))); + b.iter(|| black_box(Program::Module(module.clone()).apply(&mut folder))); Ok(()) }); } @@ -82,9 +81,9 @@ fn fold_noop_impl_all(b: &mut Bencher) { e.into_diagnostic(handler).emit(); } - let mut folder = noop(); + let mut folder = noop_pass(); - b.iter(|| black_box(module.clone().fold_with(&mut folder))); + b.iter(|| black_box(Program::Module(module.clone()).apply(&mut folder))); Ok(()) }); } @@ -110,9 +109,9 @@ fn fold_noop_impl_vec(b: &mut Bencher) { e.into_diagnostic(handler).emit(); } - let mut folder = noop(); + let mut folder = noop_pass(); - b.iter(|| black_box(module.clone().fold_with(&mut folder))); + b.iter(|| black_box(Program::Module(module.clone()).apply(&mut folder))); Ok(()) }); } @@ -147,7 +146,7 @@ fn boxing_unboxed_clone(b: &mut Bencher) { fn boxing_boxed(b: &mut Bencher) { let _ = ::testing::run_test(false, |_, _| { - let mut folder = noop(); + let mut folder = noop_fold(); let expr = Box::new(mk_expr()); b.iter(|| black_box(expr.clone().fold_with(&mut folder))); @@ -157,7 +156,7 @@ fn boxing_boxed(b: &mut Bencher) { fn boxing_unboxed(b: &mut Bencher) { let _ = ::testing::run_test(false, |_, _| { - let mut folder = noop(); + let mut folder = noop_fold(); let expr = mk_expr(); b.iter(|| black_box(expr.clone().fold_with(&mut folder))); @@ -233,3 +232,9 @@ fn bench_cases(c: &mut Criterion) { criterion_group!(benches, bench_cases); criterion_main!(benches); + +fn noop_fold() -> impl Fold { + struct Noop; + impl Fold for Noop {} + Noop +} diff --git a/crates/swc_ecma_transforms_base/benches/parallel.rs b/crates/swc_ecma_transforms_base/benches/parallel.rs index 31346f8c63d0..c1feae49c58c 100644 --- a/crates/swc_ecma_transforms_base/benches/parallel.rs +++ b/crates/swc_ecma_transforms_base/benches/parallel.rs @@ -5,9 +5,9 @@ extern crate swc_malloc; use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; use rayon::prelude::*; use swc_common::{errors::HANDLER, FileName, Mark, GLOBALS}; +use swc_ecma_ast::Program; use swc_ecma_parser::{Parser, StringInput, Syntax}; use swc_ecma_transforms_base::helpers; -use swc_ecma_visit::FoldWith; static SOURCE: &str = include_str!("../../swc_ecma_minifier/benches/full/typescript.js"); @@ -22,7 +22,7 @@ macro_rules! tr { StringInput::from(&*fm), None, ); - let module = parser.parse_module().map_err(|_| ()).unwrap(); + let module = Program::Module(parser.parse_module().map_err(|_| ()).unwrap()); $b.iter(|| { GLOBALS.with(|globals| { @@ -30,10 +30,10 @@ macro_rules! tr { GLOBALS.set(globals, || { HANDLER.set(&handler, || { helpers::HELPERS.set(&Default::default(), || { - let mut tr = $tr(); + let tr = $tr(); let module = module.clone(); - black_box(module.fold_with(&mut tr)); + black_box(module.apply(tr)); }) }) }) diff --git a/crates/swc_ecma_transforms_base/src/fixer.rs b/crates/swc_ecma_transforms_base/src/fixer.rs index 9c21d8f0ae91..546862ffc445 100644 --- a/crates/swc_ecma_transforms_base/src/fixer.rs +++ b/crates/swc_ecma_transforms_base/src/fixer.rs @@ -5,15 +5,15 @@ use rustc_hash::FxHasher; use swc_common::{comments::Comments, util::take::Take, Span, Spanned, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::stack_size::maybe_grow_default; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// Fixes ast nodes before printing so semantics are preserved. /// /// You don't have to bother to create appropriate parenthesis. /// The pass will insert parenthesis as needed. In other words, it's /// okay to store `a * (b + c)` as `Bin { a * Bin { b + c } }`. -pub fn fixer(comments: Option<&dyn Comments>) -> impl '_ + Fold + VisitMut { - as_folder(Fixer { +pub fn fixer(comments: Option<&dyn Comments>) -> impl '_ + Pass + VisitMut { + visit_mut_pass(Fixer { comments, ctx: Default::default(), span_map: Default::default(), @@ -23,8 +23,8 @@ pub fn fixer(comments: Option<&dyn Comments>) -> impl '_ + Fold + VisitMut { }) } -pub fn paren_remover(comments: Option<&dyn Comments>) -> impl '_ + Fold + VisitMut { - as_folder(Fixer { +pub fn paren_remover(comments: Option<&dyn Comments>) -> impl '_ + Pass + VisitMut { + visit_mut_pass(Fixer { comments, ctx: Default::default(), span_map: Default::default(), @@ -1178,13 +1178,13 @@ fn will_eat_else_token(s: &Stmt) -> bool { #[cfg(test)] mod tests { - use crate::pass::noop; + use swc_ecma_ast::noop_pass; fn run_test(from: &str, to: &str) { crate::tests::test_transform( Default::default(), // test_transform has alreay included fixer - |_| noop(), + |_| noop_pass(), from, to, true, diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 18fb5907dbe8..58fc87e6a2fc 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -6,7 +6,7 @@ use swc_atoms::JsWord; use swc_common::{FileName, FilePathMapping, Mark, SourceMap, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{prepend_stmts, quote_ident, DropSpan, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; #[macro_export] macro_rules! enable_helper { @@ -420,8 +420,8 @@ define_helpers!(Helpers { using_ctx: (), }); -pub fn inject_helpers(global_mark: Mark) -> impl Fold + VisitMut { - as_folder(InjectHelpers { +pub fn inject_helpers(global_mark: Mark) -> impl Pass + VisitMut { + visit_mut_pass(InjectHelpers { global_mark, helper_ctxt: None, }) @@ -631,11 +631,9 @@ impl VisitMut for Marker { #[cfg(test)] mod tests { - use swc_ecma_visit::FoldWith; use testing::DebugUsingDisplay; use super::*; - use crate::pass::noop; #[test] fn external_helper() { @@ -643,7 +641,7 @@ mod tests { crate::tests::Tester::run(|tester| { HELPERS.set(&Helpers::new(true), || { let expected = tester.apply_transform( - as_folder(DropSpan), + DropSpan, "output.js", Default::default(), "import { _ as _throw } from \"@swc/helpers/_/_throw\"; @@ -653,11 +651,11 @@ _throw();", eprintln!("----- Actual -----"); - let tr = as_folder(inject_helpers(Mark::new())); + let tr = inject_helpers(Mark::new()); let actual = tester .apply_transform(tr, "input.js", Default::default(), input)? - .fold_with(&mut crate::hygiene::hygiene()) - .fold_with(&mut crate::fixer::fixer(None)); + .apply(crate::hygiene::hygiene()) + .apply(crate::fixer::fixer(None)); if actual == expected { return Ok(()); @@ -686,7 +684,7 @@ _throw();", Default::default(), |_| { enable_helper!(throw); - as_folder(inject_helpers(Mark::new())) + inject_helpers(Mark::new()) }, "'use strict'", "'use strict' @@ -705,7 +703,7 @@ function _throw(e) { Default::default(), |_| { enable_helper!(throw); - as_folder(inject_helpers(Mark::new())) + inject_helpers(Mark::new()) }, "let _throw = null", "function _throw(e) { @@ -722,7 +720,7 @@ let _throw1 = null; fn use_strict_abort() { crate::tests::test_transform( Default::default(), - |_| noop(), + |_| noop_pass(), "'use strict' let x = 4;", @@ -740,7 +738,7 @@ let x = 4;", Default::default(), |_| { enable_helper!(using_ctx); - as_folder(inject_helpers(Mark::new())) + inject_helpers(Mark::new()) }, "let _throw = null", r#" diff --git a/crates/swc_ecma_transforms_base/src/hygiene/mod.rs b/crates/swc_ecma_transforms_base/src/hygiene/mod.rs index f80bec258084..f7e27f71b42b 100644 --- a/crates/swc_ecma_transforms_base/src/hygiene/mod.rs +++ b/crates/swc_ecma_transforms_base/src/hygiene/mod.rs @@ -1,7 +1,7 @@ -use swc_common::{chain, Mark}; +use swc_common::Mark; use swc_ecma_ast::*; use swc_ecma_utils::stack_size::maybe_grow_default; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; pub use crate::rename::rename; use crate::rename::{renamer, Renamer}; @@ -28,7 +28,7 @@ pub struct Config { /// See [hygiene_with_config] for doc. Creates a `hygiene` pass with default /// value of [Config]. -pub fn hygiene() -> impl Fold + VisitMut + 'static { +pub fn hygiene() -> impl Pass + VisitMut { hygiene_with_config(Default::default()) } @@ -58,8 +58,11 @@ pub fn hygiene() -> impl Fold + VisitMut + 'static { /// ## Third phase /// /// At third phase, we rename all identifiers in the queue. -pub fn hygiene_with_config(config: Config) -> impl 'static + Fold + VisitMut { - chain!(renamer(config, HygieneRenamer), as_folder(HygieneRemover)) +pub fn hygiene_with_config(config: Config) -> impl 'static + Pass + VisitMut { + ( + renamer(config, HygieneRenamer), + visit_mut_pass(HygieneRemover), + ) } struct HygieneRenamer; diff --git a/crates/swc_ecma_transforms_base/src/hygiene/tests.rs b/crates/swc_ecma_transforms_base/src/hygiene/tests.rs index 0bb4980b83b2..2e4b77679e8d 100644 --- a/crates/swc_ecma_transforms_base/src/hygiene/tests.rs +++ b/crates/swc_ecma_transforms_base/src/hygiene/tests.rs @@ -2,7 +2,7 @@ use swc_atoms::JsWord; use swc_common::{collections::AHashMap, hygiene::*, DUMMY_SP}; use swc_ecma_parser::Syntax; use swc_ecma_utils::quote_ident; -use swc_ecma_visit::FoldWith; +use swc_ecma_visit::{Fold, FoldWith}; use testing::{assert_eq, DebugUsingDisplay}; use super::*; @@ -82,19 +82,21 @@ where F: FnOnce(&mut crate::tests::Tester<'_>) -> Result, { crate::tests::Tester::run(|tester| { - let module = op(tester)?; + let mut module = Program::Module(op(tester)?); let hygiene_src = tester.print(&module.clone().fold_with(&mut HygieneVisualizer)); println!("----- Hygiene -----\n{}", hygiene_src); - let module = module.fold_with(&mut hygiene_with_config(config())); + hygiene_with_config(config()).process(&mut module); let actual = tester.print(&module); let expected = { - let expected = tester.with_parser("expected.js", Syntax::default(), expected, |p| { - p.parse_module() - })?; + let expected = tester + .with_parser("expected.js", Syntax::default(), expected, |p| { + p.parse_module() + }) + .map(Program::Module)?; tester.print(&expected) }; diff --git a/crates/swc_ecma_transforms_base/src/lib.rs b/crates/swc_ecma_transforms_base/src/lib.rs index 54696ee89274..67048e5baca1 100644 --- a/crates/swc_ecma_transforms_base/src/lib.rs +++ b/crates/swc_ecma_transforms_base/src/lib.rs @@ -13,7 +13,6 @@ pub mod feature; pub mod helpers; #[doc(hidden)] pub mod native; -pub mod pass; pub mod perf; pub mod quote; pub mod rename; diff --git a/crates/swc_ecma_transforms_base/src/pass.rs b/crates/swc_ecma_transforms_base/src/pass.rs deleted file mode 100644 index ac6465d165fb..000000000000 --- a/crates/swc_ecma_transforms_base/src/pass.rs +++ /dev/null @@ -1,29 +0,0 @@ -use swc_common::pass::CompilerPass; -pub use swc_common::pass::{Optional, Repeated}; -use swc_ecma_ast::{Module, Script}; -use swc_ecma_visit::Fold; - -pub fn noop() -> impl Fold { - Noop -} - -struct Noop; -impl Fold for Noop { - #[inline(always)] - fn fold_module(&mut self, m: Module) -> Module { - m - } - - #[inline(always)] - fn fold_script(&mut self, s: Script) -> Script { - s - } -} - -pub trait JsPass: CompilerPass + Fold {} - -impl JsPass for T where T: CompilerPass + Fold {} - -pub trait RepeatedJsPass: Repeated + JsPass {} - -impl RepeatedJsPass for T where T: Repeated + JsPass {} diff --git a/crates/swc_ecma_transforms_base/src/rename/mod.rs b/crates/swc_ecma_transforms_base/src/rename/mod.rs index f427d94fb503..e8495391c734 100644 --- a/crates/swc_ecma_transforms_base/src/rename/mod.rs +++ b/crates/swc_ecma_transforms_base/src/rename/mod.rs @@ -7,7 +7,9 @@ use swc_atoms::Atom; use swc_common::collections::AHashMap; use swc_ecma_ast::*; use swc_ecma_utils::stack_size::maybe_grow_default; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith, VisitWith}; +use swc_ecma_visit::{ + noop_visit_mut_type, visit_mut_pass, Fold, VisitMut, VisitMutWith, VisitWith, +}; #[cfg(feature = "concurrent-renamer")] use self::renamer_concurrent::{Send, Sync}; @@ -53,31 +55,31 @@ pub trait Renamer: Send + Sync { pub type RenameMap = FxHashMap; -pub fn rename(map: &RenameMap) -> impl '_ + Fold + VisitMut { +pub fn rename(map: &RenameMap) -> impl '_ + Pass + VisitMut { rename_with_config(map, Default::default()) } -pub fn rename_with_config(map: &RenameMap, config: Config) -> impl '_ + Fold + VisitMut { - as_folder(Operator { +pub fn rename_with_config(map: &RenameMap, config: Config) -> impl '_ + Pass + VisitMut { + visit_mut_pass(Operator { rename: map, config, extra: Default::default(), }) } -pub fn remap(map: &FxHashMap, config: Config) -> impl '_ + Fold + VisitMut { - as_folder(Operator { +pub fn remap(map: &FxHashMap, config: Config) -> impl '_ + Pass + VisitMut { + visit_mut_pass(Operator { rename: map, config, extra: Default::default(), }) } -pub fn renamer(config: Config, renamer: R) -> impl Fold + VisitMut +pub fn renamer(config: Config, renamer: R) -> impl Pass + VisitMut where R: Renamer, { - as_folder(RenamePass { + visit_mut_pass(RenamePass { config, renamer, preserved: Default::default(), diff --git a/crates/swc_ecma_transforms_base/src/resolver/mod.rs b/crates/swc_ecma_transforms_base/src/resolver/mod.rs index 599e600409b2..cdc807094dfa 100644 --- a/crates/swc_ecma_transforms_base/src/resolver/mod.rs +++ b/crates/swc_ecma_transforms_base/src/resolver/mod.rs @@ -7,7 +7,7 @@ use swc_common::{ use swc_ecma_ast::*; use swc_ecma_utils::{find_pat_ids, stack_size::maybe_grow_default}; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, visit_mut_obj_and_computed, Fold, VisitMut, VisitMutWith, + noop_visit_mut_type, visit_mut_obj_and_computed, visit_mut_pass, VisitMut, VisitMutWith, }; use tracing::{debug, span, Level}; @@ -134,7 +134,7 @@ pub fn resolver( unresolved_mark: Mark, top_level_mark: Mark, typescript: bool, -) -> impl 'static + Fold + VisitMut { +) -> impl 'static + Pass + VisitMut { assert_ne!( unresolved_mark, Mark::root(), @@ -144,7 +144,7 @@ pub fn resolver( let _ = SyntaxContext::empty().apply_mark(unresolved_mark); let _ = SyntaxContext::empty().apply_mark(top_level_mark); - as_folder(Resolver { + visit_mut_pass(Resolver { current: Scope::new(ScopeKind::Fn, top_level_mark, None), ident_type: IdentType::Ref, in_type: false, diff --git a/crates/swc_ecma_transforms_base/src/resolver/tests.rs b/crates/swc_ecma_transforms_base/src/resolver/tests.rs index d2f9a068fb28..a324d829ef1f 100644 --- a/crates/swc_ecma_transforms_base/src/resolver/tests.rs +++ b/crates/swc_ecma_transforms_base/src/resolver/tests.rs @@ -48,7 +48,7 @@ fn run_test_with_config( config: impl FnOnce() -> crate::hygiene::Config, ) where F: FnOnce() -> V, - V: Fold, + V: Pass, { crate::tests::test_transform(syntax, |_| tr(), src, to, true, config); } diff --git a/crates/swc_ecma_transforms_base/src/tests.rs b/crates/swc_ecma_transforms_base/src/tests.rs index 4cff1aefc944..c998cece3132 100644 --- a/crates/swc_ecma_transforms_base/src/tests.rs +++ b/crates/swc_ecma_transforms_base/src/tests.rs @@ -8,7 +8,7 @@ use swc_ecma_ast::*; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{error::Error, lexer::Lexer, Parser, StringInput, Syntax}; use swc_ecma_utils::DropSpan; -use swc_ecma_visit::{as_folder, Fold, FoldWith}; +use swc_ecma_visit::{Fold, FoldWith}; use crate::{fixer::fixer, helpers::HELPERS, hygiene::hygiene_with_config}; @@ -84,13 +84,13 @@ impl Tester<'_> { Ok(stmts.pop().unwrap()) } - pub fn apply_transform( + pub fn apply_transform( &mut self, - mut tr: T, + tr: T, name: &str, syntax: Syntax, src: &str, - ) -> Result { + ) -> Result { let fm = self .cm .new_source_file(FileName::Real(name.into()).into(), src.into()); @@ -108,14 +108,12 @@ impl Tester<'_> { res? }; - let module = module - .fold_with(&mut tr) - .fold_with(&mut as_folder(DropSpan {})); + let module = Program::Module(module).apply(tr).apply(DropSpan); Ok(module) } - pub fn print(&mut self, module: &Module) -> String { + pub fn print(&mut self, program: &Program) -> String { let mut buf = Vec::new(); { let mut emitter = Emitter { @@ -131,7 +129,7 @@ impl Tester<'_> { }; // println!("Emitting: {:?}", module); - emitter.emit_module(module).unwrap(); + emitter.emit_program(program).unwrap(); } let s = String::from_utf8_lossy(&buf); @@ -158,15 +156,10 @@ pub(crate) fn test_transform( hygiene_config: impl FnOnce() -> crate::hygiene::Config, ) where F: FnOnce(&mut Tester) -> P, - P: Fold, + P: Pass, { crate::tests::Tester::run(|tester| { - let expected = tester.apply_transform( - as_folder(::swc_ecma_utils::DropSpan), - "output.js", - syntax, - expected, - )?; + let expected = tester.apply_transform(DropSpan, "output.js", syntax, expected)?; println!("----- Actual -----"); @@ -182,9 +175,9 @@ pub(crate) fn test_transform( } let actual = actual - .fold_with(&mut hygiene_with_config(hygiene_config())) - .fold_with(&mut fixer(None)) - .fold_with(&mut as_folder(DropSpan)); + .apply(hygiene_with_config(hygiene_config())) + .apply(fixer(None)) + .apply(DropSpan); if actual == expected { return Ok(()); diff --git a/crates/swc_ecma_transforms_base/tests/fixer_test262.rs b/crates/swc_ecma_transforms_base/tests/fixer_test262.rs index 89c1f6b53a21..bd6feababb68 100644 --- a/crates/swc_ecma_transforms_base/tests/fixer_test262.rs +++ b/crates/swc_ecma_transforms_base/tests/fixer_test262.rs @@ -208,13 +208,15 @@ fn identity_tests(tests: &mut Vec) -> Result<(), io::Error> { let module = parser .parse_module() .map(normalize) - .map(|p| p.fold_with(&mut fixer(None))) + .map(Program::Module) + .map(|p| p.apply(fixer(None))) .map_err(|e| { e.into_diagnostic(handler).emit(); })?; let module2 = e_parser .parse_module() .map(normalize) + .map(Program::Module) .map_err(|e| { e.into_diagnostic(handler).emit(); }) @@ -222,20 +224,22 @@ fn identity_tests(tests: &mut Vec) -> Result<(), io::Error> { if module == module2 { return Ok(()); } - emitter.emit_module(&module).unwrap(); - expected_emitter.emit_module(&module2).unwrap(); + emitter.emit_program(&module).unwrap(); + expected_emitter.emit_program(&module2).unwrap(); } else { let script = parser .parse_script() .map(normalize) - .map(|p| p.fold_with(&mut fixer(None))) + .map(Program::Script) + .map(|p| p.apply(fixer(None))) .map_err(|e| { e.into_diagnostic(handler).emit(); })?; let script2 = e_parser .parse_script() .map(normalize) - .map(|p| p.fold_with(&mut fixer(None))) + .map(Program::Script) + .map(|p| p.apply(fixer(None))) .map_err(|e| { e.into_diagnostic(handler).emit(); })?; @@ -243,8 +247,8 @@ fn identity_tests(tests: &mut Vec) -> Result<(), io::Error> { if script == script2 { return Ok(()); } - emitter.emit_script(&script).unwrap(); - expected_emitter.emit_script(&script2).unwrap(); + emitter.emit_program(&script).unwrap(); + expected_emitter.emit_program(&script2).unwrap(); } } diff --git a/crates/swc_ecma_transforms_base/tests/fixture.rs b/crates/swc_ecma_transforms_base/tests/fixture.rs index 581258c01217..a1272c88c98b 100644 --- a/crates/swc_ecma_transforms_base/tests/fixture.rs +++ b/crates/swc_ecma_transforms_base/tests/fixture.rs @@ -1,13 +1,11 @@ use std::path::{Path, PathBuf}; -use swc_common::{chain, sync::Lrc, Mark, SourceMap, SyntaxContext}; +use swc_common::{sync::Lrc, Mark, SourceMap, SyntaxContext}; use swc_ecma_ast::*; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{lexer::Lexer, EsSyntax, Parser, StringInput, Syntax, TsSyntax}; use swc_ecma_transforms_base::{fixer::fixer, resolver}; -use swc_ecma_visit::{ - as_folder, visit_mut_obj_and_computed, Fold, FoldWith, VisitMut, VisitMutWith, -}; +use swc_ecma_visit::{visit_mut_obj_and_computed, visit_mut_pass, VisitMut, VisitMutWith}; use testing::{fixture, run_test2, NormalizedOutput}; pub fn print(cm: Lrc, program: &Program) -> String { @@ -33,7 +31,7 @@ pub fn print(cm: Lrc, program: &Program) -> String { fn run(syntax: Syntax, input: &Path, op: F) where F: FnOnce() -> P, - P: Fold, + P: Pass, { let dir = input.parent().unwrap(); let output = dir.join(format!( @@ -53,7 +51,7 @@ where let mut folder = op(); - let program = program.fold_with(&mut folder); + let program = program.apply(&mut folder); let actual = print(cm, &program); let actual = NormalizedOutput::from(actual); @@ -76,10 +74,10 @@ fn test_resolver(input: PathBuf) { || { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), - as_folder(TsHygiene { unresolved_mark }), - fixer(None) + visit_mut_pass(TsHygiene { unresolved_mark }), + fixer(None), ) }, ); @@ -96,10 +94,10 @@ fn test_ts_resolver(input: PathBuf) { || { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( resolver(unresolved_mark, Mark::new(), true), - as_folder(TsHygiene { unresolved_mark }), - fixer(None) + visit_mut_pass(TsHygiene { unresolved_mark }), + fixer(None), ) }, ); diff --git a/crates/swc_ecma_transforms_base/tests/ts_resolver.rs b/crates/swc_ecma_transforms_base/tests/ts_resolver.rs index e6a033a6452a..ba3c9b7573c4 100644 --- a/crates/swc_ecma_transforms_base/tests/ts_resolver.rs +++ b/crates/swc_ecma_transforms_base/tests/ts_resolver.rs @@ -6,7 +6,7 @@ use swc_common::{Mark, SyntaxContext}; use swc_ecma_ast::*; use swc_ecma_parser::{parse_file_as_module, Syntax, TsSyntax}; use swc_ecma_transforms_base::resolver; -use swc_ecma_visit::{FoldWith, Visit, VisitWith}; +use swc_ecma_visit::{Visit, VisitWith}; use testing::fixture; #[fixture("../swc_ecma_parser/tests/**/*.ts")] @@ -34,7 +34,7 @@ fn no_empty(input: PathBuf) { Err(..) => return Ok(()), }; - let module = module.fold_with(&mut resolver(Mark::new(), Mark::new(), true)); + let module = Program::Module(module).apply(resolver(Mark::new(), Mark::new(), true)); module.visit_with(&mut AssertNoEmptyCtxt); diff --git a/crates/swc_ecma_transforms_classes/src/super_field.rs b/crates/swc_ecma_transforms_classes/src/super_field.rs index 3bc4b9a26c12..5023f9a74e7f 100644 --- a/crates/swc_ecma_transforms_classes/src/super_field.rs +++ b/crates/swc_ecma_transforms_classes/src/super_field.rs @@ -64,7 +64,7 @@ macro_rules! mark_nested { } impl VisitMut for SuperFieldAccessFolder<'_> { - noop_visit_mut_type!(fail); + noop_visit_mut_type!(); // mark_nested!(fold_function, Function); mark_nested!(visit_mut_class, Class); diff --git a/crates/swc_ecma_transforms_compat/src/class_fields_use_set.rs b/crates/swc_ecma_transforms_compat/src/class_fields_use_set.rs index d8f84f74d70b..6e929f2a33d1 100644 --- a/crates/swc_ecma_transforms_compat/src/class_fields_use_set.rs +++ b/crates/swc_ecma_transforms_compat/src/class_fields_use_set.rs @@ -6,7 +6,7 @@ use swc_ecma_utils::{ constructor::inject_after_super, default_constructor, is_literal, is_simple_pure_expr, private_ident, prop_name_to_member_prop, ExprFactory, ModuleItemLike, StmtLike, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// # What does this module do? /// @@ -83,8 +83,8 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWit /// } /// } /// ``` -pub fn class_fields_use_set(pure_getters: bool) -> impl Fold + VisitMut { - as_folder(ClassFieldsUseSet { pure_getters }) +pub fn class_fields_use_set(pure_getters: bool) -> impl Pass { + visit_mut_pass(ClassFieldsUseSet { pure_getters }) } #[derive(Debug)] diff --git a/crates/swc_ecma_transforms_compat/src/reserved_words.rs b/crates/swc_ecma_transforms_compat/src/reserved_words.rs index 41454a85af0b..40188c4e192e 100644 --- a/crates/swc_ecma_transforms_compat/src/reserved_words.rs +++ b/crates/swc_ecma_transforms_compat/src/reserved_words.rs @@ -2,11 +2,11 @@ use swc_atoms::JsWord; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, visit_mut_obj_and_computed, Fold, VisitMut, VisitMutWith, + noop_visit_mut_type, visit_mut_obj_and_computed, visit_mut_pass, VisitMut, VisitMutWith, }; -pub fn reserved_words() -> impl 'static + Fold + VisitMut { - as_folder(EsReservedWord) +pub fn reserved_words() -> impl 'static + Pass + VisitMut { + visit_mut_pass(EsReservedWord) } #[derive(Clone, Copy)] diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs b/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs index 9f8738033a89..1815098f6983 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs @@ -1,15 +1,15 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::arrow; use swc_ecma_transforms_testing::{compare_stdout, test_fixture}; -use swc_ecma_visit::Fold; -fn tr() -> impl Fold { +fn tr() -> impl Pass { let unresolved = Mark::new(); let global = Mark::new(); - chain!(resolver(unresolved, global, false), arrow(unresolved)) + (resolver(unresolved, global, false), arrow(unresolved)) } compare_stdout!( @@ -60,9 +60,9 @@ fn fixture(input: PathBuf) { Default::default(), &|_| { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), - arrow(unresolved_mark) + arrow(unresolved_mark), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs b/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs index 26c553f591d1..4895e2babc71 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs @@ -1,7 +1,7 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, Mark, SyntaxContext}; -use swc_ecma_ast::{Ident, PropName, TsQualifiedName}; +use swc_common::{Mark, SyntaxContext}; +use swc_ecma_ast::{Ident, Pass, PropName, TsQualifiedName}; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -10,13 +10,13 @@ use swc_ecma_transforms_compat::{ es2017::async_to_generator, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture, Tester}; -use swc_ecma_visit::{as_folder, visit_mut_obj_and_computed, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{visit_mut_obj_and_computed, visit_mut_pass, VisitMut, VisitMutWith}; -fn tr() -> impl Fold { +fn tr() -> impl Pass { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) } @@ -87,7 +87,7 @@ expect(functions[1]()).toBe(3); test_exec!( ::swc_ecma_parser::Syntax::default(), - |_| chain!(for_of(Default::default()), block_scoping(Mark::new())), + |_| (for_of(Default::default()), block_scoping(Mark::new())), issue_609_1, "let functions = []; for (let i of [1, 3, 5, 7, 9]) { @@ -221,9 +221,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - es2015::es2015(unresolved_mark, Some(comments.clone()), Default::default()) + es2015::es2015(unresolved_mark, Some(comments.clone()), Default::default()), ) }, issue_1022_1, @@ -240,9 +240,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - es2015::es2015(unresolved_mark, Some(comments.clone()), Default::default()) + es2015::es2015(unresolved_mark, Some(comments.clone()), Default::default()), ) }, issue_1022_2, @@ -260,9 +260,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - es2015::es2015(unresolved_mark, Some(comments.clone()), Default::default()) + es2015::es2015(unresolved_mark, Some(comments.clone()), Default::default()), ) }, issue_1022_3, @@ -310,9 +310,9 @@ test!( Syntax::default(), |Tester { comments, .. }| { let mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), mark), - es2015::es2015(mark, Some(comments.clone()), Default::default(),) + es2015::es2015(mark, Some(comments.clone()), Default::default()), ) }, issue_1036_2, @@ -329,9 +329,9 @@ test_exec!( Syntax::default(), |Tester { comments, .. }| { let mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), mark), - es2015::es2015(mark, Some(comments.clone()), Default::default(),) + es2015::es2015(mark, Some(comments.clone()), Default::default()), ) }, issue_1036_3, @@ -420,9 +420,9 @@ test!( ::swc_ecma_parser::Syntax::default(), |Tester { comments, .. }| { let mark = Mark::new(); - chain!( + ( resolver(mark, Mark::new(), false), - es2015::es2015(mark, Some(comments.clone()), Default::default(),) + es2015::es2015(mark, Some(comments.clone()), Default::default()), ) }, arguments_loop, @@ -457,9 +457,9 @@ compare_stdout!( ::swc_ecma_parser::Syntax::default(), |Tester { comments, .. }| { let mark = Mark::fresh(Mark::root()); - chain!( + ( resolver(mark, Mark::new(), false), - es2015::es2015(mark, Some(comments.clone()), Default::default(),) + es2015::es2015(mark, Some(comments.clone()), Default::default()), ) }, arguments_arrow, @@ -638,9 +638,9 @@ fn exec(input: PathBuf) { Default::default(), |_| { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) }, &input, @@ -650,13 +650,13 @@ fn exec(input: PathBuf) { Default::default(), |t| { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() - ) + Default::default(), + ), ) }, &input, @@ -671,10 +671,10 @@ fn fixture(input: PathBuf) { Default::default(), &|_| { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), block_scoping(unresolved_mark), - as_folder(TsHygiene { unresolved_mark }) + visit_mut_pass(TsHygiene { unresolved_mark }), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs index 64df8c18bc5e..c21e9db1756d 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs @@ -1,6 +1,7 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -10,19 +11,18 @@ use swc_ecma_transforms_compat::{ es2022::class_properties, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture, Tester}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Syntax::default() } -fn tr(_: &Tester) -> impl Fold { +fn tr(_: &Tester) -> impl Pass { classes(Default::default()) } -fn spec_tr(_: &Tester) -> impl Fold { +fn spec_tr(_: &Tester) -> impl Pass { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), classes(Default::default()), spread(Default::default()), @@ -318,7 +318,7 @@ expect(constructor).toBe(CustomElement); // regression_5817 test!( syntax(), - |t| chain!(tr(t), arrow(Mark::new())), + |t| (tr(t), arrow(Mark::new())), regression_5817, r#" class A extends B { @@ -2602,7 +2602,7 @@ test!( test!( syntax(), - |_| chain!( + |_| ( resolver(Mark::new(), Mark::new(), false), classes(Default::default()) ), @@ -2725,7 +2725,7 @@ class List extends Array {} // extend_builtins_imported_babel_plugin_transform_builtin_classes test_exec!( syntax(), - |_| chain!(classes(Default::default()), block_scoping(Mark::new())), + |_| (classes(Default::default()), block_scoping(Mark::new())), extend_builtins_imported_babel_plugin_transform_builtin_classes_exec, r#" // Imported from @@ -3026,7 +3026,7 @@ expect(obj.test).toBe(3); // extend_builtins_spec test_exec!( syntax(), - |_| chain!(classes(Default::default()), block_scoping(Mark::new())), + |_| (classes(Default::default()), block_scoping(Mark::new())), extend_builtins_spec_exec, r#" class List extends Array {} @@ -3158,7 +3158,7 @@ test!( // TODO: Unignore this ignore, syntax(), - |t| chain!(tr(t), block_scoping(Mark::new())), + |t| (tr(t), block_scoping(Mark::new())), regression_t7010, r#" class Foo { @@ -3414,7 +3414,7 @@ expect(obj.test).toBe(3); // extend_builtins_builtin_objects_throw_when_wrapped test_exec!( syntax(), - |_| chain!(classes(Default::default()), block_scoping(Mark::new())), + |_| (classes(Default::default()), block_scoping(Mark::new())), extend_builtins_builtin_objects_throw_when_wrapped_exec, r#" // JSON is wrapped because it starts with an uppercase letter, but it @@ -3463,7 +3463,7 @@ test_exec!( // Just don't do this. ignore, syntax(), - |_| chain!(classes(Default::default()), block_scoping(Mark::new())), + |_| (classes(Default::default()), block_scoping(Mark::new())), extend_builtins_overwritten_null_exec, r#" var env = { @@ -3484,7 +3484,7 @@ test_exec!( // Just don't do this. With is evil. ignore, syntax(), - |_| chain!(classes(Default::default()), block_scoping(Mark::new())), + |_| (classes(Default::default()), block_scoping(Mark::new())), extend_builtins_super_called_exec, r#" var called = false; @@ -3675,7 +3675,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), es2022::es2022(Default::default(), unresolved_mark), es2018::es2018(Default::default()), @@ -3684,7 +3684,7 @@ test!( es2015::es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -3700,13 +3700,13 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), es2015::es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -3744,13 +3744,13 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), es2015::es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -3770,13 +3770,13 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), es2015::es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -3799,10 +3799,10 @@ fn exec(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, &src, @@ -3819,10 +3819,10 @@ fn fixture(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, &input, @@ -4078,7 +4078,7 @@ class Thing extends B { test!( syntax(), - |_| chain!(classes(Default::default()), block_scoping(Mark::new())), + |_| (classes(Default::default()), block_scoping(Mark::new())), issue_5102, r#" let C = class {} diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_computed_props.rs b/crates/swc_ecma_transforms_compat/tests/es2015_computed_props.rs index 3b0bd78803f1..4ba28624ea95 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_computed_props.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_computed_props.rs @@ -1,15 +1,15 @@ #![allow(clippy::unit_arg)] +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_compat::es2015::computed_props::{computed_properties, Config}; use swc_ecma_transforms_testing::{test, test_exec}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { ::swc_ecma_parser::Syntax::default() } -fn tr(_: ()) -> impl Fold { +fn tr(_: ()) -> impl Pass { computed_properties(Default::default()) } diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs index 020f4561cd3f..028b313215d8 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -13,16 +14,15 @@ use swc_ecma_transforms_compat::{ es2018::object_rest_spread, }; use swc_ecma_transforms_testing::{test, test_exec, test_fixture}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() } -fn tr() -> impl Fold { - chain!( +fn tr() -> impl Pass { + ( resolver(Mark::new(), Mark::new(), false), - destructuring(Config { loose: true }) + destructuring(Config { loose: true }), ) } @@ -91,7 +91,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -590,12 +590,12 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -619,7 +619,7 @@ test!( // destructuring_function_key_with_object_rest_spread test_exec!( syntax(), - |_| chain!( + |_| ( object_rest_spread(Default::default()), destructuring(Default::default()) ), @@ -650,7 +650,7 @@ function isBetween(x, a, b) { // destructuring_for_of test!( syntax(), - |_| chain!( + |_| ( spread(Default::default()), destructuring(Default::default()), block_scoping(Mark::new()), @@ -676,7 +676,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -700,7 +700,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -723,7 +723,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -754,7 +754,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -779,7 +779,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -807,7 +807,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -863,7 +863,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -894,7 +894,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -983,7 +983,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1020,7 +1020,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1061,7 +1061,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1085,7 +1085,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -1130,7 +1130,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -1154,7 +1154,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -1179,7 +1179,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -1206,7 +1206,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -1226,7 +1226,7 @@ var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect; // destructuring_assignment_statement test!( syntax(), - |_| chain!( + |_| ( destructuring(Default::default()), spread(Default::default()), block_scoping(Mark::new()), @@ -1241,7 +1241,7 @@ test!( // destructuring_assignment_statement_loose test!( syntax(), - |_| chain!( + |_| ( destructuring(Config { loose: true }), spread(Default::default()), block_scoping(Mark::new()), @@ -1260,7 +1260,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1284,7 +1284,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1307,7 +1307,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), spread(Default::default()), @@ -1336,7 +1336,7 @@ const { // destructuring_number_key_with_object_rest_spread test_exec!( syntax(), - |_| chain!( + |_| ( object_rest_spread(Default::default()), destructuring(Default::default()) ), @@ -1359,7 +1359,7 @@ expect(rest).toEqual({ 2: "b", 3: "c" }); // destructuring_for_in test!( syntax(), - |_| chain!( + |_| ( spread(Default::default()), destructuring(Default::default()), block_scoping(Mark::new()), @@ -1381,7 +1381,7 @@ for ([name, value] in obj) { // destructuring_for_in_loose test!( syntax(), - |_| chain!( + |_| ( spread(Default::default()), destructuring(Config { loose: true }), block_scoping(Mark::new()), @@ -1407,7 +1407,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1430,7 +1430,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), spread(Default::default()), parameters(Default::default(), unresolved_mark), @@ -1663,10 +1663,10 @@ fn fixture(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), - destructuring(Default::default()) + destructuring(Default::default()), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs b/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs index 49f65d687104..72f2faca0539 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs @@ -1,6 +1,6 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, comments::NoopComments, Mark}; +use swc_common::{comments::NoopComments, Mark}; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::{ @@ -278,12 +278,12 @@ fn exec(input: PathBuf) { |_| { let top_level_mark = Mark::new(); - chain!( + ( resolver(Mark::new(), top_level_mark, false), for_of(Config { assume_array: false, ..Default::default() - }) + }), ) }, &input, @@ -299,12 +299,12 @@ fn fixture(input: PathBuf) { &|_| { let top_level_mark = Mark::new(); - chain!( + ( resolver(Mark::new(), top_level_mark, false), for_of(Config { assume_array: false, ..Default::default() - }) + }), ) }, &input, @@ -323,9 +323,9 @@ fn exec_es2015(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - es2015::es2015(unresolved_mark, Some(NoopComments), Default::default()) + es2015::es2015(unresolved_mark, Some(NoopComments), Default::default()), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_generator.rs b/crates/swc_ecma_transforms_compat/tests/es2015_generator.rs index 69fac8cf95a4..bf8cfd507e20 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_generator.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_generator.rs @@ -1,10 +1,10 @@ #![allow(clippy::unit_arg)] use swc_common::{ - chain, comments::{NoopComments, SingleThreadedComments}, Mark, }; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -12,28 +12,27 @@ use swc_ecma_transforms_compat::{ es2021, es2022, }; use swc_ecma_transforms_testing::{test, test_exec}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Syntax::default() } -fn tr(_: ()) -> impl Fold { +fn tr(_: ()) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - generator(unresolved_mark, NoopComments) + generator(unresolved_mark, NoopComments), ) } -fn tr_with_async() -> impl Fold { +fn tr_with_async() -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), async_to_generator(Default::default(), unresolved_mark), - generator(unresolved_mark, NoopComments) + generator(unresolved_mark, NoopComments), ) } @@ -910,13 +909,13 @@ test_exec!( syntax(), |t| { let unresolved_mark = Mark::new(); - chain!( + ( es2017(Default::default(), unresolved_mark), es2016(), es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -932,7 +931,7 @@ test_exec!( syntax(), |t| { let unresolved_mark = Mark::new(); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), es2015::for_of(Default::default()), generator(unresolved_mark, t.comments.clone()), @@ -1368,9 +1367,9 @@ test!( Syntax::default(), |_| { let mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), mark), - es2015::(mark, None, Default::default()) + es2015::(mark, None, Default::default()), ) }, issue_1799_2, @@ -1385,7 +1384,7 @@ test!( Syntax::default(), |_| { let mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), mark), es2016(), es2015::(mark, None, Default::default()), @@ -1405,7 +1404,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), es2022(Default::default(), unresolved_mark), es2021(), @@ -1443,10 +1442,10 @@ test_exec!( Syntax::default(), |t| { let mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), mark), es2015::for_of(Default::default()), - generator(mark, t.comments.clone()) + generator(mark, t.comments.clone()), ) }, issue_1918_1, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs b/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs index add83ff25d10..c2d2ae4f235a 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs @@ -1,20 +1,20 @@ use std::{fs::read_to_string, path::PathBuf}; use serde::Deserialize; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ es2015::{arrow, classes, new_target::new_target}, es2022::class_properties, }; use swc_ecma_transforms_testing::{exec_tr, parse_options, test, test_fixture, Tester}; -use swc_ecma_visit::Fold; -fn get_passes(_: &Tester, plugins: &[PluginConfig]) -> Box { +fn get_passes(_: &Tester, plugins: &[PluginConfig]) -> Box { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - let mut pass: Box = Box::new(resolver(unresolved_mark, top_level_mark, true)); + let mut pass: Box = Box::new(resolver(unresolved_mark, top_level_mark, true)); for plugin in plugins { let (name, option) = match plugin { @@ -38,7 +38,7 @@ fn get_passes(_: &Tester, plugins: &[PluginConfig]) -> Box { "transform-new-target" => {} "proposal-class-properties" => { - pass = Box::new(chain!( + pass = Box::new(( pass, class_properties( class_properties::Config { @@ -50,12 +50,12 @@ fn get_passes(_: &Tester, plugins: &[PluginConfig]) -> Box { pure_getter: loose, }, unresolved_mark, - ) + ), )); } "transform-arrow-functions" => { - pass = Box::new(chain!(pass, arrow(Mark::new()))); + pass = Box::new((pass, arrow(Mark::new()))); } _ => { @@ -64,7 +64,7 @@ fn get_passes(_: &Tester, plugins: &[PluginConfig]) -> Box { } } - pass = Box::new(chain!(pass, new_target())); + pass = Box::new((pass, new_target())); pass } @@ -110,7 +110,7 @@ fn fixture(input: PathBuf) { test!( ::swc_ecma_parser::Syntax::default(), - |_| chain!(classes(Default::default()), new_target()), + |_| (classes(Default::default()), new_target()), issue_6259, r#" (() => { diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_object_super.rs b/crates/swc_ecma_transforms_compat/tests/es2015_object_super.rs index ae6e78545a65..f2b731a0681c 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_object_super.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_object_super.rs @@ -1,19 +1,19 @@ -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::{function_name, object_super, shorthand}; use swc_ecma_transforms_testing::test; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { ::swc_ecma_parser::Syntax::default() } -fn tr() -> impl Fold { +fn tr() -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_super(), shorthand(), diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs b/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs index aec8c84f6d01..ef1040978b6b 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -10,17 +11,16 @@ use swc_ecma_transforms_compat::{ es2017::async_to_generator, }; use swc_ecma_transforms_testing::{test, test_exec, test_fixture}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() } -fn tr(c: Config) -> impl Fold { +fn tr(c: Config) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(c, unresolved_mark), destructuring(destructuring::Config { loose: false }), @@ -138,7 +138,7 @@ foo(1, 2, 3);"# test!( syntax(), - |_| chain!(classes(Default::default()), tr(Default::default())), + |_| (classes(Default::default()), tr(Default::default())), default_iife_4253, r#"class Ref { constructor(id = ++Ref.nextID) { @@ -167,7 +167,7 @@ expect(new Ref().id).toBe(2);"# test!( syntax(), - |_| chain!(classes(Default::default()), tr(Default::default())), + |_| (classes(Default::default()), tr(Default::default())), default_iife_self, r#"class Ref { constructor(ref = Ref) { @@ -476,7 +476,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( arrow(unresolved_mark), resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), @@ -722,7 +722,7 @@ function d(thing, ...args) { test!( syntax(), - |_| chain!( + |_| ( tr(Default::default()), classes(Default::default()), spread(Default::default()) @@ -791,7 +791,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), async_to_generator(Default::default(), unresolved_mark), arrow(unresolved_mark), @@ -815,7 +815,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), async_to_generator(Default::default(), unresolved_mark), arrow(unresolved_mark), @@ -838,7 +838,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -860,10 +860,10 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) }, parameters_regression_4333, @@ -881,10 +881,10 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), - destructuring(Default::default()) + destructuring(Default::default()), ) }, issue_760, @@ -898,7 +898,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -916,7 +916,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -934,7 +934,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -952,7 +952,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -970,7 +970,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -1018,7 +1018,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -1038,7 +1038,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), ) @@ -1286,9 +1286,9 @@ fn fixture(input: PathBuf) { Default::default(), &|_| { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, Mark::new(), false), - parameters(Default::default(), unresolved_mark) + parameters(Default::default(), unresolved_mark), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs b/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs index d2a49cc38e56..1af4fb4056a5 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::shorthand; use swc_ecma_transforms_testing::test_fixture; @@ -13,7 +13,7 @@ fn fixture(input: PathBuf) { Default::default(), &|_| { let unresolved_mark = Mark::new(); - chain!(resolver(unresolved_mark, Mark::new(), false), shorthand()) + (resolver(unresolved_mark, Mark::new(), false), shorthand()) }, &input, &output, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs b/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs index da6a65aea201..af98530f1888 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs @@ -1,21 +1,21 @@ -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::{block_scoping, parameters, spread}; use swc_ecma_transforms_testing::{test, test_exec}; -use swc_ecma_visit::Fold; fn syntax() -> ::swc_ecma_parser::Syntax { Default::default() } -fn tr() -> impl Fold { +fn tr() -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), - spread(Default::default()) + spread(Default::default()), ) } @@ -467,11 +467,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), spread(Default::default()), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) }, spread_known_rest, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_template_literals.rs b/crates/swc_ecma_transforms_compat/tests/es2015_template_literals.rs index 867c67d904a5..d459f0da7b90 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_template_literals.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_template_literals.rs @@ -1,13 +1,13 @@ +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_compat::es2015::template_literal; use swc_ecma_transforms_testing::{test, test_exec}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() } -fn tr(config: template_literal::Config) -> impl Fold { +fn tr(config: template_literal::Config) -> impl Pass { template_literal(config) } diff --git a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs index 6d3035dd6d7c..3b25257b3d43 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs @@ -1,6 +1,6 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, Mark, Spanned}; +use swc_common::{Mark, Spanned}; use swc_ecma_ast::*; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::{fixer::fixer, resolver}; @@ -13,7 +13,7 @@ use swc_ecma_transforms_compat::{ es2022::class_properties, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec}; -use swc_ecma_visit::{Fold, FoldWith}; +use swc_ecma_visit::{fold_pass, Fold, FoldWith}; struct ParenRemover; impl Fold for ParenRemover { @@ -38,28 +38,28 @@ fn syntax() -> Syntax { Syntax::default() } -fn tr() -> impl Fold { +fn tr() -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - ParenRemover, + fold_pass(ParenRemover), arrow(unresolved_mark), parameters(Default::default(), unresolved_mark), destructuring(destructuring::Config { loose: false }), function_name(), async_to_generator(Default::default(), unresolved_mark), - fixer(None) + fixer(None), ) } -fn with_resolver() -> impl Fold { +fn with_resolver() -> impl Pass { let unresolved = Mark::new(); let top_level = Mark::new(); - chain!( + ( resolver(unresolved, top_level, false), - async_to_generator(Default::default(), unresolved) + async_to_generator(Default::default(), unresolved), ) } @@ -412,7 +412,7 @@ test!( test_exec!( syntax(), - |t| chain!( + |t| ( tr(), es2015( Mark::fresh(Mark::root()), @@ -480,13 +480,13 @@ test_exec!( syntax(), |t| { let unresolved_mark = Mark::new(); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() - ) + Default::default(), + ), ) }, issue_400_3, @@ -506,7 +506,7 @@ return (new A()).print();" //// regression_7178 //test!( // syntax(), -// |_| chain!(jsx(), jsc_constant_elements(), +// |_| (jsx(), jsc_constant_elements(), // async_to_generator(Default::default()),), regression_7178, // r#" //const title = "Neem contact op"; @@ -586,9 +586,9 @@ test!( syntax(), |_| { let unresolved_mark = Mark::new(); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), - arrow(unresolved_mark) + arrow(unresolved_mark), ) }, regression_t7194, @@ -693,7 +693,7 @@ test!( syntax(), |_| { let unresolved_mark = Mark::new(); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), //regenerator(), arrow(unresolved_mark), @@ -793,7 +793,7 @@ var foo = async function bar() { //// async_to_generator_async_iife_with_regenerator_spec //test!( // syntax(), -// |_| chain!(async_to_generator(Default::default()), arrow(), +// |_| (async_to_generator(Default::default()), arrow(), // regenerator(),), async_to_generator_async_iife_with_regenerator_spec, // r#" //(async function() { await 'ok' })(); @@ -924,7 +924,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), async_to_generator(Default::default(), unresolved_mark), parameters(Default::default(), unresolved_mark), @@ -1362,10 +1362,10 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), Mark::new()) + async_to_generator(Default::default(), Mark::new()), ) }, issue_1341_1_exec, @@ -1406,10 +1406,10 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), Mark::new()) + async_to_generator(Default::default(), Mark::new()), ) }, issue_1341_2_exec, @@ -1464,9 +1464,9 @@ test_exec!( Syntax::default(), |t| { let mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), mark), - generator(mark, t.comments.clone()) + generator(mark, t.comments.clone()), ) }, issue_1575_2, @@ -1539,9 +1539,9 @@ test!( Syntax::default(), |t| { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), - generator(unresolved_mark, t.comments.clone()) + generator(unresolved_mark, t.comments.clone()), ) }, issue_1684_2, @@ -1652,9 +1652,9 @@ test!( Syntax::default(), |t| { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), - generator(unresolved_mark, t.comments.clone()) + generator(unresolved_mark, t.comments.clone()), ) }, issue_2402_2, @@ -1713,9 +1713,9 @@ test!( Syntax::default(), |t| { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), - generator(unresolved_mark, t.comments.clone()) + generator(unresolved_mark, t.comments.clone()), ) }, issue_2305_2, @@ -1750,9 +1750,9 @@ test!( Syntax::default(), |t| { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), - generator(unresolved_mark, t.comments.clone()) + generator(unresolved_mark, t.comments.clone()), ) }, issue_2677_1, @@ -1774,9 +1774,9 @@ test!( Syntax::default(), |t| { let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( async_to_generator(Default::default(), unresolved_mark), - generator(unresolved_mark, t.comments.clone()) + generator(unresolved_mark, t.comments.clone()), ) }, issue_2677_2, @@ -2112,10 +2112,10 @@ fn exec(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), unresolved_mark) + async_to_generator(Default::default(), unresolved_mark), ) }, &input, @@ -2131,13 +2131,13 @@ fn exec_regenerator(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), async_to_generator(Default::default(), unresolved_mark), es2015::for_of(Default::default()), block_scoping(unresolved_mark), - generator(unresolved_mark, t.comments.clone()) + generator(unresolved_mark, t.comments.clone()), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2018_object_rest_spread.rs b/crates/swc_ecma_transforms_compat/tests/es2018_object_rest_spread.rs index b5560095d2d5..f61dfd103033 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2018_object_rest_spread.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2018_object_rest_spread.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -8,13 +9,12 @@ use swc_ecma_transforms_compat::{ es2018::{object_rest_spread, object_rest_spread::Config}, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Syntax::default() } -fn tr(c: Config) -> impl Fold { +fn tr(c: Config) -> impl Pass { object_rest_spread(c) } @@ -165,7 +165,7 @@ export var [ dd, ee ] = ads; test!( syntax(), - |_| chain!(tr(Default::default()), spread(Default::default())), + |_| (tr(Default::default()), spread(Default::default())), rest_for_x, r#" // ForXStatement @@ -2458,13 +2458,13 @@ test_exec!( // let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), tr(Default::default()), es2015::es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), ) }, @@ -2485,7 +2485,7 @@ compare_stdout!( // let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), tr(Default::default()), ) @@ -2510,7 +2510,7 @@ fn fixture(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), object_rest_spread(Default::default()), ) diff --git a/crates/swc_ecma_transforms_compat/tests/es2020_nullish_coalescing.rs b/crates/swc_ecma_transforms_compat/tests/es2020_nullish_coalescing.rs index 8bbd07d6c9b9..a63b13e58f3f 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2020_nullish_coalescing.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2020_nullish_coalescing.rs @@ -1,9 +1,9 @@ +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_compat::es2020::nullish_coalescing::{nullish_coalescing, Config}; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec}; -use swc_ecma_visit::Fold; -fn tr(c: Config) -> impl Fold { +fn tr(c: Config) -> impl Pass { nullish_coalescing(c) } diff --git a/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs b/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs index 12d8f38d8e81..338c9eed32d0 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs @@ -1,6 +1,7 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -8,14 +9,13 @@ use swc_ecma_transforms_compat::{ es2022::class_properties, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture}; -use swc_ecma_visit::Fold; -fn tr(c: Config) -> impl Fold { +fn tr(c: Config) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - optional_chaining(c, unresolved_mark) + optional_chaining(c, unresolved_mark), ) } @@ -257,7 +257,7 @@ fn exec(input: PathBuf) { |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), optional_chaining( Config { @@ -265,7 +265,7 @@ fn exec(input: PathBuf) { ..Default::default() }, Mark::new(), - ) + ), ) }, &src, @@ -281,16 +281,16 @@ fn fixture(input: PathBuf) { &|_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( swc_ecma_transforms_compat::es2022::class_properties::Config { private_as_properties: false, ..Default::default() }, - unresolved_mark + unresolved_mark, ), - optional_chaining(Default::default(), unresolved_mark) + optional_chaining(Default::default(), unresolved_mark), ) }, &input, @@ -308,7 +308,7 @@ fn fixture_loose(input: PathBuf) { &|_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( swc_ecma_transforms_compat::es2022::class_properties::Config { @@ -318,7 +318,7 @@ fn fixture_loose(input: PathBuf) { ..Default::default() }, - unresolved_mark + unresolved_mark, ), optional_chaining( Config { @@ -326,7 +326,7 @@ fn fixture_loose(input: PathBuf) { pure_getter: true, }, Mark::new(), - ) + ), ) }, &input, diff --git a/crates/swc_ecma_transforms_compat/tests/es2021_logcal_assignments.rs b/crates/swc_ecma_transforms_compat/tests/es2021_logcal_assignments.rs index 5ab0ae08e3de..ff8ec09793fa 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2021_logcal_assignments.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2021_logcal_assignments.rs @@ -1,9 +1,9 @@ +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_compat::es2021; use swc_ecma_transforms_testing::{test, test_exec}; -use swc_ecma_visit::Fold; -fn tr() -> impl Fold { +fn tr() -> impl Pass { es2021::logical_assignments() } diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs index a16f09c9cfe7..e82c1efd4a5a 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs @@ -2,7 +2,8 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -14,17 +15,16 @@ use swc_ecma_transforms_compat::{ es3::reserved_words, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture, Tester}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Syntax::Es(Default::default()) } -fn tr(_: &Tester) -> impl Fold { +fn tr(_: &Tester) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), function_name(), class_properties(Default::default(), unresolved_mark), @@ -114,7 +114,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), function_name(), class_properties(Default::default(), unresolved_mark), @@ -1756,9 +1756,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - class_properties(Default::default(), unresolved_mark) + class_properties(Default::default(), unresolved_mark), ) }, issue_308, @@ -1780,10 +1780,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, issue_342, @@ -1804,10 +1804,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) }, issue_443, @@ -1831,10 +1831,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), Mark::new()) + async_to_generator(Default::default(), Mark::new()), ) }, public_regression_t7364, @@ -1867,10 +1867,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_regression_t6719, @@ -1900,10 +1900,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_reevaluated, @@ -1941,10 +1941,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_static, @@ -1975,11 +1975,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_destructuring_object_pattern_1, @@ -2002,10 +2002,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_static_inherited, @@ -2050,9 +2050,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_destructuring_object_pattern_1_exec, @@ -2085,10 +2085,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_static_undefined, @@ -2115,11 +2115,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_destructuring_array_pattern, @@ -2142,10 +2142,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_regression_t2983, @@ -2168,11 +2168,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), async_to_generator(Default::default(), unresolved_mark), - block_scoping(unresolved_mark) + block_scoping(unresolved_mark), ) }, private_regression_t7364, @@ -2205,11 +2205,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_destructuring_array_pattern_1, @@ -2232,9 +2232,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, regression_8882_exec, @@ -2271,9 +2271,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,), + class_properties(Default::default(), unresolved_mark), ) }, regression_8882_exec_2, @@ -2310,9 +2310,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_field_reinitialized, @@ -2340,10 +2340,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_static_export, @@ -2366,10 +2366,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, static_property_tdz_edgest_case, @@ -2387,10 +2387,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, static_property_tdz_false_alarm, @@ -2408,10 +2408,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - arrow(Mark::new()) + arrow(Mark::new()), ) }, regression_6153, @@ -2455,7 +2455,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), arrow(unresolved_mark), @@ -2572,9 +2572,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_optional_chain_call, @@ -2595,9 +2595,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_optional_chain_member, @@ -2618,11 +2618,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_canonical, @@ -2658,9 +2658,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, regression_8882, @@ -2696,11 +2696,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_destructuring_array_pattern_3, @@ -2721,9 +2721,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, staic_private_destructuring_array_pattern, @@ -2744,9 +2744,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, public_static_super_exec, @@ -2777,11 +2777,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_destructuring_array_pattern_2, @@ -2803,10 +2803,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_non_block_arrow_func, @@ -2833,9 +2833,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, regression_8110, @@ -2856,9 +2856,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, public_computed_without_block_exec, @@ -2878,7 +2878,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), exponentiation(), @@ -2902,10 +2902,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - classes(Default::default()) + classes(Default::default()), ) }, static_property_tdz_general, @@ -2924,10 +2924,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, public_native_classes, @@ -2963,10 +2963,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_static_infer_name, @@ -2985,9 +2985,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - class_properties(Default::default(), unresolved_mark) + class_properties(Default::default(), unresolved_mark), ) }, regression_7951, @@ -3008,10 +3008,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, private_native_classes, @@ -3039,11 +3039,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, public_computed_without_block, @@ -3060,9 +3060,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_destructuring_array_pattern_2_exec, @@ -3093,11 +3093,11 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), classes(Default::default()), - block_scoping(Mark::new()) + block_scoping(Mark::new()), ) }, public_static_super, @@ -3122,9 +3122,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_destructuring_array_pattern_exec, @@ -3154,9 +3154,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, private_destructuring_array_pattern_1_exec, @@ -3188,9 +3188,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1306_1, @@ -3215,9 +3215,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1306_2, @@ -3242,9 +3242,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1333_1, @@ -3265,9 +3265,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1333_2, @@ -3368,9 +3368,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1333_3, @@ -3405,9 +3405,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1333_4, @@ -3435,9 +3435,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1333_5, @@ -3457,9 +3457,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1333_6, @@ -3479,9 +3479,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1660_1, @@ -3496,9 +3496,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_3055_1, @@ -3524,9 +3524,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_3618, @@ -3546,10 +3546,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), Mark::new()) + async_to_generator(Default::default(), Mark::new()), ) }, issue_1694_1, @@ -3571,10 +3571,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), Mark::new()) + async_to_generator(Default::default(), Mark::new()), ) }, issue_1694_2, @@ -3596,10 +3596,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), class_properties(Default::default(), unresolved_mark), - async_to_generator(Default::default(), Mark::new()) + async_to_generator(Default::default(), Mark::new()), ) }, issue_1702_1, @@ -3629,9 +3629,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1711_1, @@ -3655,9 +3655,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1742_1, @@ -3687,10 +3687,10 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), - template_literal(Default::default()) + template_literal(Default::default()), ) }, issue_1742_2, @@ -3720,9 +3720,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, new_target_in_class_prop, @@ -3745,9 +3745,9 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, class_field_evalutaion_order, @@ -3770,9 +3770,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1742_3, @@ -3802,9 +3802,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1869_1, @@ -3829,9 +3829,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_1869_2, @@ -3855,9 +3855,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_2021_1, @@ -3878,9 +3878,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_3229_1, @@ -3902,9 +3902,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_3229_2, @@ -3927,9 +3927,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_3368, @@ -3955,9 +3955,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, nested_class_in_arrow, @@ -3979,9 +3979,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_2481, @@ -4001,9 +4001,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_4473, @@ -4027,7 +4027,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4035,7 +4035,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, constant_super_complex_super, @@ -4052,7 +4052,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4060,7 +4060,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, constant_super_field, @@ -4078,7 +4078,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4086,7 +4086,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, private_optional_chain_member_loose, @@ -4106,7 +4106,7 @@ test_exec!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4114,7 +4114,7 @@ test_exec!( ..Default::default() }, unresolved_mark, - ) + ), ) }, set_public_fields_initialization_order, @@ -4166,7 +4166,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4174,7 +4174,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, set_public_fields_computed, @@ -4213,15 +4213,15 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { set_public_fields: true, ..Default::default() }, - unresolved_mark - ) + unresolved_mark, + ), ) }, set_public_constructor_collision, @@ -4246,7 +4246,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4254,7 +4254,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, set_public_static_undefined, @@ -4271,7 +4271,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4279,7 +4279,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, private_as_properties_basic, @@ -4316,7 +4316,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4324,7 +4324,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, private_as_properties_static, @@ -4343,7 +4343,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4351,7 +4351,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, private_as_properties_getter_only, @@ -4377,7 +4377,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4386,7 +4386,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, loose_update, @@ -4447,9 +4447,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, set_only_getter, @@ -4483,9 +4483,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, get_only_setter, @@ -4510,7 +4510,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties( class_properties::Config { @@ -4518,7 +4518,7 @@ test!( ..Default::default() }, unresolved_mark, - ) + ), ) }, loose_keyword_method, @@ -4552,9 +4552,9 @@ fn exec(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, &src, @@ -4569,9 +4569,9 @@ fn fixture(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - class_properties(Default::default(), unresolved_mark) + class_properties(Default::default(), unresolved_mark), ) }, &input, @@ -4586,9 +4586,9 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), - class_properties(Default::default(), unresolved_mark,) + class_properties(Default::default(), unresolved_mark), ) }, issue_6305, @@ -4601,10 +4601,10 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), class_properties(Default::default(), unresolved_mark), - optional_chaining(Default::default(), unresolved_mark) + optional_chaining(Default::default(), unresolved_mark), ) }, issue_8003, diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_private_in_object.rs b/crates/swc_ecma_transforms_compat/tests/es2022_private_in_object.rs index 2d9d4b77a7a3..14e69c5afe86 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2022_private_in_object.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2022_private_in_object.rs @@ -1,14 +1,14 @@ use std::path::PathBuf; use serde::Deserialize; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ es2015::classes, es2022::{class_properties, private_in_object}, }; use swc_ecma_transforms_testing::{parse_options, test_fixture}; -use swc_ecma_visit::Fold; #[derive(Debug, Clone, Deserialize)] #[serde(deny_unknown_fields)] @@ -36,7 +36,7 @@ fn fixture(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - let mut pass: Box = + let mut pass: Box = Box::new(resolver(unresolved_mark, top_level_mark, false)); let mut class_props = false; @@ -55,7 +55,7 @@ fn fixture(input: PathBuf) { "proposal-class-properties" => { if !class_props { class_props = true; - pass = Box::new(chain!( + pass = Box::new(( pass, class_properties( class_properties::Config { @@ -66,7 +66,7 @@ fn fixture(input: PathBuf) { pure_getter: loose, static_blocks_mark: Mark::new(), }, - unresolved_mark + unresolved_mark, ), )); } @@ -75,7 +75,7 @@ fn fixture(input: PathBuf) { "proposal-private-methods" => { if !class_props { class_props = true; - pass = Box::new(chain!( + pass = Box::new(( pass, class_properties( class_properties::Config { @@ -87,13 +87,13 @@ fn fixture(input: PathBuf) { static_blocks_mark: Mark::new(), }, unresolved_mark, - ) + ), )); } } "transform-classes" => { - pass = Box::new(chain!(pass, classes(Default::default()))); + pass = Box::new((pass, classes(Default::default()))); } _ => { @@ -102,7 +102,7 @@ fn fixture(input: PathBuf) { } } - pass = Box::new(chain!(pass, private_in_object())); + pass = Box::new((pass, private_in_object())); pass }, diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_static_blocks.rs b/crates/swc_ecma_transforms_compat/tests/es2022_static_blocks.rs index e208c9e39fab..749a0857a322 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2022_static_blocks.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2022_static_blocks.rs @@ -1,11 +1,11 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2022::{class_properties, static_blocks}; use swc_ecma_transforms_testing::test_fixture; -use swc_ecma_visit::Fold; #[testing::fixture("tests/static-blocks/**/input.js")] fn fixture(input: PathBuf) { @@ -19,16 +19,16 @@ fn fixture(input: PathBuf) { let top_level_mark = Mark::new(); let config = class_properties::Config::default(); - let pass: Box = if input.to_string_lossy().contains("class-properties") { - Box::new(chain!( + let pass: Box = if input.to_string_lossy().contains("class-properties") { + Box::new(( resolver(unresolved_mark, top_level_mark, false), static_blocks(config.static_blocks_mark), - class_properties(config, unresolved_mark) + class_properties(config, unresolved_mark), )) } else { - Box::new(chain!( + Box::new(( resolver(unresolved_mark, top_level_mark, false), - static_blocks(config.static_blocks_mark) + static_blocks(config.static_blocks_mark), )) }; pass diff --git a/crates/swc_ecma_transforms_module/src/amd.rs b/crates/swc_ecma_transforms_module/src/amd.rs index 29849a85fbb8..c7fe26afd9f3 100644 --- a/crates/swc_ecma_transforms_module/src/amd.rs +++ b/crates/swc_ecma_transforms_module/src/amd.rs @@ -13,7 +13,7 @@ use swc_ecma_transforms_base::{feature::FeatureFlag, helper_expr}; use swc_ecma_utils::{ member_expr, private_ident, quote_ident, quote_str, ExprFactory, FunctionFactory, IsDirective, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; pub use super::util::Config as InnerConfig; use crate::{ @@ -44,13 +44,13 @@ pub fn amd( config: Config, available_features: FeatureFlag, comments: Option, -) -> impl Fold + VisitMut +) -> impl Pass where C: Comments, { let Config { module_id, config } = config; - as_folder(Amd { + visit_mut_pass(Amd { module_id, config, unresolved_mark, diff --git a/crates/swc_ecma_transforms_module/src/common_js.rs b/crates/swc_ecma_transforms_module/src/common_js.rs index 280ba2a5b80a..f85b0e7b1a2e 100644 --- a/crates/swc_ecma_transforms_module/src/common_js.rs +++ b/crates/swc_ecma_transforms_module/src/common_js.rs @@ -7,7 +7,7 @@ use swc_ecma_transforms_base::{feature::FeatureFlag, helper_expr}; use swc_ecma_utils::{ member_expr, private_ident, quote_expr, quote_ident, ExprFactory, FunctionFactory, IsDirective, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; pub use super::util::Config; use crate::{ @@ -28,8 +28,8 @@ pub fn common_js( unresolved_mark: Mark, config: Config, available_features: FeatureFlag, -) -> impl Fold + VisitMut { - as_folder(Cjs { +) -> impl Pass { + visit_mut_pass(Cjs { config, resolver, unresolved_mark, diff --git a/crates/swc_ecma_transforms_module/src/import_analysis.rs b/crates/swc_ecma_transforms_module/src/import_analysis.rs index 0a89d6807892..e3fe05397304 100644 --- a/crates/swc_ecma_transforms_module/src/import_analysis.rs +++ b/crates/swc_ecma_transforms_module/src/import_analysis.rs @@ -3,16 +3,13 @@ use swc_common::collections::AHashMap; use swc_ecma_ast::*; use swc_ecma_transforms_base::enable_helper; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitWith, }; use crate::{module_decl_strip::LinkFlag, util::ImportInterop}; -pub fn import_analyzer( - import_interop: ImportInterop, - ignore_dynamic: bool, -) -> impl Fold + VisitMut { - as_folder(ImportAnalyzer { +pub fn import_analyzer(import_interop: ImportInterop, ignore_dynamic: bool) -> impl Pass { + visit_mut_pass(ImportAnalyzer { import_interop, ignore_dynamic, flag_record: Default::default(), diff --git a/crates/swc_ecma_transforms_module/src/rewriter.rs b/crates/swc_ecma_transforms_module/src/rewriter.rs index 1bcf96502d4e..b0d174bbcdc8 100644 --- a/crates/swc_ecma_transforms_module/src/rewriter.rs +++ b/crates/swc_ecma_transforms_module/src/rewriter.rs @@ -3,13 +3,13 @@ use std::sync::Arc; use anyhow::Context; use swc_common::FileName; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{visit_mut_pass, VisitMut, VisitMutWith}; use crate::path::ImportResolver; /// Import rewriter, which rewrites imports as es modules. -pub fn import_rewriter(base: FileName, resolver: Arc) -> impl Fold + VisitMut { - as_folder(Rewriter { base, resolver }) +pub fn import_rewriter(base: FileName, resolver: Arc) -> impl Pass { + visit_mut_pass(Rewriter { base, resolver }) } struct Rewriter { diff --git a/crates/swc_ecma_transforms_module/src/system_js.rs b/crates/swc_ecma_transforms_module/src/system_js.rs index b982c1cf4a5c..526f0a50462b 100644 --- a/crates/swc_ecma_transforms_module/src/system_js.rs +++ b/crates/swc_ecma_transforms_module/src/system_js.rs @@ -6,7 +6,7 @@ use swc_ecma_ast::*; use swc_ecma_utils::{ member_expr, private_ident, quote_ident, quote_str, var::VarCollector, ExprFactory, }; -use swc_ecma_visit::{standard_only_fold, Fold, FoldWith, VisitWith}; +use swc_ecma_visit::{fold_pass, standard_only_fold, Fold, FoldWith, VisitWith}; use crate::{ path::Resolver, @@ -41,8 +41,8 @@ struct SystemJs { context_ident: Ident, } -pub fn system_js(resolver: Resolver, unresolved_mark: Mark, config: Config) -> impl Fold { - SystemJs { +pub fn system_js(resolver: Resolver, unresolved_mark: Mark, config: Config) -> impl Pass { + fold_pass(SystemJs { unresolved_mark, resolver, config, @@ -57,7 +57,7 @@ pub fn system_js(resolver: Resolver, unresolved_mark: Mark, config: Config) -> i import_idents: Vec::new(), export_ident: private_ident!("_export"), context_ident: private_ident!("_context"), - } + }) } struct ModuleItemMeta { diff --git a/crates/swc_ecma_transforms_module/src/umd.rs b/crates/swc_ecma_transforms_module/src/umd.rs index 54645fda5069..8aefd94498cd 100644 --- a/crates/swc_ecma_transforms_module/src/umd.rs +++ b/crates/swc_ecma_transforms_module/src/umd.rs @@ -9,7 +9,7 @@ use swc_ecma_transforms_base::{feature::FeatureFlag, helper_expr}; use swc_ecma_utils::{ is_valid_prop_ident, private_ident, quote_ident, quote_str, ExprFactory, IsDirective, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use self::config::BuiltConfig; pub use self::config::Config; @@ -33,8 +33,8 @@ pub fn umd( unresolved_mark: Mark, config: Config, available_features: FeatureFlag, -) -> impl Fold + VisitMut { - as_folder(Umd { +) -> impl Pass { + visit_mut_pass(Umd { config: config.build(cm.clone()), unresolved_mark, cm, diff --git a/crates/swc_ecma_transforms_module/tests/amd.rs b/crates/swc_ecma_transforms_module/tests/amd.rs index 1fd4b111813f..faf7a4e327d1 100644 --- a/crates/swc_ecma_transforms_module/tests/amd.rs +++ b/crates/swc_ecma_transforms_module/tests/amd.rs @@ -1,13 +1,13 @@ use std::{fs::File, path::PathBuf, rc::Rc}; -use swc_common::{chain, comments::SingleThreadedComments, Mark}; +use swc_common::{comments::SingleThreadedComments, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_base::{feature::FeatureFlag, resolver}; use swc_ecma_transforms_compat::es2015::for_of; use swc_ecma_transforms_module::amd::{self, amd}; use swc_ecma_transforms_testing::{test, test_fixture, FixtureTestConfig}; use swc_ecma_transforms_typescript::typescript; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() @@ -17,13 +17,13 @@ fn ts_syntax() -> Syntax { Syntax::Typescript(TsSyntax::default()) } -fn tr(config: amd::Config, is_ts: bool, comments: Rc) -> impl Fold { +fn tr(config: amd::Config, is_ts: bool, comments: Rc) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); let avalible_set = FeatureFlag::all(); - chain!( + ( resolver(unresolved_mark, top_level_mark, is_ts), typescript::typescript(Default::default(), unresolved_mark, top_level_mark), amd( @@ -31,7 +31,7 @@ fn tr(config: amd::Config, is_ts: bool, comments: Rc) -> unresolved_mark, config, avalible_set, - Some(comments) + Some(comments), ), ) } @@ -75,7 +75,7 @@ fn esm_to_amd(input: PathBuf) { test!( module, syntax(), - |t| chain!( + |t| ( for_of(for_of::Config { assume_array: true, ..Default::default() diff --git a/crates/swc_ecma_transforms_module/tests/common_js.rs b/crates/swc_ecma_transforms_module/tests/common_js.rs index 65e7abe90439..83bc7fcae1f5 100644 --- a/crates/swc_ecma_transforms_module/tests/common_js.rs +++ b/crates/swc_ecma_transforms_module/tests/common_js.rs @@ -1,13 +1,13 @@ use std::{fs::File, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_base::{feature::FeatureFlag, resolver}; use swc_ecma_transforms_compat::es2015::for_of; use swc_ecma_transforms_module::common_js::{self, common_js}; use swc_ecma_transforms_testing::{test, test_fixture, FixtureTestConfig}; use swc_ecma_transforms_typescript::typescript; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() @@ -17,13 +17,13 @@ fn ts_syntax() -> Syntax { Syntax::Typescript(TsSyntax::default()) } -fn tr(config: common_js::Config, is_ts: bool) -> impl Fold { +fn tr(config: common_js::Config, is_ts: bool) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); let available_set = FeatureFlag::all(); - chain!( + ( resolver(unresolved_mark, top_level_mark, is_ts), typescript::typescript(Default::default(), unresolved_mark, top_level_mark), common_js(Default::default(), unresolved_mark, config, available_set), @@ -68,7 +68,7 @@ fn esm_to_cjs(input: PathBuf) { test!( module, syntax(), - |_| chain!( + |_| ( for_of(for_of::Config { assume_array: true, ..Default::default() diff --git a/crates/swc_ecma_transforms_module/tests/system_js.rs b/crates/swc_ecma_transforms_module/tests/system_js.rs index 98706027c11f..1526f683753b 100644 --- a/crates/swc_ecma_transforms_module/tests/system_js.rs +++ b/crates/swc_ecma_transforms_module/tests/system_js.rs @@ -2,23 +2,23 @@ use std::path::PathBuf; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_module::system_js::{system_js, Config}; use swc_ecma_transforms_testing::{test, test_fixture, FixtureTestConfig, Tester}; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Syntax::Es(Default::default()) } -fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Fold { +fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - system_js(Default::default(), unresolved_mark, config) + system_js(Default::default(), unresolved_mark, config), ) } diff --git a/crates/swc_ecma_transforms_module/tests/umd.rs b/crates/swc_ecma_transforms_module/tests/umd.rs index 6d1cbaec3227..f811a5962c87 100644 --- a/crates/swc_ecma_transforms_module/tests/umd.rs +++ b/crates/swc_ecma_transforms_module/tests/umd.rs @@ -1,12 +1,12 @@ use std::{fs::File, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_base::{feature::FeatureFlag, resolver}; use swc_ecma_transforms_module::umd::{umd, Config}; use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig, Tester}; use swc_ecma_transforms_typescript::typescript; -use swc_ecma_visit::Fold; fn syntax() -> Syntax { Default::default() @@ -16,13 +16,13 @@ fn ts_syntax() -> Syntax { Syntax::Typescript(TsSyntax::default()) } -fn tr(tester: &mut Tester<'_>, config: Config, typescript: bool) -> impl Fold { +fn tr(tester: &mut Tester<'_>, config: Config, typescript: bool) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); let avalible_set = FeatureFlag::all(); - chain!( + ( resolver(unresolved_mark, top_level_mark, typescript), typescript::typescript(Default::default(), unresolved_mark, top_level_mark), umd( diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs index 99176f605617..cd7ed4fc6682 100644 --- a/crates/swc_ecma_transforms_optimization/src/const_modules.rs +++ b/crates/swc_ecma_transforms_optimization/src/const_modules.rs @@ -17,13 +17,13 @@ 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, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; pub fn const_modules( cm: Lrc, globals: FxHashMap>, -) -> impl Fold { - as_folder(ConstModules { +) -> impl Pass { + visit_mut_pass(ConstModules { globals: globals .into_iter() .map(|(src, map)| { diff --git a/crates/swc_ecma_transforms_optimization/src/inline_globals.rs b/crates/swc_ecma_transforms_optimization/src/inline_globals.rs index 6d5e0a407cae..82b5d77204e6 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, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// The key will be compared using [EqIgnoreSpan::eq_ignore_span], and matched /// expressions will be replaced with the value. @@ -18,7 +18,7 @@ pub fn inline_globals( envs: Lrc>, globals: Lrc>, typeofs: Lrc>, -) -> impl Fold + VisitMut { +) -> impl Pass { inline_globals2(envs, globals, Default::default(), typeofs) } @@ -33,8 +33,8 @@ pub fn inline_globals2( globals: Lrc>, global_exprs: GlobalExprMap, typeofs: Lrc>, -) -> impl Fold + VisitMut { - as_folder(InlineGlobals { +) -> impl Pass { + visit_mut_pass(InlineGlobals { envs, globals, global_exprs, @@ -222,7 +222,7 @@ mod tests { let v = tester .apply_transform( - as_folder(DropSpan), + visit_mut_pass(DropSpan), "global.js", ::swc_ecma_parser::Syntax::default(), None, diff --git a/crates/swc_ecma_transforms_optimization/src/json_parse.rs b/crates/swc_ecma_transforms_optimization/src/json_parse.rs index b829f43947f7..0c2d41933215 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, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// Transform to optimize performance of literals. /// @@ -27,8 +27,8 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWit /// - Object literal is deeply nested (threshold: ) /// /// See /~https://github.com/swc-project/swc/issues/409 -pub fn json_parse(min_cost: usize) -> impl Fold { - as_folder(JsonParse { min_cost }) +pub fn json_parse(min_cost: usize) -> impl Pass { + visit_mut_pass(JsonParse { min_cost }) } struct JsonParse { 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 be7ec7040976..06a8acfcac7d 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs @@ -6,16 +6,13 @@ use swc_common::{ Mark, Spanned, SyntaxContext, DUMMY_SP, }; use swc_ecma_ast::*; -use swc_ecma_transforms_base::{ - pass::RepeatedJsPass, - perf::{cpu_count, Parallel, ParallelExt}, -}; +use swc_ecma_transforms_base::perf::{cpu_count, Parallel, ParallelExt}; use swc_ecma_utils::{ extract_var_ids, is_literal, prepend_stmt, ExprCtx, ExprExt, ExprFactory, Hoister, IsEmpty, StmtExt, StmtLike, Value::Known, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use tracing::{debug, trace}; @@ -25,8 +22,10 @@ mod tests; /// Not intended for general use. Use [simplifier] instead. /// /// Ported from `PeepholeRemoveDeadCode` of google closure compiler. -pub fn dead_branch_remover(unresolved_mark: Mark) -> impl RepeatedJsPass + VisitMut + 'static { - as_folder(Remover { +pub fn dead_branch_remover( + unresolved_mark: Mark, +) -> impl Repeated + Pass + CompilerPass + VisitMut + 'static { + visit_mut_pass(Remover { changed: false, normal_block: Default::default(), expr_ctx: ExprCtx { @@ -38,8 +37,8 @@ pub fn dead_branch_remover(unresolved_mark: Mark) -> impl RepeatedJsPass + Visit } impl CompilerPass for Remover { - fn name() -> Cow<'static, str> { - Cow::Borrowed("branch") + fn name(&self) -> Cow<'static, str> { + Cow::Borrowed("dead_branch_remover") } } diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs b/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs index 717c826c7c14..32d562d06065 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs @@ -1,7 +1,7 @@ -use swc_common::{chain, Mark, SyntaxContext}; +use swc_common::{Mark, SyntaxContext}; use swc_ecma_transforms_base::{fixer::paren_remover, resolver}; use swc_ecma_utils::ExprCtx; -use swc_ecma_visit::as_folder; +use swc_ecma_visit::visit_mut_pass; use super::super::expr_simplifier; @@ -14,11 +14,11 @@ macro_rules! test_stmt { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), paren_remover(None), expr_simplifier(top_level_mark, Default::default()), - as_folder(super::Remover { + visit_mut_pass(super::Remover { changed: false, normal_block: Default::default(), expr_ctx: ExprCtx { @@ -27,7 +27,7 @@ macro_rules! test_stmt { is_unresolved_ref_safe: true, in_strict: false, }, - }) + }), ) }, $l, 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 70edd3c67e36..6b3391343376 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/const_propagation.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/const_propagation.rs @@ -2,11 +2,11 @@ use swc_common::{collections::AHashMap, util::take::Take}; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; /// This pass is kind of inliner, but it's far faster. -pub fn constant_propagation() -> impl 'static + Fold + VisitMut { - as_folder(ConstPropagation::default()) +pub fn constant_propagation() -> impl 'static + Pass + VisitMut { + visit_mut_pass(ConstPropagation::default()) } #[derive(Default)] 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 669dc78df6a9..b4e1201b68ea 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs @@ -19,7 +19,7 @@ use swc_ecma_utils::{ collect_decls, find_pat_ids, ExprCtx, ExprExt, IsEmpty, ModuleItemLike, StmtLike, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use swc_fast_graph::digraph::FastDiGraphMap; use tracing::{debug, span, Level}; @@ -30,8 +30,8 @@ use crate::debug_assert_valid; pub fn dce( config: Config, unresolved_mark: Mark, -) -> impl Fold + VisitMut + Repeated + CompilerPass { - as_folder(TreeShaker { +) -> impl Pass + VisitMut + Repeated + CompilerPass { + visit_mut_pass(TreeShaker { expr_ctx: ExprCtx { unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), is_unresolved_ref_safe: false, @@ -97,7 +97,7 @@ struct TreeShaker { } impl CompilerPass for TreeShaker { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { Cow::Borrowed("tree-shaker") } } 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 aa701e2df2ea..e2c1621a0f93 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs @@ -9,14 +9,13 @@ use swc_common::{ use swc_ecma_ast::*; use swc_ecma_transforms_base::{ ext::ExprRefExt, - pass::RepeatedJsPass, perf::{cpu_count, Parallel, ParallelExt}, }; 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, noop_visit_mut_type, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use Value::{Known, Unknown}; use crate::debug::debug_assert_valid; @@ -43,8 +42,8 @@ pub struct Config {} pub fn expr_simplifier( unresolved_mark: Mark, config: Config, -) -> impl RepeatedJsPass + VisitMut + 'static { - as_folder(SimplifyExpr { +) -> impl Repeated + Pass + CompilerPass + VisitMut + 'static { + visit_mut_pass(SimplifyExpr { expr_ctx: ExprCtx { unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), is_unresolved_ref_safe: false, @@ -83,7 +82,7 @@ struct SimplifyExpr { } impl CompilerPass for SimplifyExpr { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { Cow::Borrowed("simplify-expr") } } diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/expr/tests.rs b/crates/swc_ecma_transforms_optimization/src/simplify/expr/tests.rs index 0eeee91861ca..3b564fe37f11 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/tests.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/tests.rs @@ -1,8 +1,8 @@ -use swc_common::{chain, Mark, SyntaxContext}; +use swc_common::{Mark, SyntaxContext}; use swc_ecma_transforms_base::{fixer::paren_remover, resolver}; use swc_ecma_transforms_testing::test_transform; use swc_ecma_utils::ExprCtx; -use swc_ecma_visit::as_folder; +use swc_ecma_visit::visit_mut_pass; use super::SimplifyExpr; @@ -14,10 +14,10 @@ fn fold(src: &str, expected: &str) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), paren_remover(None), - as_folder(SimplifyExpr { + visit_mut_pass(SimplifyExpr { expr_ctx: ExprCtx { unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), // This is hack @@ -29,7 +29,7 @@ fn fold(src: &str, expected: &str) { is_arg_of_update: false, is_modifying: false, in_callee: false, - }) + }), ) }, src, 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 2157c7ae14d6..d1692ab94cca 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/inlining/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/inlining/mod.rs @@ -5,10 +5,10 @@ use swc_common::{ util::take::Take, }; use swc_ecma_ast::*; -use swc_ecma_transforms_base::{pass::RepeatedJsPass, scope::IdentType}; +use swc_ecma_transforms_base::scope::IdentType; use swc_ecma_utils::{contains_this_expr, find_pat_ids}; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, visit_obj_and_computed, Visit, VisitMut, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, visit_obj_and_computed, Visit, VisitMut, VisitMutWith, VisitWith, }; use tracing::{span, Level}; @@ -33,8 +33,8 @@ pub struct Config {} /// /// Currently all functions are treated as a black box, and all the pass gives /// up inlining variables across a function call or a constructor call. -pub fn inlining(_: Config) -> impl 'static + RepeatedJsPass + VisitMut { - as_folder(Inlining { +pub fn inlining(_: Config) -> impl 'static + Repeated + CompilerPass + Pass + VisitMut { + visit_mut_pass(Inlining { phase: Phase::Analysis, is_first_run: true, changed: false, @@ -54,7 +54,7 @@ enum Phase { } impl CompilerPass for Inlining<'_> { - fn name() -> Cow<'static, str> { + fn name(&self) -> Cow<'static, str> { Cow::Borrowed("inlining") } } diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/mod.rs index 3d45ee1089f8..32fa5d3f5179 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/mod.rs @@ -1,6 +1,9 @@ //! Ported from closure compiler. -use swc_common::{chain, pass::Repeat, Mark}; -use swc_ecma_transforms_base::pass::RepeatedJsPass; +use swc_common::{ + pass::{CompilerPass, Repeat, Repeated}, + Mark, +}; +use swc_ecma_ast::Pass; pub use self::{ branch::dead_branch_remover, @@ -22,10 +25,10 @@ pub struct Config { /// Performs simplify-expr, inlining, remove-dead-branch and dce until nothing /// changes. -pub fn simplifier(unresolved_mark: Mark, c: Config) -> impl RepeatedJsPass { - Repeat::new(chain!( +pub fn simplifier(unresolved_mark: Mark, c: Config) -> impl CompilerPass + Pass + Repeated { + Repeat::new(( expr_simplifier(unresolved_mark, c.expr), dead_branch_remover(unresolved_mark), - dce::dce(c.dce, unresolved_mark) + dce::dce(c.dce, unresolved_mark), )) } diff --git a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs index 5e536929f6c8..6a7b45004ace 100644 --- a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs +++ b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs @@ -1,10 +1,10 @@ use std::{collections::HashMap, fs::read_to_string, path::PathBuf}; +use swc_ecma_ast::Pass; use swc_ecma_transforms_optimization::const_modules; use swc_ecma_transforms_testing::{test, test_fixture, Tester}; -use swc_ecma_visit::Fold; -fn tr(t: &mut Tester<'_>, sources: &[(&str, &[(&str, &str)])]) -> impl Fold { +fn tr(t: &mut Tester<'_>, sources: &[(&str, &[(&str, &str)])]) -> impl Pass { let mut m = HashMap::default(); for (src, values) in sources { diff --git a/crates/swc_ecma_transforms_optimization/tests/fixture.rs b/crates/swc_ecma_transforms_optimization/tests/fixture.rs index 9ecb1adb07f0..9a04f3a83696 100644 --- a/crates/swc_ecma_transforms_optimization/tests/fixture.rs +++ b/crates/swc_ecma_transforms_optimization/tests/fixture.rs @@ -1,14 +1,15 @@ use std::path::PathBuf; -use swc_common::{chain, pass::Repeat, Mark}; +use swc_common::{pass::Repeat, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_base::fixer::paren_remover; use swc_ecma_transforms_optimization::simplify::{dce::dce, expr_simplifier}; use swc_ecma_transforms_testing::{test_fixture, Tester}; -use swc_ecma_visit::{as_folder, Fold, VisitMut}; +use swc_ecma_visit::visit_mut_pass; -fn remover(t: &Tester) -> impl VisitMut + Fold { - as_folder(paren_remover(Some( +fn remover(t: &Tester) -> impl Pass { + visit_mut_pass(paren_remover(Some( Box::leak(Box::new(t.comments.clone())) as _ ))) } @@ -25,7 +26,7 @@ fn dce_single_pass(input: PathBuf) { &|t| { let unresolved_mark = Mark::new(); - chain!(remover(t), dce(Default::default(), unresolved_mark)) + (remover(t), dce(Default::default(), unresolved_mark)) }, &input, &output, @@ -43,9 +44,9 @@ fn dce_repeated(input: PathBuf) { ..Default::default() }), &|t| { - chain!( + ( remover(t), - Repeat::new(dce(Default::default(), Mark::new())) + Repeat::new(dce(Default::default(), Mark::new())), ) }, &input, @@ -64,7 +65,7 @@ fn dce_jsx(input: PathBuf) { jsx: true, ..Default::default() }), - &|t| chain!(remover(t), dce(Default::default(), Mark::new())), + &|t| (remover(t), dce(Default::default(), Mark::new())), &input, &output, Default::default(), @@ -80,9 +81,9 @@ fn expr(input: PathBuf) { &|t| { let top_level_mark = Mark::fresh(Mark::root()); - chain!( + ( remover(t), - Repeat::new(expr_simplifier(top_level_mark, Default::default())) + Repeat::new(expr_simplifier(top_level_mark, Default::default())), ) }, &input, diff --git a/crates/swc_ecma_transforms_optimization/tests/remove_imports_with_side_effects.rs b/crates/swc_ecma_transforms_optimization/tests/remove_imports_with_side_effects.rs index d98b0385b240..e747d68a17f1 100644 --- a/crates/swc_ecma_transforms_optimization/tests/remove_imports_with_side_effects.rs +++ b/crates/swc_ecma_transforms_optimization/tests/remove_imports_with_side_effects.rs @@ -1,11 +1,11 @@ -use swc_common::{chain, pass::Repeat, Mark}; +use swc_common::{pass::Repeat, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_optimization::simplify::dce::{dce, Config}; use swc_ecma_transforms_testing::test; -use swc_ecma_visit::Fold; -fn tr() -> impl Fold { +fn tr() -> impl Pass { Repeat::new(dce( Config { top_level: true, @@ -23,7 +23,7 @@ macro_rules! to { decorators: true, ..Default::default() }), - |_| chain!(resolver(Mark::new(), Mark::new(), false), tr()), + |_| (resolver(Mark::new(), Mark::new(), false), tr()), $name, $src ); diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify.rs b/crates/swc_ecma_transforms_optimization/tests/simplify.rs index 3a8ee1291d3c..de09e6ea629a 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify.rs @@ -2,7 +2,7 @@ #![deny(warnings)] -use swc_common::{chain, pass::Repeat, Mark}; +use swc_common::{pass::Repeat, Mark}; use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_base::{helpers::inject_helpers, resolver}; use swc_ecma_transforms_compat::{es2015, es2016, es2017, es2018, es2022::class_properties, es3}; @@ -24,13 +24,13 @@ fn test(src: &str, expected: &str) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - Repeat::new(chain!( + Repeat::new(( expr_simplifier(unresolved_mark, Default::default()), inlining::inlining(Default::default()), dead_branch_remover(unresolved_mark), - dce::dce(Default::default(), unresolved_mark) + dce::dce(Default::default(), unresolved_mark), )), ) }, @@ -51,13 +51,13 @@ macro_rules! to { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - Repeat::new(chain!( + Repeat::new(( expr_simplifier(unresolved_mark, Default::default()), inlining::inlining(Default::default()), dead_branch_remover(unresolved_mark), - dce::dce(Default::default(), unresolved_mark) + dce::dce(Default::default(), unresolved_mark), )), ) }, @@ -498,7 +498,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), class_properties( @@ -506,10 +506,10 @@ test!( set_public_fields: true, ..Default::default() }, - unresolved_mark + unresolved_mark, ), dce(Default::default(), unresolved_mark), - inlining(Default::default()) + inlining(Default::default()), ) }, issue_1156_1, @@ -549,16 +549,16 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( decorators(Default::default()), resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), class_properties(Default::default(), unresolved_mark), - Repeat::new(chain!( + Repeat::new(( expr_simplifier(unresolved_mark, Default::default()), inlining::inlining(Default::default()), dead_branch_remover(unresolved_mark), - dce::dce(Default::default(), unresolved_mark) + dce::dce(Default::default(), unresolved_mark), )), es2018(Default::default()), es2017(Default::default(), unresolved_mark), @@ -566,7 +566,7 @@ test!( es2015( unresolved_mark, Some(t.comments.clone()), - Default::default() + Default::default(), ), es3(true), import_analyzer(false.into(), false), @@ -644,12 +644,12 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - Repeat::new(chain!( + Repeat::new(( inlining(Default::default()), - dead_branch_remover(unresolved_mark) - )) + dead_branch_remover(unresolved_mark), + )), ) }, issue_4173, diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs b/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs index 9e5cce5180dd..8235ee21ed3e 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs @@ -1,4 +1,5 @@ -use swc_common::{chain, pass::Repeat, Mark}; +use swc_common::{pass::Repeat, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2022::class_properties; @@ -6,9 +7,8 @@ use swc_ecma_transforms_optimization::simplify::dce::{dce, Config}; use swc_ecma_transforms_proposal::decorators; use swc_ecma_transforms_testing::test; use swc_ecma_transforms_typescript::strip; -use swc_ecma_visit::Fold; -fn tr() -> impl Fold { +fn tr() -> impl Pass { Repeat::new(dce( Config { top_level: true, @@ -25,7 +25,7 @@ macro_rules! to { decorators: true, ..Default::default() }), - |_| chain!(resolver(Mark::new(), Mark::new(), false), tr()), + |_| (resolver(Mark::new(), Mark::new(), false), tr()), $name, $src ); @@ -375,7 +375,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( decorators(decorators::Config { legacy: true, emit_metadata: false, @@ -383,7 +383,7 @@ test!( }), resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), - tr() + tr(), ) }, issue_898_2, @@ -407,7 +407,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( decorators(decorators::Config { legacy: true, emit_metadata: false, @@ -415,7 +415,7 @@ test!( }), resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), - tr() + tr(), ) }, issue_1111, @@ -434,7 +434,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!(resolver(unresolved_mark, top_level_mark, false), tr()) + (resolver(unresolved_mark, top_level_mark, false), tr()) }, issue_1150_1, " @@ -469,7 +469,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), class_properties( @@ -477,9 +477,9 @@ test!( set_public_fields: true, ..Default::default() }, - unresolved_mark + unresolved_mark, ), - tr() + tr(), ) }, issue_1156_1, @@ -508,7 +508,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), class_properties( @@ -516,7 +516,7 @@ test!( set_public_fields: true, ..Default::default() }, - unresolved_mark + unresolved_mark, ), tr(), ) @@ -561,7 +561,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), tr(), @@ -590,7 +590,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), strip(unresolved_mark, top_level_mark), class_properties( @@ -598,7 +598,7 @@ test!( set_public_fields: true, ..Default::default() }, - unresolved_mark + unresolved_mark, ), tr(), ) diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs b/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs index 14359428850c..7e6b860cad7c 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify_inlining.rs @@ -1,15 +1,15 @@ //! Copied from /~https://github.com/google/closure-compiler/blob/6ca3b62990064488074a1a8931b9e8dc39b148b3/test/com/google/javascript/jscomp/InlineVariablesTest.java -use swc_common::{chain, Mark}; +use swc_common::Mark; +use swc_ecma_ast::Pass; use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2022::class_properties; use swc_ecma_transforms_optimization::simplify::inlining::inlining; 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(unresolved_mark: Mark, top_level_mark: Mark) -> impl Pass { typescript( typescript::Config { no_empty_export: true, @@ -24,7 +24,7 @@ macro_rules! to { ($name:ident, $src:expr) => { test!( Default::default(), - |_| chain!( + |_| ( resolver(Mark::new(), Mark::new(), false), inlining(Default::default()) ), @@ -37,7 +37,7 @@ macro_rules! to { test!( ignore, Default::default(), - |_| chain!( + |_| ( resolver(Mark::new(), Mark::new(), false), inlining(Default::default()) ), @@ -62,9 +62,9 @@ fn test(src: &str, expected: &str) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - inlining(Default::default()) + inlining(Default::default()), ) }, src, @@ -2065,7 +2065,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::fresh(Mark::root()); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), simple_strip(unresolved_mark, top_level_mark), class_properties( @@ -2073,9 +2073,9 @@ test!( set_public_fields: true, ..Default::default() }, - unresolved_mark + unresolved_mark, ), - inlining(Default::default()) + inlining(Default::default()), ) }, issue_1156_1, @@ -2103,7 +2103,7 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), simple_strip(unresolved_mark, top_level_mark), class_properties( @@ -2111,9 +2111,9 @@ test!( set_public_fields: true, ..Default::default() }, - unresolved_mark + unresolved_mark, ), - inlining(Default::default()) + inlining(Default::default()), ) }, issue_1156_2, @@ -2155,10 +2155,10 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), simple_strip(unresolved_mark, top_level_mark), - inlining(Default::default()) + inlining(Default::default()), ) }, deno_8180_1, @@ -2189,10 +2189,10 @@ test!( |_| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), simple_strip(unresolved_mark, top_level_mark), - inlining(Default::default()) + inlining(Default::default()), ) }, deno_8189_1, diff --git a/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs b/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs index 3cd36353e6cc..bf4db8b8fef4 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs @@ -1,7 +1,7 @@ -use swc_ecma_visit::{Fold, VisitMut}; +use swc_ecma_ast::Pass; use crate::{decorator_impl::decorator_impl, DecoratorVersion}; -pub fn decorator_2022_03() -> impl VisitMut + Fold { +pub fn decorator_2022_03() -> impl Pass { decorator_impl(DecoratorVersion::V202203) } diff --git a/crates/swc_ecma_transforms_proposal/src/decorator_impl.rs b/crates/swc_ecma_transforms_proposal/src/decorator_impl.rs index 862f8354b297..0b76e405a7d9 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorator_impl.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorator_impl.rs @@ -10,12 +10,12 @@ use swc_ecma_utils::{ is_maybe_branch_directive, private_ident, prop_name_to_expr_value, quote_ident, replace_ident, stack_size::maybe_grow_default, ExprFactory, IdentRenamer, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use crate::DecoratorVersion; -pub(crate) fn decorator_impl(version: DecoratorVersion) -> impl VisitMut + Fold { - as_folder(DecoratorPass { +pub(crate) fn decorator_impl(version: DecoratorVersion) -> impl Pass { + visit_mut_pass(DecoratorPass { version, ..Default::default() }) diff --git a/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs b/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs index a2c48c4a14f2..303677548331 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs @@ -3,14 +3,16 @@ use std::{iter, mem::take}; use either::Either; use serde::Deserialize; use swc_common::{Spanned, DUMMY_SP}; -use swc_ecma_ast::*; +use swc_ecma_ast::{Pass, *}; use swc_ecma_transforms_base::helper; use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder; use swc_ecma_utils::{ alias_ident_for, constructor::inject_after_super, default_constructor, prepend_stmt, private_ident, prop_name_to_expr, prop_name_to_expr_value, quote_ident, quote_str, ExprFactory, }; -use swc_ecma_visit::{as_folder, noop_fold_type, Fold, FoldWith, Visit, VisitWith}; +use swc_ecma_visit::{ + fold_pass, noop_fold_type, visit_mut_pass, Fold, FoldWith, Visit, VisitMutWith, VisitWith, +}; mod legacy; @@ -54,17 +56,17 @@ mod legacy; /// } /// } /// ``` -pub fn decorators(c: Config) -> impl Fold { +pub fn decorators(c: Config) -> impl Pass { if c.legacy { - Either::Left(as_folder(self::legacy::new(c.emit_metadata))) + Either::Left(visit_mut_pass(self::legacy::new(c.emit_metadata))) } else { if c.emit_metadata { unimplemented!("emitting decorator metadata while using new proposal") } - Either::Right(Decorators { + Either::Right(fold_pass(Decorators { is_in_strict: false, vars: Default::default(), - }) + })) } } @@ -330,8 +332,8 @@ impl Decorators { macro_rules! fold_method { ($method:expr, $fn_name:expr, $key_prop_value:expr) => {{ let fn_name = $fn_name; - let method = $method; - let mut folder = swc_ecma_visit::as_folder(SuperFieldAccessFolder { + let mut method = $method; + let mut folder = SuperFieldAccessFolder { class_name: &ident, constructor_this_mark: None, is_static: method.is_static, @@ -343,9 +345,9 @@ impl Decorators { constant_super: false, super_class: &None, in_pat: false, - }); + }; - let method = method.fold_with(&mut folder); + method.visit_mut_with(&mut folder); // kind: "method", // key: getKeyJ(), diff --git a/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs b/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs index db84b979c064..30f470d9e235 100644 --- a/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs +++ b/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs @@ -5,10 +5,10 @@ use swc_ecma_utils::{ private_ident, quote_ident, stack_size::maybe_grow_default, ExprFactory, ModuleItemLike, StmtLike, }; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; -pub fn explicit_resource_management() -> impl Fold + VisitMut { - as_folder(ExplicitResourceManagement::default()) +pub fn explicit_resource_management() -> impl Pass { + visit_mut_pass(ExplicitResourceManagement::default()) } #[derive(Default)] diff --git a/crates/swc_ecma_transforms_proposal/src/export_default_from.rs b/crates/swc_ecma_transforms_proposal/src/export_default_from.rs index dfda16d920d8..489b42da9055 100644 --- a/crates/swc_ecma_transforms_proposal/src/export_default_from.rs +++ b/crates/swc_ecma_transforms_proposal/src/export_default_from.rs @@ -1,11 +1,11 @@ use swc_common::DUMMY_SP; use swc_ecma_ast::*; use swc_ecma_utils::quote_ident; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut}; /// `@babel/plugin-proposal-export-default-from` -pub fn export_default_from() -> impl Fold + VisitMut { - as_folder(ExportDefaultFrom) +pub fn export_default_from() -> impl Pass { + visit_mut_pass(ExportDefaultFrom) } struct ExportDefaultFrom; diff --git a/crates/swc_ecma_transforms_proposal/src/import_assertions.rs b/crates/swc_ecma_transforms_proposal/src/import_assertions.rs index 5612a312f169..7d3f0800a44e 100644 --- a/crates/swc_ecma_transforms_proposal/src/import_assertions.rs +++ b/crates/swc_ecma_transforms_proposal/src/import_assertions.rs @@ -1,11 +1,11 @@ -use swc_ecma_ast::{ExportAll, ImportDecl, NamedExport}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut}; +use swc_ecma_ast::{ExportAll, ImportDecl, NamedExport, Pass}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut}; #[deprecated(note = "Please use `import_assertions` instead")] pub use self::import_attributes as import_assertions; -pub fn import_attributes() -> impl VisitMut + Fold { - as_folder(ImportAssertions) +pub fn import_attributes() -> impl Pass { + visit_mut_pass(ImportAssertions) } struct ImportAssertions; diff --git a/crates/swc_ecma_transforms_proposal/tests/decorator_evanw.rs b/crates/swc_ecma_transforms_proposal/tests/decorator_evanw.rs index d077a1c645f4..5ee0e0446103 100644 --- a/crates/swc_ecma_transforms_proposal/tests/decorator_evanw.rs +++ b/crates/swc_ecma_transforms_proposal/tests/decorator_evanw.rs @@ -3,7 +3,6 @@ use std::{fs, path::PathBuf}; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_proposal::decorator_2022_03::decorator_2022_03; use swc_ecma_transforms_testing::exec_tr; -use swc_ecma_visit::as_folder; const HELPERS: &str = r###" function assertEq(callback, expected) { @@ -64,7 +63,7 @@ fn fixture(input: PathBuf) { auto_accessors: true, ..Default::default() }), - |_| as_folder(decorator_2022_03()), + |_| decorator_2022_03(), &code, ); } diff --git a/crates/swc_ecma_transforms_proposal/tests/decorators.rs b/crates/swc_ecma_transforms_proposal/tests/decorators.rs index 82fd7340f8de..3306680b1f33 100644 --- a/crates/swc_ecma_transforms_proposal/tests/decorators.rs +++ b/crates/swc_ecma_transforms_proposal/tests/decorators.rs @@ -6,7 +6,8 @@ use std::{ }; use serde::Deserialize; -use swc_common::{chain, comments::SingleThreadedComments, Mark}; +use swc_common::{comments::SingleThreadedComments, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax}; use swc_ecma_transforms_base::{assumptions::Assumptions, resolver}; use swc_ecma_transforms_proposal::{decorator_2022_03::decorator_2022_03, DecoratorVersion}; @@ -111,18 +112,18 @@ enum BabelPluginOption { Decorator { version: DecoratorVersion }, } -fn create_pass(comments: Rc, input: &Path) -> Box { +fn create_pass(comments: Rc, input: &Path) -> Box { let options_json: BabelTestOptions = swc_ecma_transforms_testing::parse_options(input.parent().unwrap()); let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - let mut pass: Box = Box::new(resolver(unresolved_mark, top_level_mark, false)); + let mut pass: Box = Box::new(resolver(unresolved_mark, top_level_mark, false)); macro_rules! add { ($e:expr) => {{ - pass = Box::new(chain!(pass, $e)); + pass = Box::new((pass, $e)); }}; } diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit_resource_management.rs b/crates/swc_ecma_transforms_proposal/tests/explicit_resource_management.rs index 562231392b13..3f98b812b714 100644 --- a/crates/swc_ecma_transforms_proposal/tests/explicit_resource_management.rs +++ b/crates/swc_ecma_transforms_proposal/tests/explicit_resource_management.rs @@ -1,6 +1,6 @@ use std::{fs::read_to_string, path::PathBuf}; -use swc_common::{chain, Mark}; +use swc_common::Mark; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_proposal::explicit_resource_management::explicit_resource_management; @@ -19,9 +19,9 @@ fn exec(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - explicit_resource_management() + explicit_resource_management(), ) }, &read_to_string(input).unwrap(), @@ -49,9 +49,9 @@ fn run_fixture(input: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), - explicit_resource_management() + explicit_resource_management(), ) }, &input, diff --git a/crates/swc_ecma_transforms_proposal/tests/export.rs b/crates/swc_ecma_transforms_proposal/tests/export.rs index b5397987a376..7c68ab27eace 100644 --- a/crates/swc_ecma_transforms_proposal/tests/export.rs +++ b/crates/swc_ecma_transforms_proposal/tests/export.rs @@ -1,9 +1,8 @@ -use swc_common::chain; +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_compat::es2020::export_namespace_from; use swc_ecma_transforms_proposal::export_default_from; use swc_ecma_transforms_testing::test; -use swc_ecma_visit::Fold; fn syntax_default() -> Syntax { Syntax::Es(EsSyntax { @@ -15,8 +14,8 @@ fn syntax_namespace() -> Syntax { Syntax::Es(Default::default()) } -fn tr() -> impl Fold { - chain!(export_default_from(), export_namespace_from()) +fn tr() -> impl Pass { + (export_default_from(), export_namespace_from()) } test!( diff --git a/crates/swc_ecma_transforms_proposal/tests/import_assertions.rs b/crates/swc_ecma_transforms_proposal/tests/import_assertions.rs index ec349abb8a71..3c9b5abd28eb 100644 --- a/crates/swc_ecma_transforms_proposal/tests/import_assertions.rs +++ b/crates/swc_ecma_transforms_proposal/tests/import_assertions.rs @@ -1,9 +1,9 @@ +use swc_ecma_ast::Pass; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_proposal::import_assertions; use swc_ecma_transforms_testing::test; -use swc_ecma_visit::Fold; -fn tr() -> impl Fold { +fn tr() -> impl Pass { import_assertions() } diff --git a/crates/swc_ecma_transforms_react/src/display_name/mod.rs b/crates/swc_ecma_transforms_react/src/display_name/mod.rs index 867b05d0eb54..e4527cc545d6 100644 --- a/crates/swc_ecma_transforms_react/src/display_name/mod.rs +++ b/crates/swc_ecma_transforms_react/src/display_name/mod.rs @@ -2,7 +2,7 @@ use std::ops::DerefMut; use swc_common::DUMMY_SP; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; #[cfg(test)] mod tests; @@ -10,8 +10,8 @@ mod tests; /// `@babel/plugin-transform-react-display-name` /// /// Add displayName to React.createClass calls -pub fn display_name() -> impl Fold + VisitMut { - as_folder(DisplayName) +pub fn display_name() -> impl Pass { + visit_mut_pass(DisplayName) } struct DisplayName; diff --git a/crates/swc_ecma_transforms_react/src/display_name/tests.rs b/crates/swc_ecma_transforms_react/src/display_name/tests.rs index ec11f08bb40b..02bbbc098678 100644 --- a/crates/swc_ecma_transforms_react/src/display_name/tests.rs +++ b/crates/swc_ecma_transforms_react/src/display_name/tests.rs @@ -2,7 +2,7 @@ use swc_ecma_transforms_testing::test; use super::*; -fn tr() -> impl Fold { +fn tr() -> impl Pass { display_name() } diff --git a/crates/swc_ecma_transforms_react/src/jsx/mod.rs b/crates/swc_ecma_transforms_react/src/jsx/mod.rs index 3ed7374896f5..f496f8f7de25 100644 --- a/crates/swc_ecma_transforms_react/src/jsx/mod.rs +++ b/crates/swc_ecma_transforms_react/src/jsx/mod.rs @@ -17,7 +17,7 @@ use swc_config::merge::Merge; use swc_ecma_ast::*; use swc_ecma_parser::{parse_file_as_expr, Syntax}; use swc_ecma_utils::{drop_span, prepend_stmt, private_ident, quote_ident, ExprFactory, StmtLike}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; use self::static_check::should_use_create_element; use crate::refresh::options::{deserialize_refresh, RefreshOptions}; @@ -189,11 +189,11 @@ pub fn jsx( options: Options, top_level_mark: Mark, unresolved_mark: Mark, -) -> impl Fold + VisitMut +) -> impl Pass + VisitMut where C: Comments, { - as_folder(Jsx { + visit_mut_pass(Jsx { cm: cm.clone(), top_level_mark, unresolved_mark, diff --git a/crates/swc_ecma_transforms_react/src/jsx/tests.rs b/crates/swc_ecma_transforms_react/src/jsx/tests.rs index 66541a384d34..7b3eb2aee19d 100644 --- a/crates/swc_ecma_transforms_react/src/jsx/tests.rs +++ b/crates/swc_ecma_transforms_react/src/jsx/tests.rs @@ -5,7 +5,6 @@ use std::{ path::{Path, PathBuf}, }; -use swc_common::chain; use swc_ecma_codegen::{Config, Emitter}; use swc_ecma_parser::{EsSyntax, Parser, StringInput}; use swc_ecma_transforms_base::{fixer::fixer, hygiene, resolver}; @@ -14,23 +13,22 @@ use swc_ecma_transforms_compat::{ es3::property_literals, }; use swc_ecma_transforms_testing::{parse_options, test, test_fixture, FixtureTestConfig, Tester}; -use swc_ecma_visit::FoldWith; use testing::NormalizedOutput; use super::*; use crate::{display_name, pure_annotations, react}; -fn tr(t: &mut Tester, options: Options, top_level_mark: Mark) -> impl Fold { +fn tr(t: &mut Tester, options: Options, top_level_mark: Mark) -> impl Pass { let unresolved_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), jsx( t.cm.clone(), Some(t.comments.clone()), options, top_level_mark, - unresolved_mark + unresolved_mark, ), display_name(), classes(Default::default()), @@ -61,7 +59,7 @@ fn true_by_default() -> bool { true } -fn fixture_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Fold { +fn fixture_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); @@ -71,7 +69,7 @@ fn fixture_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Fold { options.options.runtime = Some(Runtime::Classic); } - chain!( + ( resolver(unresolved_mark, top_level_mark, false), jsx( t.cm.clone(), @@ -81,11 +79,11 @@ fn fixture_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Fold { unresolved_mark, ), display_name(), - pure_annotations(Some(t.comments.clone())) + pure_annotations(Some(t.comments.clone())), ) } -fn integration_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Fold { +fn integration_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); @@ -95,14 +93,14 @@ fn integration_tr(t: &mut Tester, mut options: FixtureOptions) -> impl Fold { options.options.runtime = Some(Runtime::Classic); } - chain!( + ( resolver(unresolved_mark, top_level_mark, false), react( t.cm.clone(), Some(t.comments.clone()), options.options, top_level_mark, - unresolved_mark + unresolved_mark, ), display_name(), ) @@ -413,7 +411,7 @@ test!( jsx: true, ..Default::default() }), - |t| chain!( + |t| ( tr(t, Default::default(), Mark::fresh(Mark::root())), property_literals(), ), @@ -986,15 +984,15 @@ test!( let top_level_mark = Mark::fresh(Mark::root()); let unresolved_mark = Mark::fresh(Mark::root()); - chain!( + ( classes(Default::default()), jsx( t.cm.clone(), Some(t.comments.clone()), Default::default(), top_level_mark, - unresolved_mark - ) + unresolved_mark, + ), ) }, regression_2775, @@ -1028,15 +1026,15 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), jsx( t.cm.clone(), Some(t.comments.clone()), Default::default(), top_level_mark, - unresolved_mark - ) + unresolved_mark, + ), ) }, issue_4956, @@ -1126,7 +1124,7 @@ fn test_script(src: &str, output: &Path, options: Options) { let top_level_mark = Mark::new(); let unresolved_mark = Mark::new(); - let script = script.fold_with(&mut chain!( + let script = Program::Script(script).apply(( resolver(Mark::new(), top_level_mark, false), react( tester.cm.clone(), @@ -1136,7 +1134,7 @@ fn test_script(src: &str, output: &Path, options: Options) { unresolved_mark, ), hygiene::hygiene(), - fixer(Some(&tester.comments)) + fixer(Some(&tester.comments)), )); let mut buf = Vec::new(); @@ -1156,7 +1154,7 @@ fn test_script(src: &str, output: &Path, options: Options) { }; // println!("Emitting: {:?}", module); - emitter.emit_script(&script).unwrap(); + emitter.emit_program(&script).unwrap(); let s = String::from_utf8_lossy(&buf).to_string(); assert!(NormalizedOutput::new_raw(s).compare_to_file(output).is_ok()); diff --git a/crates/swc_ecma_transforms_react/src/jsx_self/mod.rs b/crates/swc_ecma_transforms_react/src/jsx_self/mod.rs index 0b4ba3a8c4f4..1bdeeccb945b 100644 --- a/crates/swc_ecma_transforms_react/src/jsx_self/mod.rs +++ b/crates/swc_ecma_transforms_react/src/jsx_self/mod.rs @@ -2,7 +2,7 @@ use swc_common::DUMMY_SP; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::quote_ident; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut}; #[cfg(test)] mod tests; @@ -10,8 +10,8 @@ mod tests; /// `@babel/plugin-transform-react-jsx-self` /// /// Add a __self prop to all JSX Elements -pub fn jsx_self(dev: bool) -> impl Fold + VisitMut { - as_folder(JsxSelf { dev }) +pub fn jsx_self(dev: bool) -> impl Pass { + visit_mut_pass(JsxSelf { dev }) } #[derive(Clone, Copy)] diff --git a/crates/swc_ecma_transforms_react/src/jsx_self/tests.rs b/crates/swc_ecma_transforms_react/src/jsx_self/tests.rs index 1315aecf2a64..8beb2294c854 100644 --- a/crates/swc_ecma_transforms_react/src/jsx_self/tests.rs +++ b/crates/swc_ecma_transforms_react/src/jsx_self/tests.rs @@ -2,7 +2,7 @@ use swc_ecma_transforms_testing::test; use super::*; -fn tr() -> impl Fold { +fn tr() -> impl Pass { jsx_self(true) } diff --git a/crates/swc_ecma_transforms_react/src/jsx_src/mod.rs b/crates/swc_ecma_transforms_react/src/jsx_src/mod.rs index cb8a1c2498ba..615cfb194d29 100644 --- a/crates/swc_ecma_transforms_react/src/jsx_src/mod.rs +++ b/crates/swc_ecma_transforms_react/src/jsx_src/mod.rs @@ -2,14 +2,14 @@ use swc_common::{sync::Lrc, SourceMap, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::Parallel; use swc_ecma_utils::quote_ident; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; #[cfg(test)] mod tests; /// `@babel/plugin-transform-react-jsx-source` -pub fn jsx_src(dev: bool, cm: Lrc) -> impl Fold + VisitMut { - as_folder(JsxSrc { cm, dev }) +pub fn jsx_src(dev: bool, cm: Lrc) -> impl Pass { + visit_mut_pass(JsxSrc { cm, dev }) } #[derive(Clone)] diff --git a/crates/swc_ecma_transforms_react/src/jsx_src/tests.rs b/crates/swc_ecma_transforms_react/src/jsx_src/tests.rs index 9523617842f0..79e24b9e50d3 100644 --- a/crates/swc_ecma_transforms_react/src/jsx_src/tests.rs +++ b/crates/swc_ecma_transforms_react/src/jsx_src/tests.rs @@ -3,7 +3,7 @@ use swc_ecma_transforms_testing::{test, test_exec}; use super::*; -fn tr() -> impl Fold { +fn tr() -> impl Pass { let cm = Lrc::new(SourceMap::new(FilePathMapping::empty())); jsx_src(true, cm) } diff --git a/crates/swc_ecma_transforms_react/src/lib.rs b/crates/swc_ecma_transforms_react/src/lib.rs index d1b76383349f..97a2a5324f46 100644 --- a/crates/swc_ecma_transforms_react/src/lib.rs +++ b/crates/swc_ecma_transforms_react/src/lib.rs @@ -3,8 +3,8 @@ #![allow(clippy::arc_with_non_send_sync)] #![allow(rustc::untranslatable_diagnostic_trivial)] -use swc_common::{chain, comments::Comments, sync::Lrc, Mark, SourceMap}; -use swc_ecma_visit::{Fold, VisitMut}; +use swc_common::{comments::Comments, sync::Lrc, Mark, SourceMap}; +use swc_ecma_ast::Pass; pub use self::{ display_name::display_name, @@ -41,7 +41,7 @@ pub fn react( mut options: Options, top_level_mark: Mark, unresolved_mark: Mark, -) -> impl Fold + VisitMut +) -> impl Pass where C: Comments + Clone, { @@ -50,24 +50,24 @@ where let refresh_options = options.refresh.take(); - chain!( + ( jsx_src(development, cm.clone()), jsx_self(development), refresh( development, - refresh_options, + refresh_options.clone(), cm.clone(), comments.clone(), - top_level_mark + top_level_mark, ), jsx( - cm, + cm.clone(), comments.clone(), options, top_level_mark, - unresolved_mark + unresolved_mark, ), display_name(), - pure_annotations(comments), + pure_annotations(comments.clone()), ) } diff --git a/crates/swc_ecma_transforms_react/src/pure_annotations/mod.rs b/crates/swc_ecma_transforms_react/src/pure_annotations/mod.rs index f16b8ab83817..ec96c2f89fa2 100644 --- a/crates/swc_ecma_transforms_react/src/pure_annotations/mod.rs +++ b/crates/swc_ecma_transforms_react/src/pure_annotations/mod.rs @@ -1,7 +1,7 @@ use swc_atoms::JsWord; use swc_common::{collections::AHashMap, comments::Comments, Span}; use swc_ecma_ast::*; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith}; #[cfg(test)] mod tests; @@ -12,11 +12,11 @@ mod tests; /// React methods, so that terser and other minifiers can safely remove them /// during dead code elimination. /// See https://reactjs.org/docs/react-api.html -pub fn pure_annotations(comments: Option) -> impl Fold + VisitMut +pub fn pure_annotations(comments: Option) -> impl Pass where C: Comments, { - as_folder(PureAnnotations { + visit_mut_pass(PureAnnotations { imports: Default::default(), comments, }) diff --git a/crates/swc_ecma_transforms_react/src/pure_annotations/tests.rs b/crates/swc_ecma_transforms_react/src/pure_annotations/tests.rs index ed6162833fc7..600bb0fc0f0a 100644 --- a/crates/swc_ecma_transforms_react/src/pure_annotations/tests.rs +++ b/crates/swc_ecma_transforms_react/src/pure_annotations/tests.rs @@ -4,14 +4,13 @@ use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; use swc_ecma_parser::{Parser, StringInput}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::Tester; -use swc_ecma_visit::FoldWith; use super::*; fn parse( tester: &mut Tester, src: &str, -) -> Result<(Module, Lrc, Lrc), ()> { +) -> Result<(Program, Lrc, Lrc), ()> { let syntax = ::swc_ecma_parser::Syntax::Es(::swc_ecma_parser::EsSyntax { jsx: true, ..Default::default() @@ -20,7 +19,7 @@ fn parse( let source_file = source_map.new_source_file(FileName::Anon.into(), src.into()); let comments = Lrc::new(SingleThreadedComments::default()); - let module = { + let program = { let mut p = Parser::new(syntax, StringInput::from(&*source_file), Some(&comments)); let res = p .parse_module() @@ -30,16 +29,16 @@ fn parse( e.into_diagnostic(tester.handler).emit() } - res? + Program::Module(res?) }; - Ok((module, source_map, comments)) + Ok((program, source_map, comments)) } fn emit( source_map: Lrc, comments: Lrc, - program: &Module, + program: &Program, ) -> String { let mut src_map_buf = Vec::new(); let mut buf = std::vec::Vec::new(); @@ -56,7 +55,7 @@ fn emit( cm: source_map, wr: writer, }; - emitter.emit_module(program).unwrap(); + emitter.emit_program(program).unwrap(); } String::from_utf8(buf).unwrap() @@ -69,8 +68,8 @@ fn run_test(input: &str, expected: &str) { let (actual, actual_sm, actual_comments) = parse(tester, input)?; let actual = actual - .fold_with(&mut resolver(unresolved_mark, top_level_mark, false)) - .fold_with(&mut crate::react( + .apply(&mut resolver(unresolved_mark, top_level_mark, false)) + .apply(&mut crate::react( actual_sm.clone(), Some(&actual_comments), Default::default(), diff --git a/crates/swc_ecma_transforms_react/src/refresh/mod.rs b/crates/swc_ecma_transforms_react/src/refresh/mod.rs index 75d771d732c1..600ebaa00da1 100644 --- a/crates/swc_ecma_transforms_react/src/refresh/mod.rs +++ b/crates/swc_ecma_transforms_react/src/refresh/mod.rs @@ -4,7 +4,7 @@ use swc_common::{ }; use swc_ecma_ast::*; use swc_ecma_utils::{private_ident, quote_ident, quote_str, ExprFactory}; -use swc_ecma_visit::{as_folder, Fold, Visit, VisitMut, VisitMutWith}; +use swc_ecma_visit::{visit_mut_pass, Visit, VisitMut, VisitMutWith}; use self::{ hook::HookRegister, @@ -52,8 +52,8 @@ pub fn refresh( cm: Lrc, comments: Option, global_mark: Mark, -) -> impl Fold + VisitMut { - as_folder(Refresh { +) -> impl Pass { + visit_mut_pass(Refresh { enable: dev && options.is_some(), cm, comments, diff --git a/crates/swc_ecma_transforms_react/src/refresh/tests.rs b/crates/swc_ecma_transforms_react/src/refresh/tests.rs index 8d5c63c1d332..49958ceafdf1 100644 --- a/crates/swc_ecma_transforms_react/src/refresh/tests.rs +++ b/crates/swc_ecma_transforms_react/src/refresh/tests.rs @@ -1,15 +1,14 @@ -use swc_common::chain; 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 { +fn tr(t: &mut Tester) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), refresh( true, @@ -19,8 +18,8 @@ fn tr(t: &mut Tester) -> impl Fold { }), t.cm.clone(), Some(t.comments.clone()), - top_level_mark - ) + top_level_mark, + ), ) } @@ -569,7 +568,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), refresh( true, @@ -579,14 +578,14 @@ test!( }), t.cm.clone(), Some(t.comments.clone()), - top_level_mark + top_level_mark, ), jsx( t.cm.clone(), Some(t.comments.clone()), Default::default(), top_level_mark, - unresolved_mark + unresolved_mark, ), ) }, @@ -706,7 +705,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), refresh( true, @@ -717,8 +716,8 @@ test!( }), t.cm.clone(), Some(t.comments.clone()), - top_level_mark - ) + top_level_mark, + ), ) }, custom_identifier, diff --git a/crates/swc_ecma_transforms_testing/src/babel_like.rs b/crates/swc_ecma_transforms_testing/src/babel_like.rs index 1cd8974c5aa8..dad9d68a5542 100644 --- a/crates/swc_ecma_transforms_testing/src/babel_like.rs +++ b/crates/swc_ecma_transforms_testing/src/babel_like.rs @@ -3,8 +3,8 @@ use std::{fs::read_to_string, path::Path}; use ansi_term::Color; use serde::Deserialize; use serde_json::Value; -use swc_common::{chain, comments::SingleThreadedComments, sync::Lrc, Mark, SourceMap}; -use swc_ecma_ast::{EsVersion, Program}; +use swc_common::{comments::SingleThreadedComments, sync::Lrc, Mark, SourceMap}; +use swc_ecma_ast::{EsVersion, Pass, Program}; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{parse_file_as_program, Syntax}; use swc_ecma_transforms_base::{ @@ -14,13 +14,12 @@ use swc_ecma_transforms_base::{ hygiene::hygiene, resolver, }; -use swc_ecma_visit::{Fold, FoldWith, VisitMutWith}; use testing::NormalizedOutput; use crate::{exec_with_node_test_runner, parse_options, stdout_of}; pub type PassFactory<'a> = - Box) -> Option>>; + Box) -> Option>>; /// These tests use `options.json`. /// @@ -87,7 +86,7 @@ impl<'a> BabelLikeFixtureTest<'a> { comments: comments.clone(), }; - let mut pass: Box = Box::new(resolver( + let mut pass: Box = Box::new(resolver( builder.unresolved_mark, builder.top_level_mark, self.syntax.typescript(), @@ -105,7 +104,7 @@ impl<'a> BabelLikeFixtureTest<'a> { let mut done = false; for factory in &mut factories { if let Some(built) = factory(&builder, &name, options.clone()) { - pass = Box::new(chain!(pass, built)); + pass = Box::new((pass, built)); done = true; break; } @@ -116,7 +115,7 @@ impl<'a> BabelLikeFixtureTest<'a> { } } - pass = Box::new(chain!(pass, hygiene(), fixer(Some(&comments)))); + pass = Box::new((pass, hygiene(), fixer(Some(&comments)))); // Run pass @@ -164,12 +163,12 @@ impl<'a> BabelLikeFixtureTest<'a> { let helpers = Helpers::new(output_path.is_some()); let (code_without_helper, output_program) = HELPERS.set(&helpers, || { - let mut p = input_program.fold_with(&mut *pass); + let mut p = input_program.apply(pass); let code_without_helper = builder.print(&p); if output_path.is_none() { - p.visit_mut_with(&mut inject_helpers(builder.unresolved_mark)) + p.mutate(inject_helpers(builder.unresolved_mark)) } (code_without_helper, p) diff --git a/crates/swc_ecma_transforms_testing/src/lib.rs b/crates/swc_ecma_transforms_testing/src/lib.rs index 3a5ec07e288f..38bb0231e1a1 100644 --- a/crates/swc_ecma_transforms_testing/src/lib.rs +++ b/crates/swc_ecma_transforms_testing/src/lib.rs @@ -20,7 +20,6 @@ use base64::prelude::{Engine, BASE64_STANDARD}; use serde::de::DeserializeOwned; use sha2::{Digest, Sha256}; use swc_common::{ - chain, comments::{Comments, SingleThreadedComments}, errors::{Handler, HANDLER}, source_map::SourceMapGenConfig, @@ -35,10 +34,9 @@ use swc_ecma_transforms_base::{ fixer, helpers::{inject_helpers, HELPERS}, hygiene, - pass::noop, }; use swc_ecma_utils::{quote_ident, quote_str, ExprFactory}; -use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, FoldWith, VisitMut}; +use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, Fold, FoldWith, VisitMut}; use tempfile::tempdir_in; use testing::{ assert_eq, find_executable, NormalizedOutput, CARGO_TARGET_DIR, CARGO_WORKSPACE_ROOT, @@ -162,9 +160,9 @@ impl Tester<'_> { Ok(stmts.pop().unwrap()) } - pub fn apply_transform( + pub fn apply_transform( &mut self, - mut tr: T, + tr: T, name: &str, syntax: Syntax, is_module: Option, @@ -182,7 +180,7 @@ impl Tester<'_> { }, )?; - Ok(program.fold_with(&mut tr)) + Ok(program.apply(tr)) } pub fn print(&mut self, program: &Program, comments: &Rc) -> String { @@ -237,14 +235,6 @@ impl VisitMut for RegeneratorHandler { } } -fn make_tr(op: F, tester: &mut Tester<'_>) -> impl Fold -where - F: FnOnce(&mut Tester<'_>) -> P, - P: Fold, -{ - chain!(op(tester), as_folder(RegeneratorHandler)) -} - #[track_caller] pub fn test_transform( syntax: Syntax, @@ -254,11 +244,11 @@ pub fn test_transform( expected: &str, ) where F: FnOnce(&mut Tester) -> P, - P: Fold, + P: Pass, { Tester::run(|tester| { let expected = tester.apply_transform( - as_folder(::swc_ecma_utils::DropSpan), + swc_ecma_utils::DropSpan, "output.js", syntax, is_module, @@ -269,7 +259,7 @@ pub fn test_transform( println!("----- Actual -----"); - let tr = make_tr(tr, tester); + let tr = (tr(tester), visit_mut_pass(RegeneratorHandler)); let actual = tester.apply_transform(tr, "input.js", syntax, is_module, input)?; match ::std::env::var("PRINT_HYGIENE") { @@ -284,9 +274,9 @@ pub fn test_transform( } let actual = actual - .fold_with(&mut as_folder(::swc_ecma_utils::DropSpan)) - .fold_with(&mut hygiene::hygiene()) - .fold_with(&mut fixer::fixer(Some(&tester.comments))); + .apply(::swc_ecma_utils::DropSpan) + .apply(hygiene::hygiene()) + .apply(fixer::fixer(Some(&tester.comments))); println!("{:?}", tester.comments); println!("{:?}", expected_comments); @@ -338,7 +328,7 @@ pub fn test_inline_input_output( output: &str, ) where F: FnOnce(&mut Tester) -> P, - P: Fold, + P: Pass, { let _logger = testing::init(); @@ -346,7 +336,7 @@ pub fn test_inline_input_output( let expected_src = Tester::run(|tester| { let expected_program = - tester.apply_transform(noop(), "expected.js", syntax, is_module, expected)?; + tester.apply_transform(noop_pass(), "expected.js", syntax, is_module, expected)?; let expected_src = tester.print(&expected_program, &Default::default()); @@ -384,8 +374,8 @@ pub fn test_inline_input_output( } let actual = actual - .fold_with(&mut crate::hygiene::hygiene()) - .fold_with(&mut crate::fixer::fixer(Some(&tester.comments))); + .apply(crate::hygiene::hygiene()) + .apply(crate::fixer::fixer(Some(&tester.comments))); let actual_src = tester.print(&actual, &Default::default()); @@ -411,7 +401,7 @@ pub fn test_inlined_transform( input: &str, ) where F: FnOnce(&mut Tester) -> P, - P: Fold, + P: Pass, { let loc = panic::Location::caller(); @@ -467,7 +457,7 @@ macro_rules! test_inline { test_inline!( ignore, Syntax::default(), - |_| noop(), + |_| noop_pass(), test_inline_ignored, "class Foo {}", "class Foo {}" @@ -475,7 +465,7 @@ test_inline!( test_inline!( Syntax::default(), - |_| noop(), + |_| noop_pass(), test_inline_pass, "class Foo {}", "class Foo {}" @@ -484,7 +474,13 @@ test_inline!( #[test] #[should_panic] fn test_inline_should_fail() { - test_inline_input_output(Default::default(), None, |_| noop(), "class Foo {}", ""); + test_inline_input_output( + Default::default(), + None, + |_| noop_pass(), + "class Foo {}", + "", + ); } #[macro_export] @@ -537,10 +533,10 @@ macro_rules! test { pub fn compare_stdout(syntax: Syntax, tr: F, input: &str) where F: FnOnce(&mut Tester<'_>) -> P, - P: Fold, + P: Pass, { Tester::run(|tester| { - let tr = make_tr(tr, tester); + let tr = (tr(tester), visit_mut_pass(RegeneratorHandler)); let program = tester.apply_transform(tr, "input.js", syntax, Some(true), input)?; @@ -556,11 +552,11 @@ where } let mut program = program - .fold_with(&mut hygiene::hygiene()) - .fold_with(&mut fixer::fixer(Some(&tester.comments))); + .apply(hygiene::hygiene()) + .apply(fixer::fixer(Some(&tester.comments))); let src_without_helpers = tester.print(&program, &tester.comments.clone()); - program = program.fold_with(&mut inject_helpers(Mark::fresh(Mark::root()))); + program = program.apply(inject_helpers(Mark::fresh(Mark::root()))); let transformed_src = tester.print(&program, &tester.comments.clone()); @@ -585,10 +581,10 @@ where pub fn exec_tr(_test_name: &str, syntax: Syntax, tr: F, input: &str) where F: FnOnce(&mut Tester<'_>) -> P, - P: Fold, + P: Pass, { Tester::run(|tester| { - let tr = make_tr(tr, tester); + let tr = (tr(tester), visit_mut_pass(RegeneratorHandler)); let program = tester.apply_transform( tr, @@ -614,11 +610,11 @@ where } let mut program = program - .fold_with(&mut hygiene::hygiene()) - .fold_with(&mut fixer::fixer(Some(&tester.comments))); + .apply(hygiene::hygiene()) + .apply(fixer::fixer(Some(&tester.comments))); let src_without_helpers = tester.print(&program, &tester.comments.clone()); - program = program.fold_with(&mut inject_helpers(Mark::fresh(Mark::root()))); + program = program.apply(inject_helpers(Mark::fresh(Mark::root()))); let src = tester.print(&program, &tester.comments.clone()); @@ -862,7 +858,7 @@ pub fn test_fixture

( output: &Path, config: FixtureTestConfig, ) where - P: Fold, + P: Pass, { let input = fs::read_to_string(input).unwrap(); @@ -877,7 +873,7 @@ pub fn test_fixture

( fn test_fixture_inner<'a>( syntax: Syntax, - tr: Box Box>, + tr: Box Box>, input: &str, output: &Path, config: FixtureTestConfig, @@ -890,7 +886,7 @@ fn test_fixture_inner<'a>( let expected_src = Tester::run(|tester| { let expected_program = - tester.apply_transform(noop(), "expected.js", syntax, config.module, &expected)?; + tester.apply_transform(noop_pass(), "expected.js", syntax, config.module, &expected)?; let expected_src = tester.print(&expected_program, &tester.comments.clone()); @@ -939,8 +935,8 @@ fn test_fixture_inner<'a>( } let actual = actual - .fold_with(&mut crate::hygiene::hygiene()) - .fold_with(&mut crate::fixer::fixer(Some(&tester.comments))); + .apply(crate::hygiene::hygiene()) + .apply(crate::fixer::fixer(Some(&tester.comments))); let actual_src = { let module = &actual; diff --git a/crates/swc_ecma_transforms_testing/tests/visited.rs b/crates/swc_ecma_transforms_testing/tests/visited.rs index 6621303e0aeb..3f3721ce0eaa 100644 --- a/crates/swc_ecma_transforms_testing/tests/visited.rs +++ b/crates/swc_ecma_transforms_testing/tests/visited.rs @@ -1,7 +1,7 @@ use swc_ecma_ast::*; use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_testing::test_transform; -use swc_ecma_visit::Fold; +use swc_ecma_visit::{fold_pass, Fold}; struct Panicking; @@ -27,7 +27,7 @@ fn ensure_visited() { ..Default::default() }), None, - |_| Panicking, + |_| fold_pass(Panicking), " import React from 'react'; const comp = () => ; diff --git a/crates/swc_ecma_transforms_typescript/benches/compat.rs b/crates/swc_ecma_transforms_typescript/benches/compat.rs index b3df1af10146..aa296d72fabb 100644 --- a/crates/swc_ecma_transforms_typescript/benches/compat.rs +++ b/crates/swc_ecma_transforms_typescript/benches/compat.rs @@ -1,10 +1,10 @@ use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; -use swc_common::{chain, comments::SingleThreadedComments, sync::Lrc, FileName, Mark, SourceMap}; -use swc_ecma_ast::{Module, Program}; +use swc_common::{comments::SingleThreadedComments, sync::Lrc, FileName, Mark, SourceMap}; +use swc_ecma_ast::{Module, Pass, Program}; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax}; use swc_ecma_transforms_base::{helpers, resolver}; use swc_ecma_transforms_typescript::strip; -use swc_ecma_visit::{Fold, FoldWith}; +use swc_ecma_visit::{fold_pass, Fold}; static SOURCE: &str = include_str!("assets/AjaxObservable.ts"); @@ -27,23 +27,23 @@ fn module(cm: Lrc) -> Program { fn run(b: &mut Bencher, tr: impl Fn(Mark) -> V) where - V: Fold, + V: Pass, { let _ = ::testing::run_test(false, |cm, _| { let module = module(cm); let unresolved_mark = Mark::fresh(Mark::root()); let top_level_mark = Mark::fresh(Mark::root()); let module = module - .fold_with(&mut resolver(unresolved_mark, top_level_mark, true)) - .fold_with(&mut strip(unresolved_mark, top_level_mark)); + .apply(resolver(unresolved_mark, top_level_mark, true)) + .apply(strip(unresolved_mark, top_level_mark)); b.iter(|| { let module = module.clone(); helpers::HELPERS.set(&Default::default(), || { - let mut tr = tr(unresolved_mark); + let tr = tr(unresolved_mark); - black_box(module.fold_with(&mut tr)); + black_box(module.apply(tr)); }); }); @@ -68,7 +68,7 @@ fn base(b: &mut Bencher) { m } } - run(b, |_| Noop); + run(b, |_| fold_pass(Noop)); } fn common_typescript(b: &mut Bencher) { @@ -77,14 +77,14 @@ fn common_typescript(b: &mut Bencher) { let unresolved_mark = Mark::fresh(Mark::root()); let top_level_mark = Mark::fresh(Mark::root()); let module = module - .fold_with(&mut resolver(unresolved_mark, top_level_mark, true)) - .fold_with(&mut strip(unresolved_mark, top_level_mark)); + .apply(resolver(unresolved_mark, top_level_mark, true)) + .apply(strip(unresolved_mark, top_level_mark)); b.iter(|| { let module = module.clone(); helpers::HELPERS.set(&Default::default(), || { - black_box(module.fold_with(&mut strip(unresolved_mark, top_level_mark))); + black_box(module.apply(strip(unresolved_mark, top_level_mark))); }); }); @@ -297,7 +297,7 @@ fn es3(b: &mut Bencher) { fn full_es2016(b: &mut Bencher) { run(b, |unresolved_mark| { - chain!( + ( swc_ecma_transforms_compat::es2022(Default::default(), unresolved_mark), swc_ecma_transforms_compat::es2019(), swc_ecma_transforms_compat::es2018(Default::default()), @@ -309,7 +309,7 @@ fn full_es2016(b: &mut Bencher) { fn full_es2017(b: &mut Bencher) { run(b, |unresolved_mark| { - chain!( + ( swc_ecma_transforms_compat::es2022(Default::default(), unresolved_mark), swc_ecma_transforms_compat::es2019(), swc_ecma_transforms_compat::es2018(Default::default()), @@ -320,7 +320,7 @@ fn full_es2017(b: &mut Bencher) { fn full_es2018(b: &mut Bencher) { run(b, |unresolved_mark| { - chain!( + ( swc_ecma_transforms_compat::es2022(Default::default(), unresolved_mark), swc_ecma_transforms_compat::es2019(), swc_ecma_transforms_compat::es2018(Default::default()), diff --git a/crates/swc_ecma_transforms_typescript/examples/ts_to_js.rs b/crates/swc_ecma_transforms_typescript/examples/ts_to_js.rs index 48585b609bd2..793b35ae66b6 100644 --- a/crates/swc_ecma_transforms_typescript/examples/ts_to_js.rs +++ b/crates/swc_ecma_transforms_typescript/examples/ts_to_js.rs @@ -14,7 +14,6 @@ use swc_ecma_codegen::to_code_default; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsSyntax}; use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver}; use swc_ecma_transforms_typescript::strip; -use swc_ecma_visit::FoldWith; fn main() { let cm: Lrc = Default::default(); @@ -65,16 +64,16 @@ fn main() { // as it might produce runtime declarations. // Conduct identifier scope analysis - let module = module.fold_with(&mut resolver(unresolved_mark, top_level_mark, true)); + let module = module.apply(resolver(unresolved_mark, top_level_mark, true)); // Remove typescript types - let module = module.fold_with(&mut strip(unresolved_mark, top_level_mark)); + let module = module.apply(strip(unresolved_mark, top_level_mark)); // Fix up any identifiers with the same name, but different contexts - let module = module.fold_with(&mut hygiene()); + let module = module.apply(hygiene()); // Ensure that we have enough parenthesis. - let program = module.fold_with(&mut fixer(Some(&comments))); + let program = module.apply(fixer(Some(&comments))); println!("{}", to_code_default(cm, Some(&comments), &program)); }) diff --git a/crates/swc_ecma_transforms_typescript/src/transform.rs b/crates/swc_ecma_transforms_typescript/src/transform.rs index dde85e956b4b..6a13f5c1b796 100644 --- a/crates/swc_ecma_transforms_typescript/src/transform.rs +++ b/crates/swc_ecma_transforms_typescript/src/transform.rs @@ -15,7 +15,7 @@ use swc_ecma_utils::{ ExprFactory, QueryRef, RefRewriter, StmtLikeInjector, }; use swc_ecma_visit::{ - as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith, + noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith, }; use crate::{ @@ -83,8 +83,8 @@ pub fn transform( ts_enum_is_mutable: bool, verbatim_module_syntax: bool, native_class_properties: bool, -) -> impl Fold + VisitMut { - as_folder(Transform { +) -> impl Pass { + visit_mut_pass(Transform { unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark), import_export_assign_config, diff --git a/crates/swc_ecma_transforms_typescript/src/typescript.rs b/crates/swc_ecma_transforms_typescript/src/typescript.rs index f60fde8fb8e2..c6005a22caca 100644 --- a/crates/swc_ecma_transforms_typescript/src/typescript.rs +++ b/crates/swc_ecma_transforms_typescript/src/typescript.rs @@ -6,19 +6,15 @@ use swc_common::{ }; use swc_ecma_ast::*; use swc_ecma_transforms_react::{parse_expr_for_jsx, JsxDirectives}; -use swc_ecma_visit::{as_folder, Fold, VisitMut, VisitMutWith}; +use swc_ecma_visit::{visit_mut_pass, VisitMut, VisitMutWith}; pub use crate::config::*; use crate::{strip_import_export::StripImportExport, strip_type::StripType, transform::transform}; -pub fn typescript( - config: Config, - unresolved_mark: Mark, - top_level_mark: Mark, -) -> impl Fold + VisitMut { +pub fn typescript(config: Config, unresolved_mark: Mark, top_level_mark: Mark) -> impl Pass { debug_assert_ne!(unresolved_mark, top_level_mark); - as_folder(TypeScript { + visit_mut_pass(TypeScript { config, unresolved_mark, top_level_mark, @@ -26,7 +22,7 @@ pub fn typescript( }) } -pub fn strip(unresolved_mark: Mark, top_level_mark: Mark) -> impl Fold + VisitMut { +pub fn strip(unresolved_mark: Mark, top_level_mark: Mark) -> impl Pass { typescript(Config::default(), unresolved_mark, top_level_mark) } @@ -52,7 +48,7 @@ impl VisitMut for TypeScript { n.visit_mut_with(&mut StripType::default()); - n.visit_mut_with(&mut transform( + n.mutate(transform( self.unresolved_mark, self.top_level_mark, self.config.import_export_assign_config, @@ -147,11 +143,11 @@ pub fn tsx( comments: C, unresolved_mark: Mark, top_level_mark: Mark, -) -> impl Fold + VisitMut +) -> impl Pass where C: Comments, { - as_folder(TypeScriptReact { + visit_mut_pass(TypeScriptReact { config, tsx_config, id_usage: Default::default(), diff --git a/crates/swc_ecma_transforms_typescript/tests/strip.rs b/crates/swc_ecma_transforms_typescript/tests/strip.rs index b0686311fbda..1fbb2d598825 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; -use swc_common::{chain, comments::NoopComments, pass::Optional, Mark}; +use swc_common::{comments::NoopComments, pass::Optional, Mark}; +use swc_ecma_ast::Pass; use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::{ @@ -16,9 +17,8 @@ use swc_ecma_transforms_testing::{test, test_exec, test_fixture, Tester}; use swc_ecma_transforms_typescript::{ tsx, typescript, ImportsNotUsedAsValues, TsImportExportAssignConfig, TsxConfig, }; -use swc_ecma_visit::Fold; -fn tr(t: &mut Tester) -> impl Fold { +fn tr(t: &mut Tester) -> impl Pass { tr_config(t, None, None, false) } @@ -27,7 +27,7 @@ fn tr_config( config: Option, decorators_config: Option, use_define_for_class_fields: bool, -) -> impl Fold { +) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); let has_decorators = decorators_config.is_some(); @@ -36,7 +36,7 @@ fn tr_config( ..Default::default() }); - chain!( + ( Optional::new( decorators(decorators_config.unwrap_or_default()), has_decorators, @@ -48,17 +48,17 @@ fn tr_config( None::, Default::default(), top_level_mark, - unresolved_mark + unresolved_mark, ), Optional::new(class_fields_use_set(true), !use_define_for_class_fields), ) } -fn tsxr(t: &Tester) -> impl Fold { +fn tsxr(t: &Tester) -> impl Pass { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), tsx( t.cm.clone(), @@ -77,17 +77,17 @@ fn tsxr(t: &Tester) -> impl Fold { Some(t.comments.clone()), swc_ecma_transforms_react::Options::default(), top_level_mark, - unresolved_mark + unresolved_mark, ), ) } -fn properties(_: &Tester, loose: bool) -> impl Fold { +fn properties(_: &Tester, loose: bool) -> impl Pass { let static_blocks_mark = Mark::new(); let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), static_blocks(static_blocks_mark), class_properties( @@ -96,8 +96,8 @@ fn properties(_: &Tester, loose: bool) -> impl Fold { set_public_fields: loose, ..Default::default() }, - unresolved_mark - ) + unresolved_mark, + ), ) } @@ -108,7 +108,7 @@ macro_rules! to { decorators: true, ..Default::default() }), - |t| chain!(tr(t), properties(t, true)), + |t| (tr(t), properties(t, true)), $name, $from ); @@ -140,14 +140,14 @@ test!( |t| { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, true), tr(t), parameters( parameters::Config { - ignore_function_length: true + ignore_function_length: true, }, - unresolved_mark + unresolved_mark, ), destructuring(destructuring::Config { loose: false }), block_scoping(unresolved_mark), @@ -558,7 +558,7 @@ test!( test!( ::swc_ecma_parser::Syntax::Typescript(Default::default()), - |t| chain!(tr(t), properties(t, true)), + |t| (tr(t), properties(t, true)), issue_930_static, "class A { static b = 'foo'; @@ -569,7 +569,7 @@ test!( test!( ::swc_ecma_parser::Syntax::Typescript(Default::default()), - |t| chain!(tr(t), properties(t, true)), + |t| (tr(t), properties(t, true)), typescript_001, "class A { foo = new Subject() @@ -612,7 +612,7 @@ test!( decorators: true, ..Default::default() }), - |t| chain!( + |t| ( decorators(decorators::Config { legacy: true, ..Default::default() @@ -647,7 +647,7 @@ test_exec!( decorators: true, ..Default::default() }), - |t| chain!( + |t| ( decorators(decorators::Config { legacy: true, ..Default::default() @@ -1858,8 +1858,7 @@ test!( no_empty_export: true, ..Default::default() }; - chain!( - Optional::new(decorators(Default::default()), false,), + ( resolver(unresolved_mark, top_level_mark, true), typescript(config, unresolved_mark, top_level_mark), async_to_generator(Default::default(), unresolved_mark), @@ -1888,11 +1887,11 @@ test!( no_empty_export: true, ..Default::default() }; - chain!( - Optional::new(decorators(Default::default()), false,), + ( + Optional::new(decorators(Default::default()), false), resolver(unresolved_mark, top_level_mark, true), typescript(config, unresolved_mark, top_level_mark), - optional_chaining(Default::default(), unresolved_mark) + optional_chaining(Default::default(), unresolved_mark), ) }, issue_1149_1, @@ -1903,7 +1902,7 @@ test!( test!( Syntax::Typescript(Default::default()), - |t| chain!(tr(t), nullish_coalescing(Default::default())), + |t| (tr(t), nullish_coalescing(Default::default())), issue_1123_1, r#" interface SuperSubmission { @@ -2661,7 +2660,7 @@ fn exec(input: PathBuf) { tsx: input.to_string_lossy().ends_with(".tsx"), ..Default::default() }), - &|t| chain!(tr(t), properties(t, true)), + &|t| (tr(t), properties(t, true)), &input, &output, Default::default(), @@ -2818,7 +2817,7 @@ test!( let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - chain!( + ( resolver(unresolved_mark, top_level_mark, false), tsx( t.cm.clone(), @@ -2830,7 +2829,7 @@ test!( t.comments.clone(), unresolved_mark, top_level_mark, - ) + ), ) }, ts_jsx_bad_pragma, diff --git a/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs b/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs index db01f04c1e40..3ece0ce609dd 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs @@ -6,7 +6,6 @@ use swc_ecma_codegen::to_code_default; use swc_ecma_parser::{lexer::Lexer, EsSyntax, Parser, Syntax, TsSyntax}; use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver}; use swc_ecma_transforms_typescript::typescript; -use swc_ecma_visit::FoldWith; #[testing::fixture("../swc_ecma_parser/tests/tsc/**/*.ts")] #[testing::fixture("../swc_ecma_parser/tests/tsc/**/*.tsx")] @@ -101,8 +100,8 @@ fn identity(entry: PathBuf) { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); Program::Module(p) - .fold_with(&mut resolver(unresolved_mark, top_level_mark, true)) - .fold_with(&mut typescript( + .apply(resolver(unresolved_mark, top_level_mark, true)) + .apply(typescript( typescript::Config { no_empty_export: true, ..Default::default() @@ -110,8 +109,8 @@ fn identity(entry: PathBuf) { unresolved_mark, top_level_mark, )) - .fold_with(&mut hygiene()) - .fold_with(&mut fixer(None)) + .apply(hygiene()) + .apply(fixer(None)) }) .map_err(|e| { eprintln!("failed to parse as typescript module"); diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index 63e77865d300..d6d375fd892f 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -2466,6 +2466,13 @@ where } pub struct DropSpan; + +impl Pass for DropSpan { + fn process(&mut self, program: &mut Program) { + program.visit_mut_with(self); + } +} + impl VisitMut for DropSpan { fn visit_mut_span(&mut self, span: &mut Span) { *span = DUMMY_SP; diff --git a/crates/swc_ecma_visit/src/lib.rs b/crates/swc_ecma_visit/src/lib.rs index 3f3c7a406a2d..87d9d463dea7 100644 --- a/crates/swc_ecma_visit/src/lib.rs +++ b/crates/swc_ecma_visit/src/lib.rs @@ -8,77 +8,239 @@ pub extern crate swc_ecma_ast; use std::{borrow::Cow, fmt::Debug}; -use swc_common::{pass::CompilerPass, Span, DUMMY_SP}; +use swc_common::{pass::CompilerPass, util::take::Take, Span, DUMMY_SP}; use swc_ecma_ast::*; -use swc_visit::{AndThen, Repeat, Repeated}; +use swc_visit::{Repeat, Repeated}; pub use crate::generated::*; mod generated; -impl Fold for AndThen +pub fn fold_pass(pass: V) -> FoldPass where - A: Fold, + V: Fold, +{ + FoldPass { pass } +} + +pub struct FoldPass { + pass: V, +} - B: Fold, +impl Pass for FoldPass +where + V: Fold, { #[inline(always)] - fn fold_program(&mut self, n: Program) -> Program { - let n = self.first.fold_program(n); - self.second.fold_program(n) + fn process(&mut self, program: &mut Program) { + program.map_with_mut(|p| p.fold_with(&mut self.pass)); + } +} + +impl Fold for FoldPass +where + V: Fold, +{ + #[inline(always)] + fn fold_program(&mut self, node: Program) -> Program { + self.pass.fold_program(node) } #[inline(always)] - fn fold_module(&mut self, n: Module) -> Module { - let n = self.first.fold_module(n); - self.second.fold_module(n) + fn fold_module(&mut self, node: Module) -> Module { + self.pass.fold_module(node) } #[inline(always)] - fn fold_script(&mut self, n: Script) -> Script { - let n = self.first.fold_script(n); - self.second.fold_script(n) + fn fold_script(&mut self, node: Script) -> Script { + self.pass.fold_script(node) + } + + #[inline(always)] + fn fold_stmt(&mut self, node: Stmt) -> Stmt { + self.pass.fold_stmt(node) + } + + #[inline(always)] + fn fold_module_item(&mut self, item: ModuleItem) -> ModuleItem { + self.pass.fold_module_item(item) + } + + #[inline(always)] + fn fold_expr(&mut self, expr: Expr) -> Expr { + self.pass.fold_expr(expr) + } + + #[inline(always)] + fn fold_pat(&mut self, pat: Pat) -> Pat { + self.pass.fold_pat(pat) + } + + #[inline(always)] + fn fold_assign_target(&mut self, target: AssignTarget) -> AssignTarget { + self.pass.fold_assign_target(target) + } + + #[inline(always)] + fn fold_ident(&mut self, ident: Ident) -> Ident { + self.pass.fold_ident(ident) + } +} + +impl Repeated for FoldPass +where + V: Fold + Repeated, +{ + fn changed(&self) -> bool { + self.pass.changed() + } + + fn reset(&mut self) { + self.pass.reset(); + } +} + +impl CompilerPass for FoldPass +where + V: Fold + CompilerPass, +{ + fn name(&self) -> Cow<'static, str> { + self.pass.name() + } +} + +pub fn visit_mut_pass(pass: V) -> VisitMutPass +where + V: VisitMut, +{ + VisitMutPass { pass } +} + +pub struct VisitMutPass { + pass: V, +} + +impl Pass for VisitMutPass +where + V: VisitMut, +{ + #[inline(always)] + fn process(&mut self, program: &mut Program) { + program.visit_mut_with(&mut self.pass); } } -impl VisitMut for AndThen +impl VisitMut for VisitMutPass where - A: VisitMut, - B: VisitMut, + V: VisitMut, { - fn visit_mut_program(&mut self, n: &mut Program) { - self.first.visit_mut_program(n); - self.second.visit_mut_program(n); + #[inline(always)] + fn visit_mut_program(&mut self, program: &mut Program) { + self.pass.visit_mut_program(program); } - fn visit_mut_module(&mut self, n: &mut Module) { - self.first.visit_mut_module(n); - self.second.visit_mut_module(n) + #[inline(always)] + fn visit_mut_module(&mut self, module: &mut Module) { + self.pass.visit_mut_module(module); } - fn visit_mut_script(&mut self, n: &mut Script) { - self.first.visit_mut_script(n); - self.second.visit_mut_script(n) + #[inline(always)] + fn visit_mut_script(&mut self, script: &mut Script) { + self.pass.visit_mut_script(script); + } + + #[inline(always)] + fn visit_mut_module_item(&mut self, item: &mut ModuleItem) { + self.pass.visit_mut_module_item(item); + } + + #[inline(always)] + fn visit_mut_stmt(&mut self, stmt: &mut Stmt) { + self.pass.visit_mut_stmt(stmt); + } + + #[inline(always)] + fn visit_mut_expr(&mut self, expr: &mut Expr) { + self.pass.visit_mut_expr(expr); + } + + #[inline(always)] + fn visit_mut_pat(&mut self, pat: &mut Pat) { + self.pass.visit_mut_pat(pat); + } + + #[inline(always)] + fn visit_mut_assign_target(&mut self, target: &mut AssignTarget) { + self.pass.visit_mut_assign_target(target); + } + + #[inline(always)] + fn visit_mut_ident(&mut self, ident: &mut Ident) { + self.pass.visit_mut_ident(ident); } } -impl Visit for AndThen +impl Repeated for VisitMutPass where - A: Visit, - B: Visit, + V: VisitMut + Repeated, { - fn visit_program(&mut self, n: &Program) { - self.first.visit_program(n); - self.second.visit_program(n); + fn changed(&self) -> bool { + self.pass.changed() } - fn visit_module(&mut self, n: &Module) { - self.first.visit_module(n); - self.second.visit_module(n); + fn reset(&mut self) { + self.pass.reset(); } +} - fn visit_script(&mut self, n: &Script) { - self.first.visit_script(n); - self.second.visit_script(n); +impl CompilerPass for VisitMutPass +where + V: VisitMut + CompilerPass, +{ + fn name(&self) -> Cow<'static, str> { + self.pass.name() + } +} + +pub fn visit_pass(pass: V) -> VisitPass +where + V: Visit, +{ + VisitPass { pass } +} + +pub struct VisitPass { + pass: V, +} + +impl Pass for VisitPass +where + V: Visit, +{ + #[inline(always)] + fn process(&mut self, program: &mut Program) { + program.visit_with(&mut self.pass); + } +} + +impl Repeated for VisitPass +where + V: Visit + Repeated, +{ + fn changed(&self) -> bool { + self.pass.changed() + } + + fn reset(&mut self) { + self.pass.reset(); + } +} + +impl CompilerPass for VisitPass +where + V: Visit + CompilerPass, +{ + fn name(&self) -> Cow<'static, str> { + self.pass.name() } } @@ -206,128 +368,30 @@ pub trait InjectVars { fn take_vars(&mut self) -> Vec; } -impl InjectVars for Folder +impl InjectVars for FoldPass where - V: VisitMut + InjectVars, + V: Fold + InjectVars, { fn take_vars(&mut self) -> Vec { - self.0.take_vars() + self.pass.take_vars() } } -/// Wrap a [VisitMut] as a [Fold]. -/// -/// The returned folder only handles `fold_script` and `fold_module`, and -/// typescript nodes are ignored. So if your visitor needs to handle typescript -/// or low-level nodes, you should use [as_folder] instead. -#[inline] -pub fn as_folder(v: V) -> Folder -where - V: VisitMut, -{ - Folder(v) -} - -/// Wrap a [VisitMut] as a [Fold] -#[derive(Debug, Clone, Copy)] -pub struct Folder(V); - -impl Repeated for Folder +impl InjectVars for VisitMutPass where - V: Repeated + VisitMut, -{ - fn changed(&self) -> bool { - self.0.changed() - } - - fn reset(&mut self) { - self.0.reset(); - } -} - -impl CompilerPass for Folder -where - V: VisitMut + CompilerPass, + V: VisitMut + InjectVars, { - fn name() -> Cow<'static, str> { - V::name() + fn take_vars(&mut self) -> Vec { + self.pass.take_vars() } } -macro_rules! delegate { - ($name:ident, $T:ty) => { - fn $name(&mut self, n: &mut $T) { - n.visit_mut_with(&mut self.0); - } - }; -} - -/// This only proxies subset of methods. -impl VisitMut for Folder +impl InjectVars for VisitPass where - V: VisitMut, + V: Visit + InjectVars, { - delegate!(visit_mut_ident, Ident); - - delegate!(visit_mut_span, Span); - - delegate!(visit_mut_expr, Expr); - - delegate!(visit_mut_decl, Decl); - - delegate!(visit_mut_stmt, Stmt); - - delegate!(visit_mut_pat, Pat); - - delegate!(visit_mut_ts_type, TsType); - - delegate!(visit_mut_module, Module); - - delegate!(visit_mut_script, Script); - - delegate!(visit_mut_program, Program); -} - -macro_rules! method { - ($name:ident, $T:ty) => { - fn $name(&mut self, mut n: $T) -> $T { - n.visit_mut_with(&mut self.0); - n - } - }; -} - -impl Fold for Folder -where - V: VisitMut, -{ - method!(fold_ident, Ident); - - method!(fold_span, Span); - - method!(fold_expr, Expr); - - method!(fold_decl, Decl); - - method!(fold_stmt, Stmt); - - method!(fold_pat, Pat); - - method!(fold_ts_type, TsType); - - method!(fold_script, Script); - - method!(fold_program, Program); - - #[inline(always)] - fn fold_module(&mut self, mut n: Module) -> Module { - #[cfg(all(debug_assertions, feature = "debug"))] - let _tracing = { - let visitor_name = std::any::type_name::(); - tracing::span!(tracing::Level::INFO, "as_folder", visitor = visitor_name).entered() - }; - n.visit_mut_with(&mut self.0); - n + fn take_vars(&mut self) -> Vec { + self.pass.take_vars() } } @@ -957,3 +1021,162 @@ macro_rules! visit_mut_obj_and_computed { } }; } + +macro_rules! impl_traits_for_tuple { + ( + [$idx:tt, $name:ident], $([$idx_rest:tt, $name_rest:ident]),* + ) => { + impl<$name, $($name_rest),*> VisitMut for ($name, $($name_rest),*) + where + $name: VisitMut, + $($name_rest: VisitMut),* + { + + fn visit_mut_program(&mut self, program: &mut Program) { + self.$idx.visit_mut_program(program); + + $( + self.$idx_rest.visit_mut_program(program); + )* + } + + fn visit_mut_module(&mut self, module: &mut Module) { + self.$idx.visit_mut_module(module); + + $( + self.$idx_rest.visit_mut_module(module); + )* + } + + fn visit_mut_script(&mut self, script: &mut Script) { + self.$idx.visit_mut_script(script); + + $( + self.$idx_rest.visit_mut_script(script); + )* + } + + fn visit_mut_stmt(&mut self, stmt: &mut Stmt) { + self.$idx.visit_mut_stmt(stmt); + + $( + self.$idx_rest.visit_mut_stmt(stmt); + )* + } + + fn visit_mut_expr(&mut self, expr: &mut Expr) { + self.$idx.visit_mut_expr(expr); + + $( + self.$idx_rest.visit_mut_expr(expr); + )* + } + + fn visit_mut_pat(&mut self, pat: &mut Pat) { + self.$idx.visit_mut_pat(pat); + + $( + self.$idx_rest.visit_mut_pat(pat); + )* + } + + fn visit_mut_assign_target(&mut self, target: &mut AssignTarget) { + self.$idx.visit_mut_assign_target(target); + + $( + self.$idx_rest.visit_mut_assign_target(target); + )* + } + + fn visit_mut_ident(&mut self, ident: &mut Ident) { + self.$idx.visit_mut_ident(ident); + + $( + self.$idx_rest.visit_mut_ident(ident); + )* + } + } + }; +} + +impl_traits_for_tuple!([0, A], [1, B]); +impl_traits_for_tuple!([0, A], [1, B], [2, C]); +impl_traits_for_tuple!([0, A], [1, B], [2, C], [3, D]); +impl_traits_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E]); +impl_traits_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F]); +impl_traits_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F], [6, G]); +impl_traits_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H] +); +impl_traits_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I] +); +impl_traits_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J] +); +impl_traits_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K] +); +impl_traits_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L] +); +impl_traits_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L], + [12, M] +); diff --git a/crates/swc_ecma_visit/tests/main.rs b/crates/swc_ecma_visit/tests/main.rs index 0fa7bf3e8e66..4549d5a7dc44 100644 --- a/crates/swc_ecma_visit/tests/main.rs +++ b/crates/swc_ecma_visit/tests/main.rs @@ -1,34 +1,6 @@ -use swc_common::{chain, DUMMY_SP}; +use swc_common::DUMMY_SP; use swc_ecma_ast::*; -use swc_ecma_visit::{NodeRef, Visit, VisitWith}; - -#[test] -fn should_visit_program() { - struct Pass1<'a>(&'a mut usize); - struct Pass2; - - impl Visit for Pass1<'_> { - fn visit_program(&mut self, _program: &Program) { - *self.0 += 1; - } - } - - impl Visit for Pass2 {} - - let n = Program::Module(Module { - span: DUMMY_SP, - body: Vec::new(), - shebang: None, - }); - - let mut counter = 0; - let p1 = Pass1(&mut counter); - let p2 = Pass2; - let mut pass = chain!(p1, p2); - n.visit_with(&mut pass); - - assert_eq!(counter, 1); -} +use swc_ecma_visit::NodeRef; #[test] fn traverse_lookup() { diff --git a/crates/swc_estree_compat/benches/babelify.rs b/crates/swc_estree_compat/benches/babelify.rs index 31d3eab1fec7..aa22f294dcd8 100644 --- a/crates/swc_estree_compat/benches/babelify.rs +++ b/crates/swc_estree_compat/benches/babelify.rs @@ -8,7 +8,6 @@ use swc_common::{ use swc_ecma_ast::{EsVersion, Program}; use swc_ecma_parser::Syntax; use swc_ecma_transforms::{compat::es2020, resolver, typescript}; -use swc_ecma_visit::FoldWith; use swc_estree_compat::babelify::{Babelify, Context}; static SOURCE: &str = include_str!("assets/AjaxObservable.ts"); @@ -53,9 +52,9 @@ fn babelify_only(b: &mut Bencher) { let top_level_mark = Mark::new(); module - .fold_with(&mut resolver(unresolved_mark, top_level_mark, true)) - .fold_with(&mut typescript::strip(unresolved_mark, top_level_mark)) - .fold_with(&mut es2020(Default::default(), unresolved_mark)) + .apply(&mut resolver(unresolved_mark, top_level_mark, true)) + .apply(&mut typescript::strip(unresolved_mark, top_level_mark)) + .apply(&mut es2020(Default::default(), unresolved_mark)) }); b.iter(|| { diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 710595d94ada..d7a7f6dfead7 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -32,7 +32,7 @@ use swc_ecma_transforms_base::{ resolver, }; use swc_ecma_transforms_typescript::typescript; -use swc_ecma_visit::{Visit, VisitMutWith, VisitWith}; +use swc_ecma_visit::{Visit, VisitWith}; #[cfg(feature = "wasm-bindgen")] use wasm_bindgen::prelude::*; @@ -285,19 +285,19 @@ pub fn operate( let top_level_mark = Mark::new(); HELPERS.set(&Helpers::new(false), || { - program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, true)); + program.mutate(&mut resolver(unresolved_mark, top_level_mark, true)); - program.visit_mut_with(&mut typescript::typescript( + program.mutate(&mut typescript::typescript( options.transform.unwrap_or_default(), unresolved_mark, top_level_mark, )); - program.visit_mut_with(&mut inject_helpers(unresolved_mark)); + program.mutate(&mut inject_helpers(unresolved_mark)); - program.visit_mut_with(&mut hygiene()); + program.mutate(&mut hygiene()); - program.visit_mut_with(&mut fixer(Some(&comments))); + program.mutate(&mut fixer(Some(&comments))); }); let mut src = std::vec::Vec::new(); diff --git a/crates/swc_html_minifier/src/lib.rs b/crates/swc_html_minifier/src/lib.rs index faff9cdfbd1a..587a464863aa 100644 --- a/crates/swc_html_minifier/src/lib.rs +++ b/crates/swc_html_minifier/src/lib.rs @@ -1935,10 +1935,8 @@ impl Minifier<'_, C> { ); } - let left_program = swc_ecma_visit::FoldWith::fold_with( - left_program, - &mut swc_ecma_transforms_base::fixer::fixer(Some(&comments)), - ); + let left_program = + left_program.apply(swc_ecma_transforms_base::fixer::fixer(Some(&comments))); let mut buf = Vec::new(); @@ -2036,10 +2034,9 @@ impl Minifier<'_, C> { &mut swc_ecma_transforms_base::resolver(unresolved_mark, top_level_mark, false), ); - let program = swc_ecma_visit::FoldWith::fold_with( - program, - &mut swc_ecma_transforms_base::fixer::paren_remover(Some(&comments)), - ); + let program = program.apply(swc_ecma_transforms_base::fixer::paren_remover(Some( + &comments, + ))); let program = swc_ecma_minifier::optimize( program, @@ -2059,10 +2056,7 @@ impl Minifier<'_, C> { }, ); - let program = swc_ecma_visit::FoldWith::fold_with( - program, - &mut swc_ecma_transforms_base::fixer::fixer(Some(&comments)), - ); + let program = program.apply(swc_ecma_transforms_base::fixer::fixer(Some(&comments))); let mut buf = Vec::new(); diff --git a/crates/swc_node_bundler/src/loaders/swc.rs b/crates/swc_node_bundler/src/loaders/swc.rs index 0de49ef81635..a53fd54c4ace 100644 --- a/crates/swc_node_bundler/src/loaders/swc.rs +++ b/crates/swc_node_bundler/src/loaders/swc.rs @@ -15,7 +15,7 @@ use swc_common::{ sync::Lrc, FileName, Mark, DUMMY_SP, }; -use swc_ecma_ast::{EsVersion, Expr, Lit, Module, Program, Str}; +use swc_ecma_ast::{noop_pass, EsVersion, Expr, Lit, Module, Program, Str}; use swc_ecma_parser::{parse_file_as_module, Syntax}; use swc_ecma_transforms::{ helpers, @@ -23,12 +23,10 @@ use swc_ecma_transforms::{ inline_globals, simplify::{dead_branch_remover, expr_simplifier}, }, - pass::noop, react::jsx, resolver, typescript::typescript, }; -use swc_ecma_visit::{FoldWith, VisitMutWith}; use crate::loaders::json::load_json_as_module; @@ -171,14 +169,14 @@ impl SwcLoader { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); - program.visit_mut_with(&mut typescript( + program.mutate(&mut resolver(unresolved_mark, top_level_mark, false)); + program.mutate(&mut typescript( Default::default(), unresolved_mark, top_level_mark, )); - program.visit_mut_with(&mut jsx( + program.mutate(&mut jsx( self.compiler.cm.clone(), None::, Default::default(), @@ -186,16 +184,17 @@ impl SwcLoader { unresolved_mark, )); - program.visit_mut_with(&mut inline_globals( + program.mutate(&mut inline_globals( self.env_map(), Default::default(), Default::default(), )); - let program = program - .fold_with(&mut expr_simplifier(unresolved_mark, Default::default())); + program.mutate(&mut expr_simplifier(unresolved_mark, Default::default())); - program.fold_with(&mut dead_branch_remover(unresolved_mark)) + program.mutate(&mut dead_branch_remover(unresolved_mark)); + + program }) }) } else { @@ -248,7 +247,7 @@ impl SwcLoader { }, &fm.name, Some(&comments), - |_| noop(), + |_| noop_pass(), )?; tracing::trace!("JsLoader.load: loaded config"); @@ -258,25 +257,21 @@ impl SwcLoader { // Note that we don't apply compat transform at loading phase. let program = if let Some(config) = config { let mut program = config.program; - let mut pass = config.pass; + let pass = config.pass; helpers::HELPERS.set(&helpers, || { HANDLER.set(handler, || { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); - program.visit_mut_with(&mut resolver( - unresolved_mark, - top_level_mark, - false, - )); - program.visit_mut_with(&mut typescript( + program.mutate(&mut resolver(unresolved_mark, top_level_mark, false)); + program.mutate(&mut typescript( Default::default(), unresolved_mark, top_level_mark, )); - program.visit_mut_with(&mut jsx( + program.mutate(&mut jsx( self.compiler.cm.clone(), None::, Default::default(), @@ -284,17 +279,16 @@ impl SwcLoader { unresolved_mark, )); - let program = program.fold_with(&mut inline_globals( + program.mutate(&mut inline_globals( self.env_map(), Default::default(), Default::default(), )); - let program = program - .fold_with(&mut expr_simplifier(unresolved_mark, Default::default())); - let program = program.fold_with(&mut dead_branch_remover(unresolved_mark)); + program.mutate(&mut expr_simplifier(unresolved_mark, Default::default())); + program.mutate(&mut dead_branch_remover(unresolved_mark)); - program.fold_with(&mut pass) + program.apply(pass) }) }) } else { diff --git a/crates/swc_node_bundler/tests/fixture.rs b/crates/swc_node_bundler/tests/fixture.rs index 35d1b7375996..ca48f638cb0b 100644 --- a/crates/swc_node_bundler/tests/fixture.rs +++ b/crates/swc_node_bundler/tests/fixture.rs @@ -12,11 +12,10 @@ use swc_bundler::{BundleKind, Bundler, Config, ModuleRecord}; use swc_common::{errors::HANDLER, FileName, Globals, Span, GLOBALS}; use swc_ecma_ast::{ Bool, EsVersion, Expr, IdentName, KeyValueProp, Lit, MemberExpr, MemberProp, MetaPropExpr, - MetaPropKind, PropName, Str, + MetaPropKind, Program, PropName, Str, }; use swc_ecma_loader::{TargetEnv, NODE_BUILTINS}; use swc_ecma_transforms::fixer; -use swc_ecma_visit::FoldWith; use swc_node_bundler::loaders::swc::SwcLoader; use testing::NormalizedOutput; @@ -86,7 +85,7 @@ fn pass(input_dir: PathBuf) { let comments = compiler.comments().clone(); let code = compiler .print( - &bundled.module.fold_with(&mut fixer(None)), + &Program::Module(bundled.module).apply(&mut fixer(None)), PrintArgs { comments: Some(&comments), codegen_config: swc_ecma_codegen::Config::default() diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs index 9f259f166196..6e291c9d4049 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs @@ -29,7 +29,8 @@ impl VisitMut for ConsoleOutputReplacer { span: DUMMY_SP, value: JsWord::from("changed_via_plugin"), raw: Some(Atom::from("\"changed_via_plugin\"")), - }).into(); + }) + .into(); } } } @@ -58,7 +59,7 @@ impl VisitMut for ConsoleOutputReplacer { /// important steps manually need to be performed like sending transformed /// results back to host. Refer swc_plugin_macro how does it work internally. #[plugin_transform] -pub fn process(program: Program, metadata: TransformPluginProgramMetadata) -> Program { +pub fn process(mut program: Program, metadata: TransformPluginProgramMetadata) -> Program { dbg!(); HANDLER.with(|handler| { handler @@ -133,5 +134,6 @@ pub fn process(program: Program, metadata: TransformPluginProgramMetadata) -> Pr dbg!(); - program.fold_with(&mut as_folder(ConsoleOutputReplacer { metadata })) + program.visit_mut_with(&mut ConsoleOutputReplacer { metadata }); + program } diff --git a/crates/swc_visit/src/lib.rs b/crates/swc_visit/src/lib.rs index b5b18401f803..07d1d7893474 100644 --- a/crates/swc_visit/src/lib.rs +++ b/crates/swc_visit/src/lib.rs @@ -129,39 +129,109 @@ pub trait Repeated { fn reset(&mut self); } -/// A visitor which applies `A` and then `B`. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -pub struct AndThen { - pub first: A, - pub second: B, -} - -/// Chains multiple visitor. -#[macro_export] -macro_rules! chain { - ($a:expr, $b:expr) => {{ - use $crate::AndThen; - - AndThen { - first: $a, - second: $b, +macro_rules! impl_repeated_for_tuple { + ( + [$idx:tt, $name:ident], $([$idx_rest:tt, $name_rest:ident]),* + ) => { + impl<$name, $($name_rest),*> Repeated for ($name, $($name_rest),*) + where + $name: Repeated, + $($name_rest: Repeated),* + { + fn changed(&self) -> bool { + self.$idx.changed() || $(self.$idx_rest.changed() ||)* false + } + + fn reset(&mut self) { + self.$idx.reset(); + $(self.$idx_rest.reset();)* + } } - }}; - - ($a:expr, $b:expr,) => { - chain!($a, $b) }; - - ($a:expr, $b:expr, $($rest:tt)+) => {{ - use $crate::AndThen; - - AndThen{ - first: $a, - second: chain!($b, $($rest)*), - } - }}; } +impl_repeated_for_tuple!([0, A], [1, B]); +impl_repeated_for_tuple!([0, A], [1, B], [2, C]); +impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D]); +impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E]); +impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F]); +impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F], [6, G]); +impl_repeated_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H] +); +impl_repeated_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I] +); +impl_repeated_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J] +); +impl_repeated_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K] +); +impl_repeated_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L] +); +impl_repeated_for_tuple!( + [0, A], + [1, B], + [2, C], + [3, D], + [4, E], + [5, F], + [6, G], + [7, H], + [8, I], + [9, J], + [10, K], + [11, L], + [12, M] +); + /// A visitor which applies `V` again and again if `V` modifies the node. /// /// # Note @@ -199,21 +269,6 @@ where } } -impl Repeated for AndThen -where - A: Repeated, - B: Repeated, -{ - fn changed(&self) -> bool { - self.first.changed() || self.second.changed() - } - - fn reset(&mut self) { - self.first.reset(); - self.second.reset(); - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct AstKindPath where diff --git a/packages/core/e2e/fixtures/plugin_transform_schema_v1/src/lib.rs b/packages/core/e2e/fixtures/plugin_transform_schema_v1/src/lib.rs index 31127e438de5..5328dcb554c7 100644 --- a/packages/core/e2e/fixtures/plugin_transform_schema_v1/src/lib.rs +++ b/packages/core/e2e/fixtures/plugin_transform_schema_v1/src/lib.rs @@ -31,5 +31,5 @@ pub fn process(program: Program, _metadata: TransformPluginProgramMetadata) -> P Program::Module(_module) => {} } - program.fold_with(&mut as_folder(ConsoleOutputReplacer)) + program.fold_with(&mut visit_mut_pass(ConsoleOutputReplacer)) } diff --git a/packages/core/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs b/packages/core/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs index 1e0a9df53d94..153ce4d69253 100644 --- a/packages/core/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs +++ b/packages/core/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs @@ -34,5 +34,5 @@ pub fn process(program: Program, _metadata: TransformPluginProgramMetadata) -> P Program::Script(..) => {} Program::Module(..) => {} } - program.fold_with(&mut as_folder(ConsoleOutputReplacer)) + program.fold_with(&mut visit_mut_pass(ConsoleOutputReplacer)) }