From 8defcb28196238e9f8bcbb0e8f345724d47f8882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 22 Feb 2024 11:54:41 +0900 Subject: [PATCH] perf(visit): Use `quote::quote!` instead of `pmutil::q!` (#8485) **Description:** **Related issue:** - Closes #7071. --- crates/swc_visit_macros/src/lib.rs | 1378 ++++++++++------------------ 1 file changed, 499 insertions(+), 879 deletions(-) diff --git a/crates/swc_visit_macros/src/lib.rs b/crates/swc_visit_macros/src/lib.rs index 897a31ae8b69..ef3bf8aa2d72 100644 --- a/crates/swc_visit_macros/src/lib.rs +++ b/crates/swc_visit_macros/src/lib.rs @@ -7,6 +7,7 @@ use std::{collections::HashSet, mem::replace}; use inflector::Inflector; use pmutil::{q, Quote, SpanExt}; use proc_macro2::Ident; +use quote::quote; use swc_macros_common::{call_site, def_site, make_doc_attr}; use syn::{ parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, Arm, AttrStyle, @@ -99,7 +100,7 @@ pub fn define(tts: proc_macro::TokenStream) -> proc_macro::TokenStream { let block = parse_macro_input!(tts as Block); let mut q = Quote::new_call_site(); - q.push_tokens(&q!({ + q.push_tokens("e!( use swc_visit::ParentKind; pub type AstKindPath = swc_visit::AstKindPath; @@ -120,7 +121,7 @@ pub fn define(tts: proc_macro::TokenStream) -> proc_macro::TokenStream { self.set_index(index) } } - })); + )); let mut field_module_body = vec![]; { @@ -178,7 +179,7 @@ fn make_field_enum_variant_from_named_field(type_name: &Ident, f: &Field) -> Var vis: Visibility::Inherited, ident: None, colon_token: None, - ty: q!({ usize }).parse(), + ty: parse_quote!(usize), mutability: FieldMutability::None, }); @@ -318,7 +319,7 @@ fn make_field_enum(item: &Item) -> Vec { v.push(FnArg::Typed(PatType { attrs: Default::default(), colon_token: Default::default(), - ty: q!({ usize }).parse(), + ty: parse_quote!(usize), pat: Box::new(Pat::Ident(PatIdent { attrs: Default::default(), by_ref: Default::default(), @@ -353,13 +354,7 @@ fn make_field_enum(item: &Item) -> Vec { pat: Pat::TupleStruct(PatTupleStruct { attrs: Default::default(), qself: None, - path: q!( - Vars { - VariantName: &variant_name - }, - { Self::VariantName } - ) - .parse(), + path: parse_quote!(Self::#variant_name), paren_token: name.span().as_token(), elems: { let mut v = Punctuated::new(); @@ -377,26 +372,25 @@ fn make_field_enum(item: &Item) -> Vec { }), guard: Default::default(), fat_arrow_token: name.span().as_token(), - body: q!(({ + body: parse_quote!({ debug_assert!( *idx == usize::MAX || index == usize::MAX, "Should be usize::MAX" ); *idx = index; - })) - .parse(), + }), comma: Some(name.span().as_token()), }); } } }; - arms.push(q!({ _ => {} }).parse()); + arms.push(parse_quote!(_ => {})); let expr = Expr::Match(ExprMatch { attrs: Default::default(), match_token: name.span().as_token(), - expr: q!({ self }).parse(), + expr: parse_quote!(self), brace_token: name.span().as_token(), arms, }); @@ -415,7 +409,7 @@ fn make_field_enum(item: &Item) -> Vec { impl_token: Default::default(), generics: Default::default(), trait_: Default::default(), - self_ty: q!(Vars { Type: &name }, { Type }).parse(), + self_ty: parse_quote!(#name), brace_token: Default::default(), items: methods, })); @@ -481,13 +475,7 @@ fn make_ast_enum(stmts: &[Stmt], is_ref: bool) -> Item { ident: None, ty: Type::Path(TypePath { qself: Default::default(), - path: q!( - Vars { - field_type_name: &field_type_name, - }, - (self::fields::field_type_name) - ) - .parse(), + path: parse_quote!(self::fields::#field_type_name), }), mutability: FieldMutability::None, }); @@ -598,7 +586,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl { v.push(FnArg::Typed(PatType { attrs: Default::default(), colon_token: Default::default(), - ty: q!({ usize }).parse(), + ty: parse_quote!(usize), pat: Box::new(Pat::Ident(PatIdent { attrs: Default::default(), by_ref: Default::default(), @@ -640,7 +628,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl { attrs: Default::default(), pat: Pat::TupleStruct(PatTupleStruct { attrs: Default::default(), - path: q!(Vars { name }, { Self::name }).parse(), + path: parse_quote!(Self::#name), qself: None, paren_token: Default::default(), elems: { @@ -658,7 +646,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl { }), guard: Default::default(), fat_arrow_token: name.span().as_token(), - body: q!({ v.set_index(index) }).parse(), + body: parse_quote!(v.set_index(index)), comma: Some(name.span().as_token()), }) } @@ -666,7 +654,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl { let match_expr = Expr::Match(ExprMatch { attrs: Default::default(), match_token: Default::default(), - expr: q!({ self }).parse(), + expr: parse_quote!(self), brace_token: Default::default(), arms, }); @@ -682,7 +670,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl { unsafety: Default::default(), impl_token: Default::default(), generics: Default::default(), - trait_: Some((None, q!((ParentKind)).parse(), Token![for](def_site()))), + trait_: Some((None, parse_quote!(ParentKind), Token![for](def_site()))), self_ty: Box::new(kind_type), brace_token: Default::default(), items: vec![set_index_item], @@ -744,7 +732,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { let pat = Pat::TupleStruct(PatTupleStruct { attrs: Default::default(), - path: q!(Vars { Name: &name }, (Self::Name)).parse(), + path: parse_quote!(Self::#name), qself: None, paren_token: Default::default(), elems: { @@ -771,7 +759,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { let path_expr = Expr::Path(ExprPath { attrs: Default::default(), qself: Default::default(), - path: q!(Vars { Name: &name }, (AstParentKind::Name)).parse(), + path: parse_quote!(AstParentKind::#name), }); arms.push(Arm { @@ -804,7 +792,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { let expr = Expr::Match(ExprMatch { attrs: Default::default(), match_token: Default::default(), - expr: q!({ self }).parse(), + expr: parse_quote!(self), brace_token: Default::default(), arms, }); @@ -833,7 +821,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { v.push(FnArg::Typed(PatType { attrs: Default::default(), colon_token: Default::default(), - ty: q!({ usize }).parse(), + ty: parse_quote!(usize), pat: Box::new(Pat::Ident(PatIdent { attrs: Default::default(), by_ref: Default::default(), @@ -876,7 +864,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { let pat = Pat::TupleStruct(PatTupleStruct { attrs: Default::default(), qself: None, - path: q!(Vars { Name: &name }, (Self::Name)).parse(), + path: parse_quote!(Self::#name), paren_token: Default::default(), elems: { let mut v = Punctuated::new(); @@ -904,7 +892,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { pat, guard: Default::default(), fat_arrow_token: stmt.span().as_token(), - body: q!({ __field_kind.set_index(index) }).parse(), + body: parse_quote!(__field_kind.set_index(index)), comma: Some(stmt.span().as_token()), }); } @@ -912,7 +900,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { let match_expr = Expr::Match(ExprMatch { attrs: Default::default(), match_token: Default::default(), - expr: q!({ self }).parse(), + expr: parse_quote!(self), brace_token: Default::default(), arms, }); @@ -929,7 +917,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { impl_token: Default::default(), generics: Default::default(), trait_: None, - self_ty: q!({ AstParentNodeRef<'_> }).parse(), + self_ty: parse_quote!(AstParentNodeRef<'_>), brace_token: Default::default(), items: vec![kind_item, set_index_item], }) @@ -968,7 +956,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { } // Remove `Box` - types.retain(|ty| as_box(ty).is_none()); + types.retain(|ty| extract_box(ty).is_none()); types.sort_by_cached_key(|ty| method_name_as_str(mode, ty)); types.dedup_by_key(|ty| method_name_as_str(mode, ty)); @@ -1003,10 +991,10 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { // &'_ mut V, Box let block = match mode.visitor_variant() { Some(VisitorVariant::Normal) | None => { - q!(Vars { visit: &name }, ({ (**self).visit(n) })).parse() + parse_quote!({ (**self).#name(n) }) } Some(VisitorVariant::WithPath) => { - q!(Vars { visit: &name }, ({ (**self).visit(n, __ast_path) })).parse() + parse_quote!({ (**self).#name(n, __ast_path) }) } }; @@ -1028,26 +1016,18 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { defaultness: None, sig: sig.clone(), block: match mode.visitor_variant() { - Some(VisitorVariant::Normal) | None => q!( - Vars { visit: &name }, - ({ - match self { - swc_visit::Either::Left(v) => v.visit(n), - swc_visit::Either::Right(v) => v.visit(n), - } - }) - ) - .parse(), - Some(VisitorVariant::WithPath) => q!( - Vars { visit: &name }, - ({ - match self { - swc_visit::Either::Left(v) => v.visit(n, __ast_path), - swc_visit::Either::Right(v) => v.visit(n, __ast_path), - } - }) - ) - .parse(), + Some(VisitorVariant::Normal) | None => parse_quote!({ + match self { + swc_visit::Either::Left(v) => v.#name(n), + swc_visit::Either::Right(v) => v.#name(n), + } + }), + Some(VisitorVariant::WithPath) => parse_quote!({ + match self { + swc_visit::Either::Left(v) => v.#name(n, __ast_path), + swc_visit::Either::Right(v) => v.#name(n, __ast_path), + } + }), }, }); } @@ -1063,50 +1043,34 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { block: match mode { Mode::VisitAll | Mode::Visit(VisitorVariant::Normal) - | Mode::VisitMut(VisitorVariant::Normal) => q!( - Vars { visit: &name }, - ({ - if self.enabled { - self.visitor.visit(n) - } - }) - ) - .parse(), + | Mode::VisitMut(VisitorVariant::Normal) => parse_quote!({ + if self.enabled { + self.visitor.#name(n) + } + }), Mode::Visit(VisitorVariant::WithPath) - | Mode::VisitMut(VisitorVariant::WithPath) => q!( - Vars { visit: &name }, - ({ - if self.enabled { - self.visitor.visit(n, __ast_path) - } - }) - ) - .parse(), - - Mode::Fold(VisitorVariant::Normal) => q!( - Vars { fold: &name }, - ({ - if self.enabled { - self.visitor.fold(n) - } else { - n - } - }) - ) - .parse(), - - Mode::Fold(VisitorVariant::WithPath) => q!( - Vars { fold: &name }, - ({ - if self.enabled { - self.visitor.fold(n, __ast_path) - } else { - n - } - }) - ) - .parse(), + | Mode::VisitMut(VisitorVariant::WithPath) => parse_quote!({ + if self.enabled { + self.visitor.#name(n, __ast_path) + } + }), + + Mode::Fold(VisitorVariant::Normal) => parse_quote!({ + if self.enabled { + self.visitor.#name(n) + } else { + n + } + }), + + Mode::Fold(VisitorVariant::WithPath) => parse_quote!({ + if self.enabled { + self.visitor.#name(n, __ast_path) + } else { + n + } + }), }, }); } @@ -1119,14 +1083,10 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { vis: Visibility::Inherited, defaultness: None, sig: sig.clone(), - block: q!( - Vars { visit: &name }, - ({ - self.visitor.visit(n); - visit(self, n); - }) - ) - .parse(), + block: parse_quote!({ + self.visitor.#name(n); + #name(self, n); + }), }); } } @@ -1150,21 +1110,13 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { Some(match mode { Mode::Fold(VisitorVariant::Normal) | Mode::VisitMut(VisitorVariant::Normal) - | Mode::Visit(VisitorVariant::Normal) => q!(Vars { fn_name: &fn_name }, { - { - fn_name(self, n) - } - }) - .parse(), + | Mode::Visit(VisitorVariant::Normal) => parse_quote!({#fn_name(self, n)}), Mode::Fold(VisitorVariant::WithPath) | Mode::VisitMut(VisitorVariant::WithPath) - | Mode::Visit(VisitorVariant::WithPath) => q!(Vars { fn_name: &fn_name }, { - { - fn_name(self, n, __ast_path) - } - }) - .parse(), + | Mode::Visit(VisitorVariant::WithPath) => { + parse_quote!({ #fn_name(self, n, __ast_path) }) + } Mode::VisitAll => Block { brace_token: Default::default(), @@ -1185,126 +1137,92 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { .unwrap(); match mode { - Mode::Fold(VisitorVariant::Normal) => tokens.push_tokens(&q!( - Vars { - fn_name, - default_body, - Type: arg_ty, - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { + Mode::Fold(VisitorVariant::Normal) => { + let t = Ident::new(mode.trait_name(), call_site()); + tokens.push_tokens("e!( /// Visits children of the nodes with the given visitor. /// /// This is the default implementation of a method of /// [Fold]. #[allow(non_shorthand_field_patterns, unused_variables)] - pub fn fn_name(_visitor: &mut V, n: Type) -> Type { - default_body + pub fn #fn_name(_visitor: &mut V, n: #arg_ty) -> #arg_ty { + #default_body } - } - )), + )) + } - Mode::VisitMut(VisitorVariant::Normal) => tokens.push_tokens(&q!( - Vars { - fn_name, - default_body, - Type: arg_ty, - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { - /// Visits children of the nodes with the given visitor. - /// - /// This is the default implementation of a method of - /// [VisitMut]. + Mode::VisitMut(VisitorVariant::Normal) => { + let t = Ident::new(mode.trait_name(), call_site()); + tokens.push_tokens("e!( #[allow(non_shorthand_field_patterns, unused_variables)] - pub fn fn_name(_visitor: &mut V, n: Type) { - default_body + pub fn #fn_name(_visitor: &mut V, n: #arg_ty) { + #default_body } - } - )), + )) + } - Mode::Visit(VisitorVariant::Normal) => tokens.push_tokens(&q!( - Vars { - fn_name, - default_body, - Type: arg_ty, - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { + Mode::Visit(VisitorVariant::Normal) => { + let t = Ident::new(mode.trait_name(), call_site()); + + tokens.push_tokens("e!( /// Visits children of the nodes with the given visitor. /// /// This is the default implementation of a method of /// [Visit]. #[allow(non_shorthand_field_patterns, unused_variables)] - pub fn fn_name(_visitor: &mut V, n: Type) { - default_body + pub fn #fn_name(_visitor: &mut V, n: #arg_ty) { + #default_body } - } - )), + )) + } - Mode::Fold(VisitorVariant::WithPath) => tokens.push_tokens(&q!( - Vars { - fn_name, - default_body, - Type: arg_ty, - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { + Mode::Fold(VisitorVariant::WithPath) => { + let t = Ident::new(mode.trait_name(), call_site()); + tokens.push_tokens("e!( #[cfg(any(feature = "path", docsrs))] #[cfg_attr(docsrs, doc(cfg(feature = "path")))] #[allow(non_shorthand_field_patterns, unused_variables)] - fn fn_name( + fn #fn_name( _visitor: &mut V, - n: Type, + n: #arg_ty, __ast_path: &mut AstKindPath, - ) -> Type { - default_body + ) -> #arg_ty { + #default_body } - } - )), + )) + } - Mode::VisitMut(VisitorVariant::WithPath) => tokens.push_tokens(&q!( - Vars { - fn_name, - default_body, - Type: arg_ty, - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { + Mode::VisitMut(VisitorVariant::WithPath) => { + let t = Ident::new(mode.trait_name(), call_site()); + + tokens.push_tokens("e!( #[cfg(any(feature = "path", docsrs))] #[cfg_attr(docsrs, doc(cfg(feature = "path")))] #[allow(non_shorthand_field_patterns, unused_variables)] - fn fn_name( + fn #fn_name( _visitor: &mut V, - n: Type, + n: #arg_ty, __ast_path: &mut AstKindPath, ) { - default_body + #default_body } - } - )), + )) + } Mode::Visit(VisitorVariant::WithPath) => { - tokens.push_tokens(&q!( - Vars { - fn_name, - default_body, - Type: arg_ty, - Trait: Ident::new(mode.trait_name(), call_site()), - }, + let t = Ident::new(mode.trait_name(), call_site()); + tokens.push_tokens("e!( + #[cfg(any(feature = "path", docsrs))] + #[cfg_attr(docsrs, doc(cfg(feature = "path")))] + #[allow(non_shorthand_field_patterns, unused_variables)] + fn #fn_name<'ast, 'r, V: ?Sized + #t>( + _visitor: &mut V, + n: #arg_ty, + __ast_path: &mut AstNodePath<'r>, + ) where + 'ast: 'r, { - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - #[allow(non_shorthand_field_patterns, unused_variables)] - fn fn_name<'ast, 'r, V: ?Sized + Trait>( - _visitor: &mut V, - n: Type, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - default_body - } + #default_body } )); } @@ -1337,15 +1255,10 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { { // impl Visit for &'_ mut V - let mut item = q!( - Vars { - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { - impl<'a, V> Trait for &'a mut V where V: ?Sized + Trait {} - } - ) - .parse::(); + let trait_name = Ident::new(mode.trait_name(), call_site()); + + let mut item: ItemImpl = + parse_quote!(impl<'a, V> #trait_name for &'a mut V where V: ?Sized + #trait_name {}); item.items .extend(ref_methods.clone().into_iter().map(ImplItem::Fn)); @@ -1359,15 +1272,11 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { { // impl Visit for Box - let mut item = q!( - Vars { - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { - impl Trait for Box where V: ?Sized + Trait {} - } - ) - .parse::(); + let trait_name = Ident::new(mode.trait_name(), call_site()); + + let mut item: ItemImpl = parse_quote!( + impl #trait_name for Box where V: ?Sized + #trait_name {} + ); item.items.extend(ref_methods.into_iter().map(ImplItem::Fn)); @@ -1380,15 +1289,10 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { { // impl Trait for Optional - let mut item = q!( - Vars { - Trait: Ident::new(mode.trait_name(), call_site()), - }, - { - impl Trait for ::swc_visit::Optional where V: Trait {} - } - ) - .parse::(); + let trait_name = Ident::new(mode.trait_name(), call_site()); + + let mut item: ItemImpl = + parse_quote!(impl #trait_name for ::swc_visit::Optional where V: #trait_name {}); item.items .extend(optional_methods.into_iter().map(ImplItem::Fn)); @@ -1401,21 +1305,16 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { } { + let trait_name = Ident::new(mode.trait_name(), call_site()); // impl Trait for Either - let mut item = q!( - Vars { - Trait: Ident::new(mode.trait_name(), call_site()), - }, + let mut item: ItemImpl = parse_quote!( + impl #trait_name for ::swc_visit::Either + where + A: #trait_name, + B: #trait_name, { - impl Trait for ::swc_visit::Either - where - A: Trait, - B: Trait, - { - } } - ) - .parse::(); + ); item.items .extend(either_methods.into_iter().map(ImplItem::Fn)); @@ -1429,25 +1328,24 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { // impl Visit for swc_visit::All where V: VisitAll if mode == Mode::VisitAll { - let mut item = q!(Vars {}, { + let mut item: ItemImpl = parse_quote!( impl Visit for ::swc_visit::All where V: VisitAll {} - }) - .parse::(); + ); item.items .extend(visit_all_methods.into_iter().map(ImplItem::Fn)); tokens.push_tokens(&item); - tokens.push_tokens(&q!({ + tokens.push_tokens("e!( pub use swc_visit::All; - })); + )); } { // Add FoldWith, VisitWith let trait_decl = match mode { - Mode::Visit(VisitorVariant::Normal) => q!({ + Mode::Visit(VisitorVariant::Normal) => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. pub trait VisitWith { @@ -1472,9 +1370,9 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_children_with(v) } } - }), + ), - Mode::Visit(VisitorVariant::WithPath) => q!({ + Mode::Visit(VisitorVariant::WithPath) => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. #[cfg(any(feature = "path", docsrs))] @@ -1531,9 +1429,9 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_children_with_path(v, ast_path) } } - }), + ), - Mode::VisitAll => q!({ + Mode::VisitAll => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. pub trait VisitAllWith { @@ -1558,8 +1456,8 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_all_children_with(v) } } - }), - Mode::Fold(VisitorVariant::Normal) => q!({ + ), + Mode::Fold(VisitorVariant::Normal) => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. pub trait FoldWith { @@ -1584,8 +1482,8 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { swc_visit::util::map::Map::map(self, |value| value.fold_children_with(v)) } } - }), - Mode::Fold(VisitorVariant::WithPath) => q!({ + ), + Mode::Fold(VisitorVariant::WithPath) => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. #[cfg(any(feature = "path", docsrs))] @@ -1629,8 +1527,8 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { }) } } - }), - Mode::VisitMut(VisitorVariant::Normal) => q!({ + ), + Mode::VisitMut(VisitorVariant::Normal) => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. pub trait VisitMutWith { @@ -1653,8 +1551,8 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_mut_children_with(v); } } - }), - Mode::VisitMut(VisitorVariant::WithPath) => q!({ + ), + Mode::VisitMut(VisitorVariant::WithPath) => quote!( /// A utility trait implemented for ast nodes, and allow to /// visit them with a visitor. #[cfg(any(feature = "path", docsrs))] @@ -1697,14 +1595,14 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_mut_children_with_path(v, ast_path); } } - }), + ), }; tokens.push_tokens(&trait_decl); let mut names = HashSet::new(); for ty in &types { - if as_box(ty).is_some() { + if extract_box(ty).is_some() { continue; } @@ -1723,43 +1621,32 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { } names.insert(s); - let expr = visit_expr(mode, ty, &q!({ v }).parse(), q!({ self }).parse(), None); + let expr = visit_expr(mode, ty, &parse_quote!(v), parse_quote!(self), None); match mode { Mode::Visit(VisitorVariant::Normal) => { - let default_body = adjust_expr(mode, ty, q!({ self }).parse(), |expr| { - q!( - Vars { - expr, - method_name: &method_name - }, - { method_name(_visitor, expr) } - ) - .parse() - }); + let default_body = adjust_expr( + mode, + ty, + parse_quote!(self), + |expr| parse_quote!(#method_name(_visitor, #expr)), + ); if let Some(elem_ty) = extract_generic("Vec", ty) { - tokens.push_tokens(&q!( - Vars { - elem_ty, - expr, - default_body, - }, - { - impl VisitWith for [elem_ty] { - fn visit_with(&self, v: &mut V) { - expr - } + tokens.push_tokens("e!( + impl VisitWith for [#elem_ty] { + fn visit_with(&self, v: &mut V) { + #expr + } - fn visit_children_with(&self, _visitor: &mut V) { - default_body - } + fn visit_children_with(&self, _visitor: &mut V) { + #default_body } } )); - tokens.push_tokens(&q!(Vars { Type: ty }, { - impl VisitWith for Type { + tokens.push_tokens("e!( + impl VisitWith for #ty { fn visit_with(&self, v: &mut V) { (**self).visit_with(v) } @@ -1768,23 +1655,16 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_children_with(_visitor) } } - })); + )); } else { - tokens.push_tokens(&q!( - Vars { - Type: ty, - expr, - default_body, - }, - { - impl VisitWith for Type { - fn visit_with(&self, v: &mut V) { - expr - } + tokens.push_tokens("e!( + impl VisitWith for #ty { + fn visit_with(&self, v: &mut V) { + #expr + } - fn visit_children_with(&self, _visitor: &mut V) { - default_body - } + fn visit_children_with(&self, _visitor: &mut V) { + #default_body } } )); @@ -1792,55 +1672,44 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { } Mode::Visit(VisitorVariant::WithPath) => { - let default_body = adjust_expr(mode, ty, q!({ self }).parse(), |expr| { - q!( - Vars { - expr, - method_name: &method_name - }, - { method_name(_visitor, expr, __ast_path) } - ) - .parse() - }); + let default_body = adjust_expr( + mode, + ty, + parse_quote!(self), + |expr| parse_quote!(#method_name(_visitor, #expr, __ast_path)), + ); if let Some(elem_ty) = extract_generic("Vec", ty) { - tokens.push_tokens(&q!( - Vars { - elem_ty, - expr, - default_body, - }, - { - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl VisitWithPath for [elem_ty] { - fn visit_with_path<'ast, 'r>( - &'ast self, - v: &mut V, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - expr - } + tokens.push_tokens("e!( + #[cfg(any(feature = "path", docsrs))] + #[cfg_attr(docsrs, doc(cfg(feature = "path")))] + impl VisitWithPath for [#elem_ty] { + fn visit_with_path<'ast, 'r>( + &'ast self, + v: &mut V, + __ast_path: &mut AstNodePath<'r>, + ) where + 'ast: 'r, + { + #expr + } - fn visit_children_with_path<'ast, 'r>( - &'ast self, - _visitor: &mut V, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - default_body - } + fn visit_children_with_path<'ast, 'r>( + &'ast self, + _visitor: &mut V, + __ast_path: &mut AstNodePath<'r>, + ) where + 'ast: 'r, + { + #default_body } } )); - tokens.push_tokens(&q!(Vars { Type: ty }, { + tokens.push_tokens("e!( #[cfg(any(feature = "path", docsrs))] #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl VisitWithPath for Type { + impl VisitWithPath for #ty { fn visit_with_path<'ast, 'r>( &'ast self, v: &mut V, @@ -1861,37 +1730,30 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { (**self).visit_children_with_path(_visitor, __ast_path) } } - })); + )); } else { - tokens.push_tokens(&q!( - Vars { - Type: ty, - expr, - default_body, - }, - { - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl VisitWithPath for Type { - fn visit_with_path<'ast, 'r>( - &'ast self, - v: &mut V, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - expr - } + tokens.push_tokens("e!( + #[cfg(any(feature = "path", docsrs))] + #[cfg_attr(docsrs, doc(cfg(feature = "path")))] + impl VisitWithPath for #ty { + fn visit_with_path<'ast, 'r>( + &'ast self, + v: &mut V, + __ast_path: &mut AstNodePath<'r>, + ) where + 'ast: 'r, + { + #expr + } - fn visit_children_with_path<'ast, 'r>( - &'ast self, - _visitor: &mut V, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - default_body - } + fn visit_children_with_path<'ast, 'r>( + &'ast self, + _visitor: &mut V, + __ast_path: &mut AstNodePath<'r>, + ) where + 'ast: 'r, + { + #default_body } } )); @@ -1899,162 +1761,115 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote { } Mode::VisitAll => { - let default_body = adjust_expr(mode, ty, q!({ self }).parse(), |expr| { - q!( - Vars { - expr, - method_name: &method_name - }, - { method_name(_visitor, expr,) } - ) - .parse() - }); + let default_body = adjust_expr( + mode, + ty, + parse_quote!(self), + |expr| parse_quote!(#method_name(_visitor, #expr)), + ); - tokens.push_tokens(&q!( - Vars { - Type: ty, - expr, - default_body, - }, - { - impl VisitAllWith for Type { - fn visit_all_with(&self, v: &mut V) { - let mut all = ::swc_visit::All { visitor: v }; - let mut v = &mut all; - expr - } + tokens.push_tokens("e!( + impl VisitAllWith for #ty { + fn visit_all_with(&self, v: &mut V) { + let mut all = ::swc_visit::All { visitor: v }; + let mut v = &mut all; + #expr + } - fn visit_all_children_with(&self, _visitor: &mut V) { - let mut all = ::swc_visit::All { visitor: _visitor }; - let mut _visitor = &mut all; - default_body - } + fn visit_all_children_with(&self, _visitor: &mut V) { + let mut all = ::swc_visit::All { visitor: _visitor }; + let mut _visitor = &mut all; + #default_body } } )); } Mode::VisitMut(VisitorVariant::Normal) => { - let default_body = adjust_expr(mode, ty, q!({ self }).parse(), |expr| { - q!( - Vars { - expr, - method_name: &method_name - }, - { method_name(_visitor, expr) } - ) - .parse() - }); + let default_body = adjust_expr( + mode, + ty, + parse_quote!(self), + |expr| parse_quote!(#method_name(_visitor, #expr)), + ); - tokens.push_tokens(&q!( - Vars { - default_body, - Type: ty, - expr, - }, - { - impl VisitMutWith for Type { - fn visit_mut_with(&mut self, v: &mut V) { - expr - } + tokens.push_tokens("e!( + impl VisitMutWith for #ty { + fn visit_mut_with(&mut self, v: &mut V) { + #expr + } - fn visit_mut_children_with(&mut self, _visitor: &mut V) { - default_body - } + fn visit_mut_children_with(&mut self, _visitor: &mut V) { + #default_body } } )); } Mode::VisitMut(VisitorVariant::WithPath) => { - let default_body = adjust_expr(mode, ty, q!({ self }).parse(), |expr| { - q!( - Vars { - expr, - method_name: &method_name - }, - { method_name(_visitor, expr, __ast_path) } - ) - .parse() - }); + let default_body = adjust_expr( + mode, + ty, + parse_quote!(self), + |expr| parse_quote!(#method_name(_visitor, #expr, __ast_path)), + ); - tokens.push_tokens(&q!( - Vars { - default_body, - Type: ty, - expr, - }, - { - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl VisitMutWithPath for Type { - fn visit_mut_with_path( - &mut self, - v: &mut V, - __ast_path: &mut AstKindPath, - ) { - expr - } + tokens.push_tokens("e!( + #[cfg(any(feature = "path", docsrs))] + #[cfg_attr(docsrs, doc(cfg(feature = "path")))] + impl VisitMutWithPath for #ty { + fn visit_mut_with_path( + &mut self, + v: &mut V, + __ast_path: &mut AstKindPath, + ) { + #expr + } - fn visit_mut_children_with_path( - &mut self, - _visitor: &mut V, - __ast_path: &mut AstKindPath, - ) { - default_body - } + fn visit_mut_children_with_path( + &mut self, + _visitor: &mut V, + __ast_path: &mut AstKindPath, + ) { + #default_body } } )); } Mode::Fold(VisitorVariant::Normal) => { - tokens.push_tokens(&q!( - Vars { - method_name, - Type: ty, - expr, - }, - { - impl FoldWith for Type { - fn fold_with(self, v: &mut V) -> Self { - expr - } + tokens.push_tokens("e!( + impl FoldWith for #ty { + fn fold_with(self, v: &mut V) -> Self { + #expr + } - fn fold_children_with(self, v: &mut V) -> Self { - method_name(v, self) - } + fn fold_children_with(self, v: &mut V) -> Self { + #method_name(v, self) } } )); } Mode::Fold(VisitorVariant::WithPath) => { - tokens.push_tokens(&q!( - Vars { - method_name, - Type: ty, - expr, - }, - { - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl FoldWithPath for Type { - fn fold_with_path( - self, - v: &mut V, - __ast_path: &mut AstKindPath, - ) -> Self { - expr - } + tokens.push_tokens("e!( + #[cfg(any(feature = "path", docsrs))] + #[cfg_attr(docsrs, doc(cfg(feature = "path")))] + impl FoldWithPath for #ty { + fn fold_with_path( + self, + v: &mut V, + __ast_path: &mut AstKindPath, + ) -> Self { + #expr + } - fn fold_children_with_path( - self, - v: &mut V, - __ast_path: &mut AstKindPath, - ) -> Self { - method_name(v, self, __ast_path) - } + fn fold_children_with_path( + self, + v: &mut V, + __ast_path: &mut AstKindPath, + ) -> Self { + #method_name(v, self, __ast_path) } } )); @@ -2076,33 +1891,33 @@ where Mode::Fold { .. } => expr, Mode::VisitMut { .. } => expr, Mode::Visit { .. } | Mode::VisitAll => { - q!(Vars { expr }, { expr.as_ref().map(|v| &**v) }).parse() + parse_quote!(#expr.as_ref().map(|v| &**v)) } } } else { match mode { Mode::Fold { .. } => expr, Mode::VisitMut { .. } => expr, - Mode::Visit { .. } | Mode::VisitAll => q!(Vars { expr }, { expr.as_ref() }).parse(), + Mode::Visit { .. } | Mode::VisitAll => parse_quote!(#expr.as_ref()), } }; } - if as_box(ty).is_some() { + if extract_box(ty).is_some() { expr = match mode { Mode::Visit { .. } | Mode::VisitAll => expr, Mode::VisitMut { .. } => expr, - Mode::Fold { .. } => q!(Vars { expr }, { *expr }).parse(), + Mode::Fold { .. } => parse_quote!(*#expr), }; } expr = visit(expr); - if as_box(ty).is_some() { + if extract_box(ty).is_some() { expr = match mode { Mode::Visit { .. } | Mode::VisitAll => expr, Mode::VisitMut { .. } => expr, - Mode::Fold { .. } => q!(Vars { expr }, { Box::new(expr) }).parse(), + Mode::Fold { .. } => parse_quote!(Box::new(#expr)), }; } @@ -2126,15 +1941,7 @@ fn visit_expr( Mode::Fold(VisitorVariant::Normal) | Mode::VisitMut(VisitorVariant::Normal) | Mode::Visit(VisitorVariant::Normal) - | Mode::VisitAll => q!( - Vars { - visitor, - expr, - visit_name - }, - { visitor.visit_name(expr) } - ) - .parse(), + | Mode::VisitAll => parse_quote!(#visitor.#visit_name(#expr)), Mode::Fold(VisitorVariant::WithPath) | Mode::VisitMut(VisitorVariant::WithPath) @@ -2148,85 +1955,34 @@ fn visit_expr( let ast_path_expr: Expr = match mode { Mode::Visit(..) => { if is_vec_or_opt_vec(ty) { - q!( - Vars { - VariantName: type_name, - FieldType: field_type_name, - FieldName: field_name, - }, - (AstParentNodeRef::VariantName( - n, - self::fields::FieldType::FieldName(usize::MAX) - )) - ) - .parse() + parse_quote!(AstParentNodeRef::#type_name( + n, + self::fields::#field_type_name::#field_name(usize::MAX) + )) } else { - q!( - Vars { - VariantName: type_name, - FieldType: field_type_name, - FieldName: field_name, - }, - (AstParentNodeRef::VariantName( - n, - self::fields::FieldType::FieldName - )) - ) - .parse() + parse_quote!(AstParentNodeRef::#type_name( + n, + self::fields::#field_type_name::#field_name + )) } } _ => { if is_vec_or_opt_vec(ty) { - q!( - Vars { - VariantName: type_name, - FieldType: field_type_name, - FieldName: field_name, - }, - (AstParentKind::VariantName(self::fields::FieldType::FieldName( - usize::MAX - ))) - ) - .parse() + parse_quote!(AstParentKind::#type_name(self::fields::#field_type_name::#field_name( + usize::MAX + ))) } else { - q!( - Vars { - VariantName: type_name, - FieldType: field_type_name, - FieldName: field_name, - }, - (AstParentKind::VariantName(self::fields::FieldType::FieldName)) - ) - .parse() + parse_quote!(AstParentKind::#type_name(self::fields::#field_type_name::#field_name)) } } }; - q!( - Vars { - visitor, - expr, - visit_name, - ast_path_expr, - }, - { - { - let mut __ast_path = __ast_path.with_guard(ast_path_expr); - visitor.visit_name(expr, &mut *__ast_path) - } - } - ) - .parse() + parse_quote!({ + let mut __ast_path = __ast_path.with_guard(#ast_path_expr); + #visitor.#visit_name(#expr, &mut *__ast_path) + }) } else { - q!( - Vars { - visitor, - expr, - visit_name - }, - { visitor.visit_name(expr, __ast_path) } - ) - .parse() + parse_quote!(#visitor.#visit_name(#expr, __ast_path)) } } }) @@ -2252,13 +2008,7 @@ fn make_arm_from_struct( .unwrap_or_else(|| Ident::new(&format!("_{}", i), call_site())); if !skip(ty) { - let expr = q!( - Vars { - binding_ident: &binding_ident - }, - { binding_ident } - ) - .parse(); + let expr: Expr = parse_quote!(#binding_ident); let ast_path = if use_ast_path { Some(( @@ -2272,21 +2022,12 @@ fn make_arm_from_struct( None }; - let expr = visit_expr(mode, ty, &q!({ _visitor }).parse(), expr, ast_path); + let expr = visit_expr(mode, ty, &parse_quote!(_visitor), expr, ast_path); stmts.push(match mode { Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => { Stmt::Expr(expr, Some(Token![;](call_site()))) } - Mode::Fold { .. } => q!( - Vars { - name: &binding_ident, - expr - }, - { - let name = expr; - } - ) - .parse(), + Mode::Fold { .. } => parse_quote!(let #binding_ident = #expr;), }); } @@ -2314,19 +2055,7 @@ fn make_arm_from_struct( match mode { Mode::Fold { .. } => { // Append return statement - stmts.push( - q!( - Vars { - Path: &path, - fields: &fields - }, - { - // - return Path { fields }; - } - ) - .parse(), - ) + stmts.push(parse_quote!(return #path { #fields };)) } Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => {} } @@ -2366,44 +2095,44 @@ fn method_sig(mode: Mode, ty: &Type) -> Signature { fn_token: Default::default(), ident: method_name(mode, ty), generics: if let Mode::Visit(VisitorVariant::WithPath) = mode { - q!({<'ast: 'r, 'r>}).parse() + parse_quote!(<'ast: 'r, 'r>) } else { Default::default() }, paren_token: Default::default(), inputs: { let mut p = Punctuated::default(); - p.push_value(q!(Vars {}, { &mut self }).parse()); + p.push_value(parse_quote!(&mut self)); p.push_punct(Token![,](def_site())); match mode { Mode::Fold { .. } => { - p.push_value(q!(Vars { Type: ty }, { n: Type }).parse()); + p.push_value(parse_quote!(n: #ty)); } Mode::VisitMut { .. } => { - p.push_value(q!(Vars { Type: ty }, { n: &mut Type }).parse()); + p.push_value(parse_quote!(n: &mut #ty)); } Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - p.push_value(q!(Vars { Type: ty }, { n: &Type }).parse()); + p.push_value(parse_quote!(n: &#ty)); } Mode::Visit(VisitorVariant::WithPath) => { - p.push_value(q!(Vars { Type: ty }, { n: &'ast Type }).parse()); + p.push_value(parse_quote!(n: &'ast #ty)); } } if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { p.push_punct(Token![,](def_site())); let ty = ast_path_type(mode); - p.push_value(q!(Vars { Type: ty }, { __ast_path: Type }).parse()); + p.push_value(parse_quote!(__ast_path: #ty)); } p }, variadic: None, output: match mode { - Mode::Fold { .. } => q!(Vars { ty }, { -> ty }).parse(), + Mode::Fold { .. } => parse_quote!(-> #ty), _ => ReturnType::Default, }, } @@ -2465,12 +2194,12 @@ fn make_method(mode: Mode, e: &Item, types: &mut Vec) -> Option) -> Option) -> Option Signature { fn_token: Default::default(), ident, generics: if let Mode::Visit(VisitorVariant::WithPath) = mode { - q!({<'ast: 'r, 'r>}).parse() + parse_quote!(<'ast: 'r, 'r>) } else { Default::default() }, paren_token: Default::default(), inputs: { let mut p = Punctuated::default(); - p.push_value(q!(Vars {}, { &mut self }).parse()); + p.push_value(parse_quote!(&mut self)); p.push_punct(Token![,](def_site())); - p.push_value(q!(Vars { Type: ty }, { n: Type }).parse()); + p.push_value(parse_quote!(n: #ty)); if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { p.push_punct(Token![,](def_site())); - p.push_value( - q!( - Vars { - Type: ast_path_type(mode) - }, - { __ast_path: Type } - ) - .parse(), - ); + let ty = ast_path_type(mode); + p.push_value(parse_quote!(__ast_path: #ty)); } p }, variadic: None, output: match mode { - Mode::Fold { .. } => q!(Vars { ty }, { -> ty }).parse(), + Mode::Fold { .. } => parse_quote!(-> #ty), _ => ReturnType::Default, }, } @@ -2617,7 +2335,7 @@ fn create_method_sig(mode: Mode, ty: &Type) -> Signature { fn mk_ref(mode: Mode, ident: Ident, ty: &Type, mutable: bool) -> Signature { if let Mode::Visit(VisitorVariant::WithPath) = mode { - return mk_exact(mode, ident, &q!(Vars { ty }, (&'ast ty)).parse()); + return mk_exact(mode, ident, &parse_quote!(&'ast #ty)); } mk_exact( @@ -2650,7 +2368,7 @@ fn create_method_sig(mode: Mode, ty: &Type) -> Signature { let ident = method_name(mode, ty); if !last.arguments.is_empty() { - if let Some(arg) = as_box(ty) { + if let Some(arg) = extract_box(ty) { let ident = method_name(mode, arg); match mode { Mode::Fold { .. } => { @@ -2673,64 +2391,36 @@ fn create_method_sig(mode: Mode, ty: &Type) -> Signature { if let Some(item) = extract_generic("Vec", arg) { match mode { Mode::Fold { .. } => { - return mk_exact( - mode, - ident, - &q!(Vars { item }, { Option> }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Option>)); } Mode::VisitMut { .. } => { return mk_exact( mode, ident, - &q!(Vars { item }, { &mut Option> }).parse(), + &parse_quote!(&mut Option>), ); } Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - return mk_exact( - mode, - ident, - &q!(Vars { item }, { Option<& [item]> }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Option<&[#item]>)); } Mode::Visit(VisitorVariant::WithPath) => { - return mk_exact( - mode, - ident, - &q!(Vars { item }, { Option<&'ast [item]> }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Option<&'ast [#item]>)); } } } match mode { Mode::Fold { .. } => { - return mk_exact( - mode, - ident, - &q!(Vars { arg }, { Option }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Option<#arg>)); } Mode::VisitMut { .. } => { - return mk_exact( - mode, - ident, - &q!(Vars { arg }, { &mut Option }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(&mut Option<#arg>)); } Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - return mk_exact( - mode, - ident, - &q!(Vars { arg }, { Option<& arg> }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Option<&#arg>)); } Mode::Visit(VisitorVariant::WithPath) => { - return mk_exact( - mode, - ident, - &q!(Vars { arg }, { Option<&'ast arg> }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Option<&'ast #arg>)); } } } @@ -2746,17 +2436,13 @@ fn create_method_sig(mode: Mode, ty: &Type) -> Signature { match mode { Mode::Fold { .. } => { - return mk_exact( - mode, - ident, - &q!(Vars { arg }, { Vec }).parse(), - ); + return mk_exact(mode, ident, &parse_quote!(Vec<#arg>)); } Mode::VisitMut { .. } => { return mk_ref( mode, ident, - &q!(Vars { arg }, { Vec }).parse(), + &parse_quote!(Vec<#arg>), true, ); } @@ -2764,7 +2450,7 @@ fn create_method_sig(mode: Mode, ty: &Type) -> Signature { return mk_ref( mode, ident, - &q!(Vars { arg }, { [arg] }).parse(), + &parse_quote!([#arg]), false, ); } @@ -2799,10 +2485,7 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { match mode { Mode::Visit(..) | Mode::VisitAll => { let visit = method_name(mode, ty); - let visit = inject_ast_path_arg_if_required( - mode, - q!(Vars { visit }, { _visitor.visit(n) }).parse(), - ); + let visit = inject_ast_path_arg_if_required(mode, parse_quote!(_visitor.#visit(n))); return Block { brace_token: Default::default(), @@ -2815,7 +2498,7 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { stmts: vec![], } } - Mode::Fold { .. } => return q!(({ n })).parse(), + Mode::Fold { .. } => return parse_quote!({ n }), } } @@ -2832,20 +2515,16 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { let last = p.path.segments.last().unwrap(); if !last.arguments.is_empty() { - if let Some(arg) = as_box(ty) { + if let Some(arg) = extract_box(ty) { match mode { Mode::Fold(..) => { let ident = method_name(mode, arg); let inner = inject_ast_path_arg_if_required( mode, - q!(Vars { ident }, { _visitor.ident(*n) }).parse(), + parse_quote!(_visitor.#ident(*n)), ); - return q!( - Vars { inner }, - ({ swc_visit::util::map::Map::map(n, |n| inner) }) - ) - .parse(); + return parse_quote!(swc_visit::util::map::Map::map(n, |n| #inner)); } Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => { @@ -2864,27 +2543,22 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { let ident = method_name(mode, arg); if let Mode::Fold(..) = mode { - if as_box(arg).is_some() { + if extract_box(arg).is_some() { let inner = inject_ast_path_arg_if_required( mode, - q!(Vars { ident }, { _visitor.ident(n) }).parse(), + parse_quote!(_visitor.#ident(n)), ); - return q!( - Vars { inner }, - ({ - match n { - Some(n) => { - Some(swc_visit::util::map::Map::map( - n, - |n| inner, - )) - } - None => None, - } - }) - ) - .parse(); + return parse_quote!({ + match n { + Some(n) => Some( + swc_visit::util::map::Map::map(n, |n| { + #inner + }), + ), + None => None, + } + }); } } @@ -2892,37 +2566,29 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { Mode::Fold(..) => { let inner = inject_ast_path_arg_if_required( mode, - q!(Vars { ident }, { _visitor.ident(n) }).parse(), + parse_quote!(_visitor.#ident(n)), ); - q!( - Vars { inner }, - ({ - match n { - Some(n) => Some(inner), - None => None, - } - }) - ) - .parse() + parse_quote!({ + match n { + Some(n) => Some(#inner), + None => None, + } + }) } Mode::VisitMut(..) | Mode::Visit(..) | Mode::VisitAll => { let inner = inject_ast_path_arg_if_required( mode, - q!(Vars { ident }, { _visitor.ident(n) }).parse(), + parse_quote!(_visitor.#ident(n)), ); - q!( - Vars { inner }, - ({ - match n { - Some(n) => inner, - None => {} - } - }) - ) - .parse() + parse_quote!({ + match n { + Some(n) => #inner, + None => {} + } + }) } }; } @@ -2938,36 +2604,28 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { match mode { Mode::Fold(v) => { - if as_box(arg).is_some() { + if extract_box(arg).is_some() { return match v { - VisitorVariant::Normal => q!( - Vars { ident }, - ({ - swc_visit::util::move_map::MoveMap::move_map(n, |v| { - swc_visit::util::map::Map::map(v, |v| { - _visitor.ident(v) - }) + VisitorVariant::Normal => parse_quote!({ + swc_visit::util::move_map::MoveMap::move_map(n, |v| { + swc_visit::util::map::Map::map(v, |v| { + _visitor.#ident(v) }) }) - ) - .parse(), - VisitorVariant::WithPath => q!( - Vars { ident }, - ({ - n.into_iter() - .enumerate() - .map(|(idx, v)| { - let mut __ast_path = - __ast_path.with_index_guard(idx); - - swc_visit::util::map::Map::map(v, |v| { - _visitor.ident(v, &mut *__ast_path) - }) + }), + VisitorVariant::WithPath => parse_quote!({ + n.into_iter() + .enumerate() + .map(|(idx, v)| { + let mut __ast_path = + __ast_path.with_index_guard(idx); + + swc_visit::util::map::Map::map(v, |v| { + _visitor.#ident(v, &mut *__ast_path) }) - .collect() - }) - ) - .parse(), + }) + .collect() + }), }; } } @@ -2976,136 +2634,98 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block { return if is_option(arg) { match mode { - Mode::Fold(VisitorVariant::Normal) => q!( - Vars { ident }, - ({ - swc_visit::util::move_map::MoveMap::move_map(n, |v| { - _visitor.ident(v) - }) + Mode::Fold(VisitorVariant::Normal) => parse_quote!({ + swc_visit::util::move_map::MoveMap::move_map(n, |v| { + _visitor.#ident(v) }) - ) - .parse(), - - Mode::Fold(VisitorVariant::WithPath) => q!( - Vars { ident }, - ({ - n.into_iter() - .enumerate() - .map(|(idx, v)| { - let mut __ast_path = __ast_path.with_index_guard(idx); - - _visitor.ident(v, &mut *__ast_path) - }) - .collect() - }) - ) - .parse(), - - Mode::VisitMut(VisitorVariant::Normal) => q!( - Vars { ident }, - ({ n.iter_mut().for_each(|v| _visitor.ident(v)) }) - ) - .parse(), - - Mode::VisitMut(VisitorVariant::WithPath) => q!( - Vars { ident }, - ({ - n.iter_mut().enumerate().for_each(|(idx, v)| { + }), + + Mode::Fold(VisitorVariant::WithPath) => parse_quote!({ + n.into_iter() + .enumerate() + .map(|(idx, v)| { let mut __ast_path = __ast_path.with_index_guard(idx); - _visitor.ident(v, &mut *__ast_path) + _visitor.#ident(v, &mut *__ast_path) }) + .collect() + }), + + Mode::VisitMut(VisitorVariant::Normal) => { + parse_quote!({ n.iter_mut().for_each(|v| _visitor.#ident(v)) }) + } + + Mode::VisitMut(VisitorVariant::WithPath) => parse_quote!({ + n.iter_mut().enumerate().for_each(|(idx, v)| { + let mut __ast_path = __ast_path.with_index_guard(idx); + + _visitor.#ident(v, &mut *__ast_path) }) - ) - .parse(), - - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => q!( - Vars { ident }, - ({ n.iter().for_each(|v| _visitor.ident(v.as_ref())) }) - ) - .parse(), - - Mode::Visit(VisitorVariant::WithPath) => q!( - Vars { ident }, - ({ - n.iter().enumerate().for_each(|(idx, v)| { - let mut __ast_path = __ast_path.with_index_guard(idx); + }), - _visitor.ident(v.as_ref(), &mut *__ast_path) - }) + Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { + parse_quote!({ n.iter().for_each(|v| _visitor.#ident(v.as_ref())) }) + } + + Mode::Visit(VisitorVariant::WithPath) => parse_quote!({ + n.iter().enumerate().for_each(|(idx, v)| { + let mut __ast_path = __ast_path.with_index_guard(idx); + + _visitor.#ident(v.as_ref(), &mut *__ast_path) }) - ) - .parse(), + }), } } else { match mode { - Mode::Fold(VisitorVariant::Normal) => q!( - Vars { ident }, - ({ - swc_visit::util::move_map::MoveMap::move_map(n, |v| { - _visitor.ident(v) - }) + Mode::Fold(VisitorVariant::Normal) => parse_quote!({ + swc_visit::util::move_map::MoveMap::move_map(n, |v| { + _visitor.#ident(v) }) - ) - .parse(), - - Mode::Fold(VisitorVariant::WithPath) => q!( - Vars { ident }, - ({ - n.into_iter() - .enumerate() - .map(|(idx, v)| { - let mut __ast_path = __ast_path.with_index_guard(idx); - _visitor.ident(v, &mut *__ast_path) - }) - .collect() - }) - ) - .parse(), - - Mode::VisitMut(VisitorVariant::Normal) => q!( - Vars { ident }, - ({ n.iter_mut().for_each(|v| _visitor.ident(v)) }) - ) - .parse(), - - Mode::VisitMut(VisitorVariant::WithPath) => q!( - Vars { ident }, - ({ - n.iter_mut().enumerate().for_each(|(idx, v)| { - let mut __ast_path = __ast_path.with_index_guard(idx); + }), - _visitor.ident(v, &mut *__ast_path) + Mode::Fold(VisitorVariant::WithPath) => parse_quote!({ + n.into_iter() + .enumerate() + .map(|(idx, v)| { + let mut __ast_path = __ast_path.with_index_guard(idx); + _visitor.#ident(v, &mut *__ast_path) }) + .collect() + }), + + Mode::VisitMut(VisitorVariant::Normal) => { + parse_quote!({ n.iter_mut().for_each(|v| _visitor.#ident(v)) }) + } + + Mode::VisitMut(VisitorVariant::WithPath) => parse_quote!({ + n.iter_mut().enumerate().for_each(|(idx, v)| { + let mut __ast_path = __ast_path.with_index_guard(idx); + + _visitor.#ident(v, &mut *__ast_path) }) - ) - .parse(), - - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => q!( - Vars { ident }, - ({ n.iter().for_each(|v| _visitor.ident(v)) }) - ) - .parse(), - - Mode::Visit(VisitorVariant::WithPath) => q!( - Vars { ident }, - ({ + }), + + Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { + parse_quote!({ n.iter().for_each(|v| _visitor.#ident(v)) }) + } + + Mode::Visit(VisitorVariant::WithPath) => { + parse_quote!({ n.iter().enumerate().for_each(|(idx, v)| { let mut __ast_path = __ast_path.with_index_guard(idx); - _visitor.ident(v, &mut *__ast_path) + _visitor.#ident(v, &mut *__ast_path) }) }) - ) - .parse(), + } } }; } } match mode { - Mode::Fold { .. } => q!(({ return n })).parse(), - Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => q!(({})).parse(), + Mode::Fold { .. } => parse_quote!({ return n }), + Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => parse_quote!({}), } } Type::Ptr(_) => unimplemented!("type: pointer"), @@ -3147,7 +2767,7 @@ fn is_option(ty: &Type) -> bool { false } -fn as_box(ty: &Type) -> Option<&Type> { +fn extract_box(ty: &Type) -> Option<&Type> { extract_generic("Box", ty) } @@ -3219,8 +2839,8 @@ fn method_name_as_str(mode: Mode, ty: &Type) -> String { fn ast_path_type(mode: Mode) -> Type { match mode { - Mode::Visit(_) => q!((&mut AstNodePath<'r>)).parse(), - Mode::VisitMut(_) | Mode::Fold(_) => q!((&mut AstKindPath)).parse(), + Mode::Visit(_) => parse_quote!(&mut AstNodePath<'r>), + Mode::VisitMut(_) | Mode::Fold(_) => parse_quote!(&mut AstKindPath), _ => unreachable!(), } } @@ -3286,7 +2906,7 @@ fn inject_ast_path_arg_if_required(mode: Mode, mut visit_expr: ExprMethodCall) - _ => return Expr::MethodCall(visit_expr), } - visit_expr.args.push(q!((__ast_path)).parse()); + visit_expr.args.push(parse_quote!(__ast_path)); Expr::MethodCall(visit_expr) }