From f344caa4fa3e8269a4b3a02b3037a99dfda0520b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 13 Feb 2020 21:55:54 +0900 Subject: [PATCH] Reduce binary size (#663) This pr add two helper macros to prevent folding type unnecessarily. This reduces the binary size by 3MB on linux. --- .../transforms/src/compat/es2015/arrow.rs | 2 + .../src/compat/es2015/block_scoped_fn.rs | 2 + .../src/compat/es2015/block_scoping.rs | 2 + .../transforms/src/compat/es2015/classes.rs | 2 + .../src/compat/es2015/classes/constructor.rs | 2 + .../src/compat/es2015/classes/super_field.rs | 4 + .../src/compat/es2015/computed_props.rs | 2 + .../src/compat/es2015/destructuring.rs | 2 + .../src/compat/es2015/duplicate_keys.rs | 4 + .../transforms/src/compat/es2015/for_of.rs | 2 + .../src/compat/es2015/function_name.rs | 4 + .../src/compat/es2015/instanceof.rs | 2 + .../src/compat/es2015/parameters.rs | 2 + .../src/compat/es2015/regenerator/hoist.rs | 2 + .../src/compat/es2015/regenerator/mod.rs | 2 + .../src/compat/es2015/shorthand_property.rs | 2 + .../transforms/src/compat/es2015/spread.rs | 4 + .../src/compat/es2015/sticky_regex.rs | 2 + .../src/compat/es2015/template_literal.rs | 2 + .../src/compat/es2015/typeof_symbol.rs | 2 + .../src/compat/es2016/exponentation.rs | 4 + .../src/compat/es2017/async_to_generator.rs | 5 + .../src/compat/es2018/object_rest_spread.rs | 6 + .../compat/es2018/optional_catch_binding.rs | 2 + .../src/compat/es3/member_expr_lits.rs | 2 + .../transforms/src/compat/es3/prop_lits.rs | 2 + .../src/compat/es3/reserved_word.rs | 2 + ecmascript/transforms/src/const_modules.rs | 2 + ecmascript/transforms/src/fixer.rs | 2 + ecmascript/transforms/src/helpers.rs | 6 + ecmascript/transforms/src/hygiene.rs | 2 + ecmascript/transforms/src/hygiene/ops.rs | 2 + ecmascript/transforms/src/macros.rs | 150 ++++++++++++++++++ ecmascript/transforms/src/modules/amd.rs | 2 + .../transforms/src/modules/common_js.rs | 2 + .../transforms/src/modules/import_analysis.rs | 3 + ecmascript/transforms/src/modules/umd.rs | 2 + .../src/optimization/inline_globals.rs | 2 + .../transforms/src/optimization/json_parse.rs | 2 + .../src/optimization/simplify/branch/mod.rs | 2 + .../src/optimization/simplify/dce/mod.rs | 2 + .../optimization/simplify/dce/side_effect.rs | 2 + .../src/optimization/simplify/expr/mod.rs | 2 + .../src/optimization/simplify/inlining/mod.rs | 2 + .../class_properties/class_name_tdz.rs | 2 + .../class_properties/private_field.rs | 2 + .../class_properties/this_in_static.rs | 2 + .../proposals/class_properties/used_name.rs | 4 + .../transforms/src/proposals/decorators.rs | 2 + .../src/proposals/decorators/legacy.rs | 2 + .../src/proposals/decorators/usage.rs | 2 + ecmascript/transforms/src/proposals/export.rs | 2 + .../transforms/src/proposals/opt_chaining.rs | 2 + .../transforms/src/react/display_name.rs | 2 + ecmascript/transforms/src/react/jsx.rs | 2 + ecmascript/transforms/src/react/jsx_self.rs | 2 + ecmascript/transforms/src/react/jsx_src.rs | 2 + ecmascript/transforms/src/resolver.rs | 2 + 58 files changed, 288 insertions(+) diff --git a/ecmascript/transforms/src/compat/es2015/arrow.rs b/ecmascript/transforms/src/compat/es2015/arrow.rs index 0614db16298..3abc159f4a6 100644 --- a/ecmascript/transforms/src/compat/es2015/arrow.rs +++ b/ecmascript/transforms/src/compat/es2015/arrow.rs @@ -60,6 +60,8 @@ pub fn arrow() -> impl Pass { struct Arrow; +noop_fold_type!(Arrow); + impl Fold for Arrow { fn fold(&mut self, e: Expr) -> Expr { // fast path diff --git a/ecmascript/transforms/src/compat/es2015/block_scoped_fn.rs b/ecmascript/transforms/src/compat/es2015/block_scoped_fn.rs index 3323d2dc92a..45d0bec6e39 100644 --- a/ecmascript/transforms/src/compat/es2015/block_scoped_fn.rs +++ b/ecmascript/transforms/src/compat/es2015/block_scoped_fn.rs @@ -5,6 +5,8 @@ use swc_ecma_ast::*; #[derive(Clone, Copy)] pub struct BlockScopedFns; +noop_fold_type!(BlockScopedFns); + impl Fold> for BlockScopedFns { fn fold(&mut self, items: Vec) -> Vec { let mut stmts = Vec::with_capacity(items.len()); diff --git a/ecmascript/transforms/src/compat/es2015/block_scoping.rs b/ecmascript/transforms/src/compat/es2015/block_scoping.rs index 5eb69d6adc9..f7db1ed5de8 100644 --- a/ecmascript/transforms/src/compat/es2015/block_scoping.rs +++ b/ecmascript/transforms/src/compat/es2015/block_scoping.rs @@ -45,6 +45,8 @@ struct BlockScoping { vars: Vec, } +noop_fold_type!(BlockScoping); + impl BlockScoping { /// This methods remove [ScopeKind::Loop] and [ScopeKind::Fn], but not /// [ScopeKind::ForLetLoop] diff --git a/ecmascript/transforms/src/compat/es2015/classes.rs b/ecmascript/transforms/src/compat/es2015/classes.rs index e14fde96082..32003707d9f 100644 --- a/ecmascript/transforms/src/compat/es2015/classes.rs +++ b/ecmascript/transforms/src/compat/es2015/classes.rs @@ -61,6 +61,8 @@ pub struct Classes { in_strict: bool, } +noop_fold_type!(Classes); + struct Data { key_prop: Box, method: Option>, diff --git a/ecmascript/transforms/src/compat/es2015/classes/constructor.rs b/ecmascript/transforms/src/compat/es2015/classes/constructor.rs index 71001f09ed6..60b0fca6a57 100644 --- a/ecmascript/transforms/src/compat/es2015/classes/constructor.rs +++ b/ecmascript/transforms/src/compat/es2015/classes/constructor.rs @@ -12,6 +12,8 @@ pub(super) struct SuperCallFinder { in_complex: bool, } +noop_visit_type!(SuperCallFinder); + impl SuperCallFinder { /// /// - `None`: if no `super()` is found or super() is last call diff --git a/ecmascript/transforms/src/compat/es2015/classes/super_field.rs b/ecmascript/transforms/src/compat/es2015/classes/super_field.rs index c81d2a3c82e..62474aefe8b 100644 --- a/ecmascript/transforms/src/compat/es2015/classes/super_field.rs +++ b/ecmascript/transforms/src/compat/es2015/classes/super_field.rs @@ -39,6 +39,8 @@ pub(crate) struct SuperFieldAccessFolder<'a> { pub this_alias_mark: Option, } +noop_fold_type!(SuperFieldAccessFolder<'_>); + struct SuperCalleeFolder<'a> { vars: &'a mut Vec, class_name: &'a Ident, @@ -56,6 +58,8 @@ struct SuperCalleeFolder<'a> { this_alias_mark: Option, } +noop_fold_type!(SuperCalleeFolder<'_>); + macro_rules! mark_nested { ($T:tt) => { impl<'a> Fold<$T> for SuperFieldAccessFolder<'a> { diff --git a/ecmascript/transforms/src/compat/es2015/computed_props.rs b/ecmascript/transforms/src/compat/es2015/computed_props.rs index 9d1fc2dbc87..c32807321e6 100644 --- a/ecmascript/transforms/src/compat/es2015/computed_props.rs +++ b/ecmascript/transforms/src/compat/es2015/computed_props.rs @@ -42,6 +42,8 @@ pub fn computed_properties() -> impl Pass { struct ComputedProps; +noop_fold_type!(ComputedProps); + #[derive(Default)] struct ObjectLitFolder { vars: Vec, diff --git a/ecmascript/transforms/src/compat/es2015/destructuring.rs b/ecmascript/transforms/src/compat/es2015/destructuring.rs index b56e3b764d2..89b9417ff2c 100644 --- a/ecmascript/transforms/src/compat/es2015/destructuring.rs +++ b/ecmascript/transforms/src/compat/es2015/destructuring.rs @@ -39,6 +39,8 @@ struct Destructuring { c: Config, } +noop_fold_type!(Destructuring); + #[derive(Debug, Default, Clone, Copy, Deserialize)] pub struct Config { #[serde(default)] diff --git a/ecmascript/transforms/src/compat/es2015/duplicate_keys.rs b/ecmascript/transforms/src/compat/es2015/duplicate_keys.rs index 9cb912628c5..0d8a9c988e4 100644 --- a/ecmascript/transforms/src/compat/es2015/duplicate_keys.rs +++ b/ecmascript/transforms/src/compat/es2015/duplicate_keys.rs @@ -10,6 +10,8 @@ pub fn duplicate_keys() -> impl Pass { struct DuplicateKeys; +noop_fold_type!(DuplicateKeys); + impl Fold for DuplicateKeys { fn fold(&mut self, expr: Expr) -> Expr { let expr = expr.fold_children(self); @@ -35,6 +37,8 @@ struct PropFolder { setter_props: HashSet, } +noop_fold_type!(PropFolder); + impl Fold for PropFolder { fn fold(&mut self, node: Expr) -> Expr { node diff --git a/ecmascript/transforms/src/compat/es2015/for_of.rs b/ecmascript/transforms/src/compat/es2015/for_of.rs index 4b59f7b9d81..52d58e9ab83 100644 --- a/ecmascript/transforms/src/compat/es2015/for_of.rs +++ b/ecmascript/transforms/src/compat/es2015/for_of.rs @@ -55,6 +55,8 @@ struct ForOf { c: Config, } +noop_fold_type!(ForOf); + /// Real folder. struct Actual { c: Config, diff --git a/ecmascript/transforms/src/compat/es2015/function_name.rs b/ecmascript/transforms/src/compat/es2015/function_name.rs index f9ae346291d..5a6aba0fe81 100644 --- a/ecmascript/transforms/src/compat/es2015/function_name.rs +++ b/ecmascript/transforms/src/compat/es2015/function_name.rs @@ -26,10 +26,14 @@ pub fn function_name() -> impl Pass { #[derive(Clone, Copy)] struct FnName; +noop_fold_type!(FnName); + struct Renamer { name: Option, } +noop_fold_type!(Renamer); + /// This function makes a new private identifier if required. fn prepare(i: Ident, force: bool) -> Ident { if i.is_reserved_for_es3() || i.sym == *"await" || i.sym == *"eval" { diff --git a/ecmascript/transforms/src/compat/es2015/instanceof.rs b/ecmascript/transforms/src/compat/es2015/instanceof.rs index 41f6584ff88..f78bce01c69 100644 --- a/ecmascript/transforms/src/compat/es2015/instanceof.rs +++ b/ecmascript/transforms/src/compat/es2015/instanceof.rs @@ -30,6 +30,8 @@ use swc_ecma_ast::*; #[derive(Clone)] pub struct InstanceOf; +noop_fold_type!(InstanceOf); + impl Fold for InstanceOf { fn fold(&mut self, expr: Expr) -> Expr { fn should_work(node: &Expr) -> bool { diff --git a/ecmascript/transforms/src/compat/es2015/parameters.rs b/ecmascript/transforms/src/compat/es2015/parameters.rs index d8a03f7cf71..cb034fc65d8 100644 --- a/ecmascript/transforms/src/compat/es2015/parameters.rs +++ b/ecmascript/transforms/src/compat/es2015/parameters.rs @@ -11,6 +11,8 @@ pub fn parameters() -> Params { pub struct Params; // prevent_recurse!(Params, Pat); +noop_fold_type!(Params); + impl Params { fn fold_fn_like(&mut self, ps: Vec, body: BlockStmt) -> (Vec, BlockStmt) { let body = validate!(body); diff --git a/ecmascript/transforms/src/compat/es2015/regenerator/hoist.rs b/ecmascript/transforms/src/compat/es2015/regenerator/hoist.rs index 09634bc7116..9100358786d 100644 --- a/ecmascript/transforms/src/compat/es2015/regenerator/hoist.rs +++ b/ecmascript/transforms/src/compat/es2015/regenerator/hoist.rs @@ -25,6 +25,8 @@ pub(super) struct Hoister { pub arguments: Option, } +noop_fold_type!(Hoister); + impl Hoister { fn var_decl_to_expr(&mut self, var: VarDecl) -> Expr { let var = var.fold_children(self); diff --git a/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs b/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs index 59bf4a211a7..e3d51d1b379 100644 --- a/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs +++ b/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs @@ -26,6 +26,8 @@ struct Regenerator { top_level_vars: Vec, } +noop_fold_type!(Regenerator); + fn rt(rt: Ident) -> Stmt { Stmt::Decl(Decl::Var(VarDecl { span: DUMMY_SP, diff --git a/ecmascript/transforms/src/compat/es2015/shorthand_property.rs b/ecmascript/transforms/src/compat/es2015/shorthand_property.rs index 2f64e034e3c..df01bc8f79a 100644 --- a/ecmascript/transforms/src/compat/es2015/shorthand_property.rs +++ b/ecmascript/transforms/src/compat/es2015/shorthand_property.rs @@ -38,6 +38,8 @@ use swc_ecma_ast::*; #[derive(Default, Clone, Copy)] pub struct Shorthand; +noop_fold_type!(Shorthand); + impl Fold for Shorthand { fn fold(&mut self, prop: Prop) -> Prop { let prop = prop.fold_children(self); diff --git a/ecmascript/transforms/src/compat/es2015/spread.rs b/ecmascript/transforms/src/compat/es2015/spread.rs index 8b776cfa057..9ec9cdcb099 100644 --- a/ecmascript/transforms/src/compat/es2015/spread.rs +++ b/ecmascript/transforms/src/compat/es2015/spread.rs @@ -24,12 +24,16 @@ struct Spread { c: Config, } +noop_fold_type!(Spread); + #[derive(Default)] struct ActualFolder { c: Config, vars: Vec, } +noop_fold_type!(ActualFolder); + impl Fold> for Spread where T: StmtLike + FoldWith + FoldWith, diff --git a/ecmascript/transforms/src/compat/es2015/sticky_regex.rs b/ecmascript/transforms/src/compat/es2015/sticky_regex.rs index 0ca8817a1de..69b1c32ccef 100644 --- a/ecmascript/transforms/src/compat/es2015/sticky_regex.rs +++ b/ecmascript/transforms/src/compat/es2015/sticky_regex.rs @@ -20,6 +20,8 @@ use swc_ecma_ast::*; #[derive(Clone, Copy)] pub struct StickyRegex; +noop_fold_type!(StickyRegex); + impl Fold for StickyRegex { fn fold(&mut self, e: Expr) -> Expr { let e = e.fold_children(self); diff --git a/ecmascript/transforms/src/compat/es2015/template_literal.rs b/ecmascript/transforms/src/compat/es2015/template_literal.rs index 19587cb7d9a..e7edb9e6541 100644 --- a/ecmascript/transforms/src/compat/es2015/template_literal.rs +++ b/ecmascript/transforms/src/compat/es2015/template_literal.rs @@ -9,6 +9,8 @@ pub struct TemplateLiteral { added: Vec, } +noop_fold_type!(TemplateLiteral); + impl Fold for TemplateLiteral { fn fold(&mut self, m: Module) -> Module { let mut body = m.body.fold_children(self); diff --git a/ecmascript/transforms/src/compat/es2015/typeof_symbol.rs b/ecmascript/transforms/src/compat/es2015/typeof_symbol.rs index e154bc113f4..bd0b37298b2 100644 --- a/ecmascript/transforms/src/compat/es2015/typeof_symbol.rs +++ b/ecmascript/transforms/src/compat/es2015/typeof_symbol.rs @@ -23,6 +23,8 @@ use swc_ecma_ast::*; #[derive(Clone)] pub struct TypeOfSymbol; +noop_fold_type!(TypeOfSymbol); + impl Fold for TypeOfSymbol { fn fold(&mut self, expr: Expr) -> Expr { // fast path diff --git a/ecmascript/transforms/src/compat/es2016/exponentation.rs b/ecmascript/transforms/src/compat/es2016/exponentation.rs index 480dde1121f..7a1914f93a3 100644 --- a/ecmascript/transforms/src/compat/es2016/exponentation.rs +++ b/ecmascript/transforms/src/compat/es2016/exponentation.rs @@ -30,11 +30,15 @@ pub fn exponentation() -> impl Pass { #[derive(Clone, Copy)] struct Exponentation; +noop_fold_type!(Exponentation); + #[derive(Default)] struct AssignFolder { vars: Vec, } +noop_fold_type!(AssignFolder); + impl Fold for AssignFolder { fn fold(&mut self, e: Expr) -> Expr { let e = e.fold_children(self); diff --git a/ecmascript/transforms/src/compat/es2017/async_to_generator.rs b/ecmascript/transforms/src/compat/es2017/async_to_generator.rs index 403cb44a121..0221e7d5b39 100644 --- a/ecmascript/transforms/src/compat/es2017/async_to_generator.rs +++ b/ecmascript/transforms/src/compat/es2017/async_to_generator.rs @@ -33,10 +33,15 @@ pub fn async_to_generator() -> impl Pass { #[derive(Default, Clone)] struct AsyncToGenerator; + +noop_fold_type!(AsyncToGenerator); + struct Actual { extra_stmts: Vec, } +noop_fold_type!(Actual); + impl Fold> for AsyncToGenerator where T: StmtLike + VisitWith + FoldWith, diff --git a/ecmascript/transforms/src/compat/es2018/object_rest_spread.rs b/ecmascript/transforms/src/compat/es2018/object_rest_spread.rs index 8892512be3b..0f4c3a3bb5f 100644 --- a/ecmascript/transforms/src/compat/es2018/object_rest_spread.rs +++ b/ecmascript/transforms/src/compat/es2018/object_rest_spread.rs @@ -17,6 +17,8 @@ pub fn object_rest_spread() -> impl Pass { struct ObjectRest; +noop_fold_type!(ObjectRest); + #[allow(clippy::vec_box)] struct RestFolder { /// Injected before the original statement. @@ -27,6 +29,8 @@ struct RestFolder { exprs: Vec>, } +noop_fold_type!(RestFolder); + macro_rules! impl_for_for_stmt { ($T:tt) => { impl Fold<$T> for RestFolder { @@ -1012,6 +1016,8 @@ fn simplify_pat(pat: Pat) -> Pat { struct ObjectSpread; +noop_fold_type!(ObjectSpread); + impl Fold for ObjectSpread { fn fold(&mut self, expr: Expr) -> Expr { // fast-path diff --git a/ecmascript/transforms/src/compat/es2018/optional_catch_binding.rs b/ecmascript/transforms/src/compat/es2018/optional_catch_binding.rs index f47a027c8a1..e11f86385d3 100644 --- a/ecmascript/transforms/src/compat/es2018/optional_catch_binding.rs +++ b/ecmascript/transforms/src/compat/es2018/optional_catch_binding.rs @@ -4,6 +4,8 @@ use swc_ecma_ast::*; struct OptionalCatchBinding; +noop_fold_type!(OptionalCatchBinding); + pub fn optional_catch_binding() -> impl Pass { OptionalCatchBinding } diff --git a/ecmascript/transforms/src/compat/es3/member_expr_lits.rs b/ecmascript/transforms/src/compat/es3/member_expr_lits.rs index 5eed858a8f7..e756b610ecf 100644 --- a/ecmascript/transforms/src/compat/es3/member_expr_lits.rs +++ b/ecmascript/transforms/src/compat/es3/member_expr_lits.rs @@ -22,6 +22,8 @@ use swc_ecma_ast::*; #[derive(Default, Clone, Copy)] pub struct MemberExprLit; +noop_fold_type!(MemberExprLit); + impl Fold for MemberExprLit { fn fold(&mut self, node: Module) -> Module { validate!(node.fold_children(self)) diff --git a/ecmascript/transforms/src/compat/es3/prop_lits.rs b/ecmascript/transforms/src/compat/es3/prop_lits.rs index d1a01064161..ae3bf70cc78 100644 --- a/ecmascript/transforms/src/compat/es3/prop_lits.rs +++ b/ecmascript/transforms/src/compat/es3/prop_lits.rs @@ -32,6 +32,8 @@ use swc_ecma_ast::*; #[derive(Default, Clone, Copy)] pub struct PropertyLiteral; +noop_fold_type!(PropertyLiteral); + impl Fold for PropertyLiteral { fn fold(&mut self, node: Module) -> Module { validate!(node.fold_children(self)) diff --git a/ecmascript/transforms/src/compat/es3/reserved_word.rs b/ecmascript/transforms/src/compat/es3/reserved_word.rs index 0cdc9cc2cd6..fd6fc7c3cd6 100644 --- a/ecmascript/transforms/src/compat/es3/reserved_word.rs +++ b/ecmascript/transforms/src/compat/es3/reserved_word.rs @@ -23,6 +23,8 @@ pub struct ReservedWord { pub preserve_import: bool, } +noop_fold_type!(ReservedWord); + impl Fold for ReservedWord { fn fold(&mut self, i: Ident) -> Ident { fold_ident(self.preserve_import, i) diff --git a/ecmascript/transforms/src/const_modules.rs b/ecmascript/transforms/src/const_modules.rs index 258ee879f60..142b821abf8 100644 --- a/ecmascript/transforms/src/const_modules.rs +++ b/ecmascript/transforms/src/const_modules.rs @@ -75,6 +75,8 @@ struct ConstModules { scope: Scope, } +noop_fold_type!(ConstModules); + #[derive(Default)] struct Scope { imported: HashMap>, diff --git a/ecmascript/transforms/src/fixer.rs b/ecmascript/transforms/src/fixer.rs index dfac6090da1..53d5362b1de 100644 --- a/ecmascript/transforms/src/fixer.rs +++ b/ecmascript/transforms/src/fixer.rs @@ -26,6 +26,8 @@ struct Fixer { span_map: FxHashMap, } +noop_fold_type!(Fixer); + #[repr(u8)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Context { diff --git a/ecmascript/transforms/src/helpers.rs b/ecmascript/transforms/src/helpers.rs index a10fc04a4b8..9cb8885267b 100644 --- a/ecmascript/transforms/src/helpers.rs +++ b/ecmascript/transforms/src/helpers.rs @@ -224,6 +224,9 @@ define_helpers!(Helpers { #[derive(Clone)] pub struct InjectHelpers; + +noop_fold_type!(InjectHelpers); + impl InjectHelpers { fn mk_helpers(&self) -> Vec { let (mark, external) = HELPERS.with(|helper| (helper.mark(), helper.external())); @@ -257,6 +260,9 @@ impl Fold for InjectHelpers { } struct Marker(Mark); + +noop_fold_type!(Marker); + impl Fold for Marker { fn fold(&mut self, sp: Span) -> Span { sp.apply_mark(self.0) diff --git a/ecmascript/transforms/src/hygiene.rs b/ecmascript/transforms/src/hygiene.rs index 626d1cd186b..55d3370125d 100644 --- a/ecmascript/transforms/src/hygiene.rs +++ b/ecmascript/transforms/src/hygiene.rs @@ -22,6 +22,8 @@ struct Hygiene<'a> { ident_type: IdentType, } +noop_fold_type!(Hygiene<'_>); + type Contexts = SmallVec<[SyntaxContext; 32]>; impl<'a> Hygiene<'a> { diff --git a/ecmascript/transforms/src/hygiene/ops.rs b/ecmascript/transforms/src/hygiene/ops.rs index 24509dd58cb..bd9431eb41d 100644 --- a/ecmascript/transforms/src/hygiene/ops.rs +++ b/ecmascript/transforms/src/hygiene/ops.rs @@ -12,6 +12,8 @@ pub(super) enum ScopeOp { pub(super) struct Operator<'a>(pub &'a [ScopeOp]); +noop_fold_type!(Operator<'_>); + impl<'a> Fold> for Operator<'a> { fn fold(&mut self, items: Vec) -> Vec { let mut stmts = Vec::with_capacity(items.len()); diff --git a/ecmascript/transforms/src/macros.rs b/ecmascript/transforms/src/macros.rs index bab49e8d630..a0b8ce0949c 100644 --- a/ecmascript/transforms/src/macros.rs +++ b/ecmascript/transforms/src/macros.rs @@ -210,3 +210,153 @@ macro_rules! validate { } }}; } + +macro_rules! noop_fold_type { + ($F:ty, $N:tt) => { + impl Fold for $F { + #[inline] + fn fold(&mut self, node: swc_ecma_ast::$N) -> swc_ecma_ast::$N { + node + } + } + }; + ($F:ty) => { + noop_fold_type!($F, Accessibility); + noop_fold_type!($F, TruePlusMinus); + noop_fold_type!($F, TsArrayType); + noop_fold_type!($F, TsCallSignatureDecl); + noop_fold_type!($F, TsConditionalType); + noop_fold_type!($F, TsConstructSignatureDecl); + noop_fold_type!($F, TsConstructorType); + noop_fold_type!($F, TsEntityName); + noop_fold_type!($F, TsEnumDecl); + noop_fold_type!($F, TsEnumMember); + noop_fold_type!($F, TsEnumMemberId); + noop_fold_type!($F, TsExternalModuleRef); + noop_fold_type!($F, TsFnOrConstructorType); + noop_fold_type!($F, TsFnParam); + noop_fold_type!($F, TsFnType); + noop_fold_type!($F, TsImportEqualsDecl); + noop_fold_type!($F, TsImportType); + noop_fold_type!($F, TsIndexSignature); + noop_fold_type!($F, TsIndexedAccessType); + noop_fold_type!($F, TsInferType); + noop_fold_type!($F, TsInterfaceBody); + noop_fold_type!($F, TsInterfaceDecl); + noop_fold_type!($F, TsIntersectionType); + noop_fold_type!($F, TsKeywordType); + noop_fold_type!($F, TsKeywordTypeKind); + noop_fold_type!($F, TsMappedType); + noop_fold_type!($F, TsMethodSignature); + noop_fold_type!($F, TsModuleBlock); + noop_fold_type!($F, TsModuleDecl); + noop_fold_type!($F, TsModuleName); + noop_fold_type!($F, TsModuleRef); + noop_fold_type!($F, TsNamespaceBody); + noop_fold_type!($F, TsNamespaceDecl); + noop_fold_type!($F, TsNamespaceExportDecl); + noop_fold_type!($F, TsOptionalType); + noop_fold_type!($F, TsParamProp); + noop_fold_type!($F, TsParamPropParam); + noop_fold_type!($F, TsParenthesizedType); + noop_fold_type!($F, TsPropertySignature); + noop_fold_type!($F, TsQualifiedName); + noop_fold_type!($F, TsRestType); + noop_fold_type!($F, TsSignatureDecl); + noop_fold_type!($F, TsThisType); + noop_fold_type!($F, TsThisTypeOrIdent); + noop_fold_type!($F, TsTupleType); + noop_fold_type!($F, TsType); + noop_fold_type!($F, TsTypeAliasDecl); + noop_fold_type!($F, TsTypeAnn); + noop_fold_type!($F, TsTypeAssertion); + noop_fold_type!($F, TsTypeCastExpr); + noop_fold_type!($F, TsTypeElement); + noop_fold_type!($F, TsTypeLit); + noop_fold_type!($F, TsTypeOperator); + noop_fold_type!($F, TsTypeOperatorOp); + noop_fold_type!($F, TsTypeParam); + noop_fold_type!($F, TsTypeParamDecl); + noop_fold_type!($F, TsTypeParamInstantiation); + noop_fold_type!($F, TsTypePredicate); + noop_fold_type!($F, TsTypeQuery); + noop_fold_type!($F, TsTypeQueryExpr); + noop_fold_type!($F, TsTypeRef); + noop_fold_type!($F, TsUnionOrIntersectionType); + noop_fold_type!($F, TsUnionType); + }; +} + +macro_rules! noop_visit_type { + ($F:ty, $N:tt) => { + impl Visit for $F { + #[inline] + fn visit(&mut self, _: &swc_ecma_ast::$N) {} + } + }; + ($F:ty) => { + noop_visit_type!($F, Accessibility); + noop_visit_type!($F, TruePlusMinus); + noop_visit_type!($F, TsArrayType); + noop_visit_type!($F, TsCallSignatureDecl); + noop_visit_type!($F, TsConditionalType); + noop_visit_type!($F, TsConstructSignatureDecl); + noop_visit_type!($F, TsConstructorType); + noop_visit_type!($F, TsEntityName); + noop_visit_type!($F, TsEnumDecl); + noop_visit_type!($F, TsEnumMember); + noop_visit_type!($F, TsEnumMemberId); + noop_visit_type!($F, TsExternalModuleRef); + noop_visit_type!($F, TsFnOrConstructorType); + noop_visit_type!($F, TsFnParam); + noop_visit_type!($F, TsFnType); + noop_visit_type!($F, TsImportEqualsDecl); + noop_visit_type!($F, TsImportType); + noop_visit_type!($F, TsIndexSignature); + noop_visit_type!($F, TsIndexedAccessType); + noop_visit_type!($F, TsInferType); + noop_visit_type!($F, TsInterfaceBody); + noop_visit_type!($F, TsInterfaceDecl); + noop_visit_type!($F, TsIntersectionType); + noop_visit_type!($F, TsKeywordType); + noop_visit_type!($F, TsKeywordTypeKind); + noop_visit_type!($F, TsMappedType); + noop_visit_type!($F, TsMethodSignature); + noop_visit_type!($F, TsModuleBlock); + noop_visit_type!($F, TsModuleDecl); + noop_visit_type!($F, TsModuleName); + noop_visit_type!($F, TsModuleRef); + noop_visit_type!($F, TsNamespaceBody); + noop_visit_type!($F, TsNamespaceDecl); + noop_visit_type!($F, TsNamespaceExportDecl); + noop_visit_type!($F, TsOptionalType); + noop_visit_type!($F, TsParamProp); + noop_visit_type!($F, TsParamPropParam); + noop_visit_type!($F, TsParenthesizedType); + noop_visit_type!($F, TsPropertySignature); + noop_visit_type!($F, TsQualifiedName); + noop_visit_type!($F, TsRestType); + noop_visit_type!($F, TsSignatureDecl); + noop_visit_type!($F, TsThisType); + noop_visit_type!($F, TsThisTypeOrIdent); + noop_visit_type!($F, TsTupleType); + noop_visit_type!($F, TsType); + noop_visit_type!($F, TsTypeAliasDecl); + noop_visit_type!($F, TsTypeAnn); + noop_visit_type!($F, TsTypeAssertion); + noop_visit_type!($F, TsTypeCastExpr); + noop_visit_type!($F, TsTypeElement); + noop_visit_type!($F, TsTypeLit); + noop_visit_type!($F, TsTypeOperator); + noop_visit_type!($F, TsTypeOperatorOp); + noop_visit_type!($F, TsTypeParam); + noop_visit_type!($F, TsTypeParamDecl); + noop_visit_type!($F, TsTypeParamInstantiation); + noop_visit_type!($F, TsTypePredicate); + noop_visit_type!($F, TsTypeQuery); + noop_visit_type!($F, TsTypeQueryExpr); + noop_visit_type!($F, TsTypeRef); + noop_visit_type!($F, TsUnionOrIntersectionType); + noop_visit_type!($F, TsUnionType); + }; +} diff --git a/ecmascript/transforms/src/modules/amd.rs b/ecmascript/transforms/src/modules/amd.rs index 33d1dd484d8..c7f9fa226a8 100644 --- a/ecmascript/transforms/src/modules/amd.rs +++ b/ecmascript/transforms/src/modules/amd.rs @@ -29,6 +29,8 @@ struct Amd { exports: Exports, } +noop_fold_type!(Amd); + #[derive(Debug, Clone, Default, Serialize, Deserialize)] #[serde(deny_unknown_fields, rename_all = "camelCase")] pub struct Config { diff --git a/ecmascript/transforms/src/modules/common_js.rs b/ecmascript/transforms/src/modules/common_js.rs index 06b04066207..09f866af7d7 100644 --- a/ecmascript/transforms/src/modules/common_js.rs +++ b/ecmascript/transforms/src/modules/common_js.rs @@ -26,6 +26,8 @@ struct CommonJs { in_top_level: bool, } +noop_fold_type!(CommonJs); + impl Fold> for CommonJs { fn fold(&mut self, items: Vec) -> Vec { let mut emitted_esmodule = false; diff --git a/ecmascript/transforms/src/modules/import_analysis.rs b/ecmascript/transforms/src/modules/import_analysis.rs index 12d6a195eee..5a1f1b97f39 100644 --- a/ecmascript/transforms/src/modules/import_analysis.rs +++ b/ecmascript/transforms/src/modules/import_analysis.rs @@ -15,6 +15,9 @@ struct ImportAnalyzer { scope: Scope, } +noop_fold_type!(ImportAnalyzer); +noop_visit_type!(ImportAnalyzer); + impl Fold for ImportAnalyzer { fn fold(&mut self, module: Module) -> Module { module.visit_with(self); diff --git a/ecmascript/transforms/src/modules/umd.rs b/ecmascript/transforms/src/modules/umd.rs index 42a08eb3cd6..dafed96f11e 100644 --- a/ecmascript/transforms/src/modules/umd.rs +++ b/ecmascript/transforms/src/modules/umd.rs @@ -35,6 +35,8 @@ struct Umd { exports: Exports, } +noop_fold_type!(Umd); + impl Fold for Umd { fn fold(&mut self, module: Module) -> Module { self.in_top_level = true; diff --git a/ecmascript/transforms/src/optimization/inline_globals.rs b/ecmascript/transforms/src/optimization/inline_globals.rs index fc8542d95d9..6089e0fd885 100644 --- a/ecmascript/transforms/src/optimization/inline_globals.rs +++ b/ecmascript/transforms/src/optimization/inline_globals.rs @@ -9,6 +9,8 @@ pub struct InlineGlobals { pub globals: HashMap, } +noop_fold_type!(InlineGlobals); + impl Fold for InlineGlobals { fn fold(&mut self, expr: Expr) -> Expr { let expr = match expr { diff --git a/ecmascript/transforms/src/optimization/json_parse.rs b/ecmascript/transforms/src/optimization/json_parse.rs index 765bfb33ac6..3789c41f661 100644 --- a/ecmascript/transforms/src/optimization/json_parse.rs +++ b/ecmascript/transforms/src/optimization/json_parse.rs @@ -31,6 +31,8 @@ pub struct JsonParse { pub min_cost: usize, } +noop_fold_type!(JsonParse); + impl Default for JsonParse { fn default() -> Self { JsonParse { min_cost: 1024 } diff --git a/ecmascript/transforms/src/optimization/simplify/branch/mod.rs b/ecmascript/transforms/src/optimization/simplify/branch/mod.rs index 716f6ee35a0..7a45f77389b 100644 --- a/ecmascript/transforms/src/optimization/simplify/branch/mod.rs +++ b/ecmascript/transforms/src/optimization/simplify/branch/mod.rs @@ -44,6 +44,8 @@ struct Remover { normal_block: bool, } +noop_fold_type!(Remover); + impl Fold> for Remover where Self: Fold, diff --git a/ecmascript/transforms/src/optimization/simplify/dce/mod.rs b/ecmascript/transforms/src/optimization/simplify/dce/mod.rs index 1ea85cddb43..8347e29ae4b 100644 --- a/ecmascript/transforms/src/optimization/simplify/dce/mod.rs +++ b/ecmascript/transforms/src/optimization/simplify/dce/mod.rs @@ -73,6 +73,8 @@ struct UsedMarkRemover { used_mark: Mark, } +noop_fold_type!(UsedMarkRemover); + impl CompilerPass for UsedMarkRemover { fn name() -> Cow<'static, str> { Cow::Borrowed("dce-cleanup") diff --git a/ecmascript/transforms/src/optimization/simplify/dce/side_effect.rs b/ecmascript/transforms/src/optimization/simplify/dce/side_effect.rs index 1326e4a0cfd..0253092ef44 100644 --- a/ecmascript/transforms/src/optimization/simplify/dce/side_effect.rs +++ b/ecmascript/transforms/src/optimization/simplify/dce/side_effect.rs @@ -34,6 +34,8 @@ impl SideEffectVisitor<'_> { } } +noop_visit_type!(SideEffectVisitor<'_>); + pub(super) struct SideEffectVisitor<'a> { included: &'a mut FxHashSet, exports: Option<&'a [Id]>, diff --git a/ecmascript/transforms/src/optimization/simplify/expr/mod.rs b/ecmascript/transforms/src/optimization/simplify/expr/mod.rs index 2233f62fce3..023b156213d 100644 --- a/ecmascript/transforms/src/optimization/simplify/expr/mod.rs +++ b/ecmascript/transforms/src/optimization/simplify/expr/mod.rs @@ -22,6 +22,8 @@ struct SimplifyExpr { changed: bool, } +noop_fold_type!(SimplifyExpr); + impl CompilerPass for SimplifyExpr { fn name() -> Cow<'static, str> { Cow::Borrowed("simplify-expr") diff --git a/ecmascript/transforms/src/optimization/simplify/inlining/mod.rs b/ecmascript/transforms/src/optimization/simplify/inlining/mod.rs index 36e5993f6c2..0f5580d2f0e 100644 --- a/ecmascript/transforms/src/optimization/simplify/inlining/mod.rs +++ b/ecmascript/transforms/src/optimization/simplify/inlining/mod.rs @@ -71,6 +71,8 @@ struct Inlining<'a> { pat_mode: PatFoldingMode, } +noop_fold_type!(Inlining<'_>); + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum PatFoldingMode { Assign, diff --git a/ecmascript/transforms/src/proposals/class_properties/class_name_tdz.rs b/ecmascript/transforms/src/proposals/class_properties/class_name_tdz.rs index df61106f685..df73e5c3c8a 100644 --- a/ecmascript/transforms/src/proposals/class_properties/class_name_tdz.rs +++ b/ecmascript/transforms/src/proposals/class_properties/class_name_tdz.rs @@ -6,6 +6,8 @@ pub(super) struct ClassNameTdzFolder<'a> { pub class_name: &'a Ident, } +noop_fold_type!(ClassNameTdzFolder<'_>); + impl<'a> Fold for ClassNameTdzFolder<'a> { fn fold(&mut self, expr: Expr) -> Expr { match expr { diff --git a/ecmascript/transforms/src/proposals/class_properties/private_field.rs b/ecmascript/transforms/src/proposals/class_properties/private_field.rs index 78aa00aeec6..0f2c6813dd4 100644 --- a/ecmascript/transforms/src/proposals/class_properties/private_field.rs +++ b/ecmascript/transforms/src/proposals/class_properties/private_field.rs @@ -13,6 +13,8 @@ pub(super) struct FieldAccessFolder<'a> { pub in_assign_pat: bool, } +noop_fold_type!(FieldAccessFolder<'_>); + impl<'a> Fold for FieldAccessFolder<'a> { fn fold(&mut self, e: Expr) -> Expr { match e { diff --git a/ecmascript/transforms/src/proposals/class_properties/this_in_static.rs b/ecmascript/transforms/src/proposals/class_properties/this_in_static.rs index fd762d51544..ba4e24c75d9 100644 --- a/ecmascript/transforms/src/proposals/class_properties/this_in_static.rs +++ b/ecmascript/transforms/src/proposals/class_properties/this_in_static.rs @@ -5,6 +5,8 @@ pub(super) struct ThisInStaticFolder { pub ident: Ident, } +noop_fold_type!(ThisInStaticFolder); + impl Fold for ThisInStaticFolder { fn fold(&mut self, e: Expr) -> Expr { let e = e.fold_children(self); diff --git a/ecmascript/transforms/src/proposals/class_properties/used_name.rs b/ecmascript/transforms/src/proposals/class_properties/used_name.rs index a08ad204102..f871a0df40e 100644 --- a/ecmascript/transforms/src/proposals/class_properties/used_name.rs +++ b/ecmascript/transforms/src/proposals/class_properties/used_name.rs @@ -7,6 +7,8 @@ pub(super) struct UsedNameRenamer<'a> { pub used_names: &'a [JsWord], } +noop_fold_type!(UsedNameRenamer<'_>); + impl<'a> Fold for UsedNameRenamer<'a> { fn fold(&mut self, ident: Ident) -> Ident { if self.used_names.contains(&ident.sym) { @@ -23,6 +25,8 @@ pub(super) struct UsedNameCollector<'a> { pub used_names: &'a mut Vec, } +noop_visit_type!(UsedNameCollector<'_>); + impl<'a> Visit for UsedNameCollector<'a> { fn visit(&mut self, expr: &Expr) { match *expr { diff --git a/ecmascript/transforms/src/proposals/decorators.rs b/ecmascript/transforms/src/proposals/decorators.rs index 5dbf261a703..3f89b692b9c 100644 --- a/ecmascript/transforms/src/proposals/decorators.rs +++ b/ecmascript/transforms/src/proposals/decorators.rs @@ -75,6 +75,8 @@ struct Decorators { is_in_strict: bool, } +noop_fold_type!(Decorators); + impl Fold> for Decorators { fn fold(&mut self, items: Vec) -> Vec { if !self::usage::has_decorator(&items) { diff --git a/ecmascript/transforms/src/proposals/decorators/legacy.rs b/ecmascript/transforms/src/proposals/decorators/legacy.rs index 855e3728e3b..7565345babf 100644 --- a/ecmascript/transforms/src/proposals/decorators/legacy.rs +++ b/ecmascript/transforms/src/proposals/decorators/legacy.rs @@ -15,6 +15,8 @@ pub(super) struct Legacy { exports: Vec, } +noop_fold_type!(Legacy); + impl Fold for Legacy { fn fold(&mut self, m: Module) -> Module { let mut m = m.fold_children(self); diff --git a/ecmascript/transforms/src/proposals/decorators/usage.rs b/ecmascript/transforms/src/proposals/decorators/usage.rs index ec55a4daab5..232c7975510 100644 --- a/ecmascript/transforms/src/proposals/decorators/usage.rs +++ b/ecmascript/transforms/src/proposals/decorators/usage.rs @@ -12,6 +12,8 @@ pub(super) struct DecoratorFinder { found: bool, } +noop_visit_type!(DecoratorFinder); + impl Visit for DecoratorFinder { fn visit(&mut self, _: &Decorator) { self.found = true; diff --git a/ecmascript/transforms/src/proposals/export.rs b/ecmascript/transforms/src/proposals/export.rs index 44bac94518e..386193d6bd5 100644 --- a/ecmascript/transforms/src/proposals/export.rs +++ b/ecmascript/transforms/src/proposals/export.rs @@ -11,6 +11,8 @@ pub fn export() -> impl Pass { #[derive(Clone)] struct ExportDefaultFrom; +noop_fold_type!(ExportDefaultFrom); + impl Fold> for ExportDefaultFrom { fn fold(&mut self, items: Vec) -> Vec { // Imports diff --git a/ecmascript/transforms/src/proposals/opt_chaining.rs b/ecmascript/transforms/src/proposals/opt_chaining.rs index 7b81bb44216..2581013a7a5 100644 --- a/ecmascript/transforms/src/proposals/opt_chaining.rs +++ b/ecmascript/transforms/src/proposals/opt_chaining.rs @@ -15,6 +15,8 @@ struct OptChaining { vars: Vec, } +noop_fold_type!(OptChaining); + impl Fold> for OptChaining where T: Debug + StmtLike + FoldWith, diff --git a/ecmascript/transforms/src/react/display_name.rs b/ecmascript/transforms/src/react/display_name.rs index 506265d456c..c04d73cc591 100644 --- a/ecmascript/transforms/src/react/display_name.rs +++ b/ecmascript/transforms/src/react/display_name.rs @@ -15,6 +15,8 @@ pub fn display_name() -> impl Pass { struct DisplayName; +noop_fold_type!(DisplayName); + impl Fold for DisplayName { fn fold(&mut self, decl: VarDeclarator) -> VarDeclarator { match decl.name { diff --git a/ecmascript/transforms/src/react/jsx.rs b/ecmascript/transforms/src/react/jsx.rs index 4737624e42b..07efa376995 100644 --- a/ecmascript/transforms/src/react/jsx.rs +++ b/ecmascript/transforms/src/react/jsx.rs @@ -114,6 +114,8 @@ struct Jsx { throw_if_namespace: bool, } +noop_fold_type!(Jsx); + impl Jsx { fn jsx_frag_to_expr(&mut self, el: JSXFragment) -> Expr { let span = el.span(); diff --git a/ecmascript/transforms/src/react/jsx_self.rs b/ecmascript/transforms/src/react/jsx_self.rs index 6e8eeae4b2f..c0e5604eafb 100644 --- a/ecmascript/transforms/src/react/jsx_self.rs +++ b/ecmascript/transforms/src/react/jsx_self.rs @@ -15,6 +15,8 @@ struct JsxSelf { dev: bool, } +noop_fold_type!(JsxSelf); + impl Fold for JsxSelf { fn fold(&mut self, mut n: JSXOpeningElement) -> JSXOpeningElement { if !self.dev { diff --git a/ecmascript/transforms/src/react/jsx_src.rs b/ecmascript/transforms/src/react/jsx_src.rs index 3178b7c3ac0..8a40179bf6f 100644 --- a/ecmascript/transforms/src/react/jsx_src.rs +++ b/ecmascript/transforms/src/react/jsx_src.rs @@ -16,6 +16,8 @@ struct JsxSrc { dev: bool, } +noop_fold_type!(JsxSrc); + impl Fold for JsxSrc { fn fold(&mut self, mut e: JSXOpeningElement) -> JSXOpeningElement { if !self.dev || e.span == DUMMY_SP { diff --git a/ecmascript/transforms/src/resolver.rs b/ecmascript/transforms/src/resolver.rs index 0b56c7e0957..ace42e00b3a 100644 --- a/ecmascript/transforms/src/resolver.rs +++ b/ecmascript/transforms/src/resolver.rs @@ -64,6 +64,8 @@ struct Resolver<'a> { ident_type: IdentType, } +noop_fold_type!(Resolver<'_>); + impl<'a> Resolver<'a> { fn new(mark: Mark, current: Scope<'a>, cur_defining: Option<(JsWord, Mark)>) -> Self { Resolver {