diff --git a/.changeset/cyan-dolls-appear.md b/.changeset/cyan-dolls-appear.md new file mode 100644 index 000000000000..e9a0635e5e5c --- /dev/null +++ b/.changeset/cyan-dolls-appear.md @@ -0,0 +1,8 @@ +--- +swc_ecma_visit: patch +swc_css_visit: patch +swc_html_visit: patch +swc_xml_visit: patch +--- + +perf(visit): Modify `Box` and `Vec` in-place diff --git a/Cargo.lock b/Cargo.lock index 68d9a562187f..6627742d3109 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5325,18 +5325,6 @@ name = "swc_visit" version = "0.6.0" dependencies = [ "either", - "swc_visit_macros", -] - -[[package]] -name = "swc_visit_macros" -version = "0.5.13" -dependencies = [ - "Inflector", - "proc-macro2", - "quote", - "swc_macros_common", - "syn 2.0.55", ] [[package]] diff --git a/crates/swc_css_visit/src/generated.rs b/crates/swc_css_visit/src/generated.rs index 96fcc0a2b7ad..ff0e25efb69f 100644 --- a/crates/swc_css_visit/src/generated.rs +++ b/crates/swc_css_visit/src/generated.rs @@ -84483,9 +84483,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84495,9 +84495,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84507,9 +84507,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84519,9 +84519,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84531,9 +84531,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84543,9 +84543,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84555,9 +84555,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84567,9 +84567,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84579,9 +84579,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84591,9 +84591,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84603,9 +84603,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84615,9 +84615,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84627,9 +84627,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84639,9 +84639,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84651,9 +84651,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84663,9 +84663,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84675,9 +84675,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84687,9 +84687,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Option> { @@ -84983,9 +84983,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -84995,9 +84995,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -85007,9 +85007,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -85019,9 +85019,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -85031,9 +85031,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -85043,9 +85043,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for swc_common::Span { @@ -85065,9 +85065,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -85077,9 +85077,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -85089,9 +85089,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for std::boxed::Box @@ -85100,15 +85100,13 @@ where T: FoldWith, { fn fold_with(self, visitor: &mut V) -> Self { - let v = >::fold_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| >::fold_with(inner, visitor)) } fn fold_children_with(self, visitor: &mut V) -> Self { - let v = >::fold_children_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with(inner, visitor) + }) } } #[doc = r" A visitor trait for traversing the AST."] @@ -106050,13 +106048,11 @@ impl FoldWithAstPath for Vec } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106068,13 +106064,11 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106086,13 +106080,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106104,13 +106096,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106122,13 +106110,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106140,13 +106124,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106158,13 +106138,11 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106176,11 +106154,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106193,13 +106169,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106211,11 +106185,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106227,13 +106199,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106245,13 +106215,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106263,11 +106231,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106279,13 +106245,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106297,11 +106259,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106313,13 +106273,11 @@ impl FoldWithAstPath for Vec } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106331,13 +106289,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106349,11 +106305,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106785,13 +106739,11 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106803,11 +106755,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106819,13 +106769,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106838,13 +106786,11 @@ impl FoldWithAstPath for Vec Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106856,13 +106802,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106874,9 +106816,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| >::fold_with_ast_path(item, visitor, __ast_path)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106900,13 +106842,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106918,13 +106856,11 @@ impl FoldWithAstPath for Vec } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106936,11 +106872,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -106951,15 +106885,15 @@ where T: FoldWithAstPath, { fn fold_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_with_ast_path(inner, visitor, __ast_path) + }) } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_children_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with_ast_path(inner, visitor, __ast_path) + }) } } #[doc = r" A visitor trait for traversing the AST."] diff --git a/crates/swc_ecma_visit/src/generated.rs b/crates/swc_ecma_visit/src/generated.rs index 0f15b949a439..0f1d84403a50 100644 --- a/crates/swc_ecma_visit/src/generated.rs +++ b/crates/swc_ecma_visit/src/generated.rs @@ -107593,9 +107593,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107605,9 +107605,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107617,9 +107617,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107629,9 +107629,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec> { @@ -107641,9 +107641,9 @@ impl FoldWith for Vec> { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| as FoldWith>::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWith>::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107653,9 +107653,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107665,9 +107665,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107677,9 +107677,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107689,9 +107689,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107701,9 +107701,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107713,9 +107713,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Option { @@ -107975,9 +107975,9 @@ impl FoldWith for Vec> { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| as FoldWith>::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWith>::fold_with(item, visitor) + }) } } impl FoldWith for Vec> { @@ -107987,9 +107987,9 @@ impl FoldWith for Vec> { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| as FoldWith>::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWith>::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -107999,9 +107999,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108011,9 +108011,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108023,9 +108023,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108035,9 +108035,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for swc_common::Span { @@ -108057,9 +108057,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108069,9 +108069,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for swc_common::SyntaxContext { @@ -108091,9 +108091,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108103,9 +108103,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108115,9 +108115,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108127,9 +108127,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108139,9 +108139,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108151,9 +108151,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108163,9 +108163,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec> { @@ -108175,9 +108175,9 @@ impl FoldWith for Vec> { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| as FoldWith>::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWith>::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -108187,9 +108187,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for std::boxed::Box @@ -108198,15 +108198,13 @@ where T: FoldWith, { fn fold_with(self, visitor: &mut V) -> Self { - let v = >::fold_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| >::fold_with(inner, visitor)) } fn fold_children_with(self, visitor: &mut V) -> Self { - let v = >::fold_children_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with(inner, visitor) + }) } } #[doc = r" A visitor trait for traversing the AST."] @@ -134961,11 +134959,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -134977,11 +134973,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -134993,13 +134987,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135011,11 +135001,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135027,11 +135015,9 @@ impl FoldWithAstPath for Vec> { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - as FoldWithAstPath>::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWithAstPath>::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135043,13 +135029,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135061,13 +135043,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135079,13 +135057,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135097,13 +135071,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135115,11 +135085,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135131,11 +135099,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135505,13 +135471,11 @@ impl FoldWithAstPath for Vec> { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - as FoldWithAstPath>::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWithAstPath>::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135523,11 +135487,9 @@ impl FoldWithAstPath for Vec> { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - as FoldWithAstPath>::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWithAstPath>::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135539,13 +135501,11 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135557,11 +135517,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135573,9 +135531,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| >::fold_with_ast_path(item, visitor, __ast_path)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135587,11 +135545,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135615,9 +135571,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| >::fold_with_ast_path(item, visitor, __ast_path)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135629,11 +135585,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135657,11 +135611,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135673,11 +135625,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135689,13 +135639,11 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path( + item, visitor, __ast_path, + ) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135707,11 +135655,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135723,13 +135669,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135741,11 +135683,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135757,11 +135697,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135773,11 +135711,9 @@ impl FoldWithAstPath for Vec> { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - as FoldWithAstPath>::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + as FoldWithAstPath>::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135789,11 +135725,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -135804,15 +135738,15 @@ where T: FoldWithAstPath, { fn fold_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_with_ast_path(inner, visitor, __ast_path) + }) } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_children_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with_ast_path(inner, visitor, __ast_path) + }) } } #[doc = r" A visitor trait for traversing the AST."] diff --git a/crates/swc_html_visit/src/generated.rs b/crates/swc_html_visit/src/generated.rs index 6de858172e40..4e229ff7f499 100644 --- a/crates/swc_html_visit/src/generated.rs +++ b/crates/swc_html_visit/src/generated.rs @@ -8440,9 +8440,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -8452,9 +8452,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -8464,9 +8464,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Option { @@ -8525,15 +8525,13 @@ where T: FoldWith, { fn fold_with(self, visitor: &mut V) -> Self { - let v = >::fold_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| >::fold_with(inner, visitor)) } fn fold_children_with(self, visitor: &mut V) -> Self { - let v = >::fold_children_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with(inner, visitor) + }) } } #[doc = r" A visitor trait for traversing the AST."] @@ -10536,13 +10534,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -10554,11 +10548,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -10570,11 +10562,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -10653,15 +10643,15 @@ where T: FoldWithAstPath, { fn fold_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_with_ast_path(inner, visitor, __ast_path) + }) } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_children_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with_ast_path(inner, visitor, __ast_path) + }) } } #[doc = r" A visitor trait for traversing the AST."] diff --git a/crates/swc_visit/Cargo.toml b/crates/swc_visit/Cargo.toml index b357b1122bee..8c9172f8dbea 100644 --- a/crates/swc_visit/Cargo.toml +++ b/crates/swc_visit/Cargo.toml @@ -15,5 +15,3 @@ path = [] [dependencies] either = { workspace = true } - -swc_visit_macros = { version = "0.5.12", path = "../swc_visit_macros" } diff --git a/crates/swc_visit/src/lib.rs b/crates/swc_visit/src/lib.rs index 2b8ca3bcc73b..b1faa036d416 100644 --- a/crates/swc_visit/src/lib.rs +++ b/crates/swc_visit/src/lib.rs @@ -95,7 +95,6 @@ use std::ops::{Deref, DerefMut}; pub use either::Either; -pub use swc_visit_macros::define; pub mod util; diff --git a/crates/swc_visit/tests/arc.rs b/crates/swc_visit/tests/arc.rs deleted file mode 100644 index 9a463354cff1..000000000000 --- a/crates/swc_visit/tests/arc.rs +++ /dev/null @@ -1,49 +0,0 @@ -#![allow(clippy::ptr_arg)] - -use std::sync::Arc; - -use swc_visit::define; - -#[derive(Debug, PartialEq)] -pub struct Item { - pub item: Option>, - pub ref_to_enum: Option>, -} -#[derive(Debug, PartialEq)] -pub enum Enum { - Item(Arc), - Items(Arc>), - Enum(Arc), - Enums(Arc>), -} - -define!({ - pub struct Item { - pub item: Option>, - pub ref_to_enum: Option>, - } - pub enum Enum { - Item(Arc), - Items(Arc>), - Enum(Arc), - Enums(Arc>), - } -}); - -struct Panic; - -impl Visit for Panic { - fn visit_item(&mut self, _: &Item) { - panic!("Success") - } -} - -#[test] -#[should_panic(expected = "Success")] -fn test_panic() { - Enum::Item(Arc::new(Item { - item: None, - ref_to_enum: None, - })) - .visit_children_with(&mut Panic) -} diff --git a/crates/swc_visit/tests/fold.rs b/crates/swc_visit/tests/fold.rs deleted file mode 100644 index 8e9d1b1c2cc7..000000000000 --- a/crates/swc_visit/tests/fold.rs +++ /dev/null @@ -1,40 +0,0 @@ -#![allow(clippy::ptr_arg)] - -use std::any::Any; - -use swc_visit::define; - -/// Visitable nodes. -pub trait Node: Any {} - -impl Node for T where T: Any {} - -#[derive(Debug, PartialEq)] -pub struct Item { - // pub field: usize, - // pub inner: Option>, - pub opt_vec: Option>, - pub vec_opt: Vec>, - - pub value: f64, -} -#[derive(Debug, PartialEq)] -pub enum Enum { - Item(Item), - Boxed(Box), -} - -define!({ - pub struct Item { - // pub field: usize, - // pub inner: Option>, - pub opt_vec: Option>, - pub vec_opt: Vec>, - - pub value: f64, - } - pub enum Enum { - Item(Item), - Boxed(Box), - } -}); diff --git a/crates/swc_visit/tests/opt_vec.rs b/crates/swc_visit/tests/opt_vec.rs deleted file mode 100644 index 65a8007a13a5..000000000000 --- a/crates/swc_visit/tests/opt_vec.rs +++ /dev/null @@ -1,30 +0,0 @@ -#![allow(clippy::ptr_arg)] - -use std::any::Any; - -use swc_visit::define; - -/// Visitable nodes. -pub trait Node: Any {} - -impl Node for T where T: Any {} - -#[derive(Debug, PartialEq)] -pub struct Item { - pub opt_vec1: Option>, - pub opt_vec2: Option>, -} -#[derive(Debug, PartialEq)] -pub enum Enum { - Item(Item), -} - -define!({ - pub struct Item { - pub opt_vec1: Option>, - pub opt_vec2: Option>, - } - pub enum Enum { - Item(Item), - } -}); diff --git a/crates/swc_visit/tests/vec_opt.rs b/crates/swc_visit/tests/vec_opt.rs deleted file mode 100644 index 8b1e03b0f886..000000000000 --- a/crates/swc_visit/tests/vec_opt.rs +++ /dev/null @@ -1,23 +0,0 @@ -#![allow(clippy::ptr_arg)] - -use swc_visit::define; - -#[derive(Debug, PartialEq)] -pub struct Item { - pub vec_opt1: Vec>, - pub vec_opt2: Vec>, -} -#[derive(Debug, PartialEq)] -pub enum Enum { - Item(Item), -} - -define!({ - pub struct Item { - pub vec_opt1: Vec>, - pub vec_opt2: Vec>, - } - pub enum Enum { - Item(Item), - } -}); diff --git a/crates/swc_visit_macros/Cargo.toml b/crates/swc_visit_macros/Cargo.toml deleted file mode 100644 index 9cd1de8162f5..000000000000 --- a/crates/swc_visit_macros/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -authors = ["강동윤 "] -description = "Visitor generator for stable rustc" -edition = "2021" -license = "Apache-2.0" -name = "swc_visit_macros" -repository = "https://github.com/swc-project/swc.git" -version = "0.5.13" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -bench = false -proc-macro = true - -[dependencies] -Inflector = { workspace = true } -proc-macro2 = { workspace = true } -quote = { workspace = true } -syn = { workspace = true, features = ["parsing", "full"] } - -swc_macros_common = { version = "0.3.11", path = "../swc_macros_common" } diff --git a/crates/swc_visit_macros/src/lib.rs b/crates/swc_visit_macros/src/lib.rs deleted file mode 100644 index a5f769f17fdb..000000000000 --- a/crates/swc_visit_macros/src/lib.rs +++ /dev/null @@ -1,2969 +0,0 @@ -#![allow(non_snake_case)] - -extern crate proc_macro; - -use std::{collections::HashSet, mem::replace}; - -use inflector::Inflector; -use proc_macro2::{Ident, TokenStream}; -use quote::{quote, ToTokens}; -use swc_macros_common::{call_site, def_site, make_doc_attr}; -use syn::{ - parse_macro_input, parse_quote, - punctuated::Punctuated, - spanned::Spanned, - token::{Brace, Paren}, - Arm, AttrStyle, Attribute, Block, Expr, ExprBlock, ExprCall, ExprMatch, ExprMethodCall, - ExprPath, ExprUnary, Field, FieldMutability, FieldPat, Fields, FieldsUnnamed, FnArg, - GenericArgument, GenericParam, Generics, ImplItem, ImplItemFn, Index, Item, ItemEnum, ItemImpl, - ItemMod, ItemStruct, ItemTrait, ItemUse, Lifetime, LifetimeParam, Member, Pat, PatIdent, - PatStruct, PatTupleStruct, PatType, PatWild, Path, PathArguments, ReturnType, Signature, Stmt, - Token, TraitItem, TraitItemFn, Type, TypePath, TypeReference, UnOp, UseTree, Variant, - Visibility, -}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum VisitorVariant { - Normal, - WithPath, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Mode { - VisitAll, - Visit(VisitorVariant), - VisitMut(VisitorVariant), - Fold(VisitorVariant), -} - -impl Mode { - fn trait_name(self) -> &'static str { - match self { - Mode::VisitAll => "VisitAll", - Mode::Fold(VisitorVariant::Normal) => "Fold", - Mode::Visit(VisitorVariant::Normal) => "Visit", - Mode::VisitMut(VisitorVariant::Normal) => "VisitMut", - Mode::Visit(VisitorVariant::WithPath) => "VisitAstPath", - Mode::VisitMut(VisitorVariant::WithPath) => "VisitMutAstPath", - Mode::Fold(VisitorVariant::WithPath) => "FoldAstPath", - } - } - - fn prefix(self) -> &'static str { - match self { - Mode::Fold { .. } => "fold", - Mode::Visit { .. } | Mode::VisitAll => "visit", - Mode::VisitMut { .. } => "visit_mut", - } - } - - fn visitor_variant(self) -> Option { - match self { - Mode::Fold(v) | Mode::Visit(v) | Mode::VisitMut(v) => Some(v), - Mode::VisitAll => None, - } - } - - fn name_of_trait_for_ast(self) -> Option<&'static str> { - Some(match self { - Mode::VisitAll => return None, - Mode::Fold(VisitorVariant::Normal) => "FoldWith", - Mode::Visit(VisitorVariant::Normal) => "VisitWith", - Mode::VisitMut(VisitorVariant::Normal) => "VisitMutWith", - Mode::Visit(VisitorVariant::WithPath) => "VisitWithPath", - Mode::VisitMut(VisitorVariant::WithPath) => "VisitMutWithPath", - Mode::Fold(VisitorVariant::WithPath) => "FoldWithPath", - }) - } - - fn name_of_trait_children_method_for_ast(self) -> Option<&'static str> { - Some(match self { - Mode::VisitAll => return None, - Mode::Fold(VisitorVariant::Normal) => "fold_children_with", - Mode::Fold(VisitorVariant::WithPath) => "fold_children_with_path", - - Mode::Visit(VisitorVariant::Normal) => "visit_children_with", - Mode::Visit(VisitorVariant::WithPath) => "visit_children_with_path", - - Mode::VisitMut(VisitorVariant::Normal) => "visit_mut_children_with", - Mode::VisitMut(VisitorVariant::WithPath) => "visit_mut_children_with_path", - }) - } - - fn call_method(self, visitor: TokenStream, arg: TokenStream, method: &Ident) -> ExprCall { - let trait_name = Ident::new(self.trait_name(), def_site()); - - match self.visitor_variant() { - Some(VisitorVariant::WithPath) => { - parse_quote!(#trait_name::#method(#visitor, #arg, __ast_path)) - } - - Some(VisitorVariant::Normal) | None => { - parse_quote!(#trait_name::#method(#visitor, #arg)) - } - } - } -} - -/// This creates `Visit`. This is extensible visitor generator, and it -/// -/// - works with stable rustc -/// -/// - highly extensible and used to create Visitor for any types -/// -/// - create `Visit`, `VisitAll`, `VisitMut`, `Fold` -#[proc_macro] -pub fn define(tts: proc_macro::TokenStream) -> proc_macro::TokenStream { - let block = parse_macro_input!(tts as Block); - - let mut q = quote!(); - q.extend(quote!( - use ::swc_visit::ParentKind; - - pub type AstKindPath = ::swc_visit::AstKindPath; - pub type AstNodePath<'ast> = ::swc_visit::AstNodePath>; - - /// Not a public API - #[doc(hidden)] - impl ::swc_visit::NodeRef for AstParentNodeRef<'_> { - type ParentKind = AstParentKind; - - #[inline] - fn kind(&self) -> Self::ParentKind { - self.kind() - } - - #[inline] - fn set_index(&mut self, index: usize) { - self.set_index(index) - } - } - )); - - let mut field_module_body = Vec::new(); - { - for stmts in block.stmts.iter() { - let item = match stmts { - Stmt::Item(item) => item, - _ => unimplemented!("error reporting for something other than Item"), - }; - - field_module_body.extend(make_field_enum(item)); - } - - q.extend(make_ast_enum(&block.stmts, true).into_token_stream()); - q.extend(make_ast_enum(&block.stmts, false).into_token_stream()); - - q.extend(make_impl_kind_for_node_ref(&block.stmts).into_token_stream()); - q.extend(make_impl_parent_kind(&block.stmts).into_token_stream()); - } - - q.extend(make(Mode::Fold(VisitorVariant::WithPath), &block.stmts)); - q.extend(make(Mode::Fold(VisitorVariant::Normal), &block.stmts)); - - q.extend(make(Mode::Visit(VisitorVariant::WithPath), &block.stmts)); - q.extend(make(Mode::Visit(VisitorVariant::Normal), &block.stmts)); - - q.extend(make(Mode::VisitMut(VisitorVariant::WithPath), &block.stmts)); - q.extend(make(Mode::VisitMut(VisitorVariant::Normal), &block.stmts)); - - q.extend(make(Mode::VisitAll, &block.stmts)); - - q.extend( - ItemMod { - attrs: vec![make_doc_attr( - "This module contains enums representing fields of each types", - )], - vis: Visibility::Public(Token![pub](def_site())), - mod_token: Default::default(), - ident: Ident::new("fields", call_site()), - content: Some((Default::default(), field_module_body)), - semi: None, - unsafety: None, - } - .into_token_stream(), - ); - - q.into() -} - -fn make_field_enum_variant_from_named_field(type_name: &Ident, f: &Field) -> Variant { - let fields = if is_vec_or_opt_vec(&f.ty) { - let mut v = Punctuated::new(); - - v.push(Field { - attrs: Default::default(), - vis: Visibility::Inherited, - ident: None, - colon_token: None, - ty: parse_quote!(usize), - mutability: FieldMutability::None, - }); - - Fields::Unnamed(FieldsUnnamed { - paren_token: Paren(f.span()), - unnamed: v, - }) - } else { - Fields::Unit - }; - - let field_name = f.ident.as_ref().unwrap(); - let doc_attr = make_doc_attr(&format!( - "This represents [{field_name}](`crate::{type_name}::{field_name}`)", - type_name = type_name, - field_name = field_name, - )); - Variant { - attrs: vec![doc_attr], - ident: Ident::new(&field_name.to_string().to_pascal_case(), f.ident.span()), - discriminant: Default::default(), - fields, - } -} - -fn make_field_enum(item: &Item) -> Vec { - let mut items = Vec::new(); - - let name = match item { - Item::Struct(s) => s.ident.clone(), - Item::Enum(e) => { - // Skip C-like enums - if e.variants.iter().all(|v| v.fields.is_empty()) { - return Vec::new(); - } - - e.ident.clone() - } - _ => return Vec::new(), - }; - - let name = Ident::new(&format!("{}Field", name), name.span()); - { - let mut attrs = Vec::new(); - - let variants = match item { - Item::Struct(s) => { - let mut v = Punctuated::new(); - - for f in s.fields.iter() { - if f.ident.is_none() { - continue; - } - - v.push(make_field_enum_variant_from_named_field(&s.ident, f)) - } - - v - } - Item::Enum(e) => { - let mut variants = Punctuated::new(); - - for v in e.variants.iter() { - let doc_attr = make_doc_attr(&format!( - "This represents [{variant_name}](`crate::{type_name}::{variant_name}`)", - type_name = e.ident, - variant_name = v.ident, - )); - - variants.push(Variant { - attrs: vec![doc_attr], - ident: Ident::new(&v.ident.to_string().to_pascal_case(), v.ident.span()), - discriminant: Default::default(), - fields: Fields::Unit, - }) - } - - variants - } - _ => return Vec::new(), - }; - - attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(derive( - Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash - )), - }); - - attrs.push(make_doc_attr(&format!( - "This enum represents fields of [{type_name}](crate::{type_name})", - type_name = name, - ))); - - attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(cfg_attr( - feature = "serde", - derive(serde::Serialize, serde::Deserialize), - )), - }); - - items.push(Item::Enum(ItemEnum { - attrs, - vis: Visibility::Public(Token![pub](def_site())), - enum_token: Default::default(), - ident: name.clone(), - generics: Default::default(), - brace_token: Default::default(), - variants, - })); - } - - { - let mut methods = Vec::new(); - - methods.push(ImplItem::Fn(ImplItemFn { - attrs: vec![make_doc_attr("This is not considered as a public API")], - vis: Visibility::Public(Token![pub](def_site())), - defaultness: Default::default(), - sig: Signature { - constness: Default::default(), - asyncness: Default::default(), - unsafety: Default::default(), - abi: Default::default(), - fn_token: Token![fn](name.span()), - ident: Ident::new("set_index", name.span()), - generics: Default::default(), - paren_token: Paren(name.span()), - inputs: { - let mut v = Punctuated::new(); - v.push(FnArg::Receiver(parse_quote!(&mut self))); - v.push(FnArg::Typed(PatType { - attrs: Default::default(), - colon_token: Default::default(), - ty: parse_quote!(usize), - pat: Box::new(Pat::Ident(PatIdent { - attrs: Default::default(), - by_ref: Default::default(), - mutability: Default::default(), - ident: Ident::new("index", call_site()), - subpat: Default::default(), - })), - })); - - v - }, - variadic: Default::default(), - output: ReturnType::Default, - }, - block: { - let mut arms = Vec::new(); - - if let Item::Struct(s) = item { - for f in s.fields.iter() { - if f.ident.is_none() { - continue; - } - - let variant_name = Ident::new( - &f.ident.as_ref().unwrap().to_string().to_pascal_case(), - f.ident.span(), - ); - - if is_vec_or_opt_vec(&f.ty) { - arms.push(Arm { - attrs: Default::default(), - pat: Pat::TupleStruct(PatTupleStruct { - attrs: Default::default(), - qself: None, - path: parse_quote!(Self::#variant_name), - paren_token: Paren(name.span()), - elems: { - let mut v = Punctuated::new(); - - v.push(Pat::Ident(PatIdent { - attrs: Default::default(), - by_ref: None, - mutability: None, - ident: Ident::new("idx", name.span()), - subpat: None, - })); - - v - }, - }), - guard: Default::default(), - fat_arrow_token: Token![=>](name.span()), - body: parse_quote!({ - debug_assert!( - *idx == usize::MAX || index == usize::MAX, - "Should be usize::MAX" - ); - *idx = index; - }), - comma: Some(Token![,](name.span())), - }); - } - } - }; - - arms.push(parse_quote!(_ => {})); - - let expr = Expr::Match(ExprMatch { - attrs: Default::default(), - match_token: Token![match](name.span()), - expr: parse_quote!(self), - brace_token: Brace(name.span()), - arms, - }); - - Block { - brace_token: Default::default(), - stmts: vec![Stmt::Expr(expr, None)], - } - }, - })); - - items.push(Item::Impl(ItemImpl { - attrs: Default::default(), - defaultness: Default::default(), - unsafety: Default::default(), - impl_token: Default::default(), - generics: Default::default(), - trait_: Default::default(), - self_ty: parse_quote!(#name), - brace_token: Default::default(), - items: methods, - })); - } - - items -} - -fn make_ast_enum(stmts: &[Stmt], is_ref: bool) -> Item { - let mut variants = Punctuated::new(); - - for stmt in stmts { - let item = match stmt { - Stmt::Item(item) => item, - _ => continue, - }; - let name = match item { - Item::Enum(ItemEnum { - ident, variants, .. - }) => { - // Skip C-like enums - if variants.iter().all(|v| v.fields.is_empty()) { - continue; - } - - ident - } - Item::Struct(ItemStruct { ident, .. }) => ident, - _ => continue, - }; - - let field_type_name = Ident::new(&format!("{}Field", name), name.span()); - - let fields = { - let mut fields = Punctuated::new(); - - if is_ref { - fields.push(Field { - attrs: Default::default(), - vis: Visibility::Inherited, - colon_token: None, - ident: None, - ty: Type::Reference(TypeReference { - and_token: Token![&](name.span()), - lifetime: Some(Lifetime { - apostrophe: def_site(), - ident: Ident::new("ast", name.span()), - }), - mutability: Default::default(), - elem: Box::new(Type::Path(TypePath { - qself: Default::default(), - path: name.clone().into(), - })), - }), - mutability: FieldMutability::None, - }); - } - - fields.push(Field { - attrs: Default::default(), - vis: Visibility::Inherited, - colon_token: None, - ident: None, - ty: Type::Path(TypePath { - qself: Default::default(), - path: parse_quote!(self::fields::#field_type_name), - }), - mutability: FieldMutability::None, - }); - - Fields::Unnamed(FieldsUnnamed { - paren_token: Default::default(), - unnamed: fields, - }) - }; - - variants.push(Variant { - attrs: Default::default(), - ident: name.clone(), - fields, - discriminant: None, - }); - } - let mut attrs = Vec::new(); - - attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(derive(Debug, Copy, Clone, PartialEq)), - }); - if !is_ref { - attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(derive(Eq, PartialOrd, Ord, Hash)), - }); - - attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(cfg_attr( - feature = "serde", - derive(serde::Serialize, serde::Deserialize), - )), - }); - } - attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(allow(clippy::derive_partial_eq_without_eq)), - }); - - Item::Enum(ItemEnum { - attrs, - vis: Visibility::Public(Token![pub](def_site())), - enum_token: Default::default(), - ident: if is_ref { - Ident::new("AstParentNodeRef", call_site()) - } else { - Ident::new("AstParentKind", call_site()) - }, - generics: if is_ref { - let mut g = Punctuated::new(); - g.push(GenericParam::Lifetime(LifetimeParam { - attrs: Default::default(), - lifetime: Lifetime { - apostrophe: def_site(), - ident: Ident::new("ast", def_site()), - }, - colon_token: Default::default(), - bounds: Default::default(), - })); - - Generics { - lt_token: Some(Token![<](def_site())), - params: g, - gt_token: Some(Token![>](def_site())), - where_clause: None, - } - } else { - Default::default() - }, - brace_token: Default::default(), - variants, - }) -} - -fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl { - let kind_type = Type::Path(TypePath { - qself: None, - path: Ident::new("AstParentKind", call_site()).into(), - }); - - let set_index_item = ImplItem::Fn(ImplItemFn { - attrs: Default::default(), - vis: Visibility::Inherited, - defaultness: Default::default(), - sig: Signature { - constness: Default::default(), - asyncness: Default::default(), - unsafety: Default::default(), - abi: Default::default(), - fn_token: Default::default(), - ident: Ident::new("set_index", call_site()), - generics: Default::default(), - paren_token: Default::default(), - inputs: { - let mut v = Punctuated::new(); - v.push(FnArg::Receiver(parse_quote!(&mut self))); - v.push(FnArg::Typed(PatType { - attrs: Default::default(), - colon_token: Default::default(), - ty: parse_quote!(usize), - pat: Box::new(Pat::Ident(PatIdent { - attrs: Default::default(), - by_ref: Default::default(), - mutability: Default::default(), - ident: Ident::new("index", call_site()), - subpat: Default::default(), - })), - })); - - v - }, - variadic: Default::default(), - output: ReturnType::Default, - }, - block: Block { - brace_token: Default::default(), - stmts: { - let mut arms = Vec::new(); - - for stmt in stmts { - let item = match stmt { - Stmt::Item(item) => item, - _ => continue, - }; - let name = match item { - Item::Enum(ItemEnum { - ident, variants, .. - }) => { - if variants.iter().all(|v| v.fields.is_empty()) { - continue; - } - ident - } - Item::Struct(ItemStruct { ident, .. }) => ident, - _ => continue, - }; - - arms.push(Arm { - attrs: Default::default(), - pat: Pat::TupleStruct(PatTupleStruct { - attrs: Default::default(), - path: parse_quote!(Self::#name), - qself: None, - paren_token: Default::default(), - elems: { - let mut v = Punctuated::new(); - v.push(Pat::Ident(PatIdent { - attrs: Default::default(), - by_ref: Default::default(), - mutability: Default::default(), - ident: Ident::new("v", name.span()), - subpat: Default::default(), - })); - - v - }, - }), - guard: Default::default(), - fat_arrow_token: Token![=>](name.span()), - body: parse_quote!(v.set_index(index)), - comma: Some(Token![,](name.span())), - }) - } - - let match_expr = Expr::Match(ExprMatch { - attrs: Default::default(), - match_token: Default::default(), - expr: parse_quote!(self), - brace_token: Default::default(), - arms, - }); - - vec![Stmt::Expr(match_expr, None)] - }, - }, - }); - - ItemImpl { - attrs: Default::default(), - defaultness: Default::default(), - unsafety: Default::default(), - impl_token: Default::default(), - generics: Default::default(), - 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], - } -} - -fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option { - let kind_type = Type::Path(TypePath { - qself: None, - path: Ident::new("AstParentKind", call_site()).into(), - }); - - let kind_item = ImplItem::Fn(ImplItemFn { - attrs: Default::default(), - vis: Visibility::Public(Token![pub](def_site())), - defaultness: Default::default(), - sig: Signature { - constness: Default::default(), - asyncness: Default::default(), - unsafety: Default::default(), - abi: Default::default(), - fn_token: Default::default(), - ident: Ident::new("kind", call_site()), - generics: Default::default(), - paren_token: Default::default(), - inputs: { - let mut v = Punctuated::new(); - v.push(FnArg::Receiver(parse_quote!(&self))); - - v - }, - variadic: Default::default(), - output: ReturnType::Type(Token![->](def_site()), Box::new(kind_type)), - }, - block: Block { - brace_token: Default::default(), - stmts: { - let mut arms = Vec::new(); - - for stmt in stmts { - let item = match stmt { - Stmt::Item(item) => item, - _ => continue, - }; - let name = match item { - Item::Enum(ItemEnum { - ident, variants, .. - }) => { - if variants.iter().all(|v| v.fields.is_empty()) { - continue; - } - ident - } - Item::Struct(ItemStruct { ident, .. }) => ident, - _ => continue, - }; - - let field_kind = Ident::new("__field_kind", item.span()); - - let pat = Pat::TupleStruct(PatTupleStruct { - attrs: Default::default(), - path: parse_quote!(Self::#name), - qself: None, - paren_token: Default::default(), - elems: { - let mut v = Punctuated::new(); - - // Ignore node ref itself - v.push(Pat::Wild(PatWild { - attrs: Default::default(), - underscore_token: Token![_](stmt.span()), - })); - - v.push(Pat::Ident(PatIdent { - attrs: Default::default(), - ident: field_kind.clone(), - subpat: None, - by_ref: Default::default(), - mutability: Default::default(), - })); - - v - }, - }); - - let path_expr = Expr::Path(ExprPath { - attrs: Default::default(), - qself: Default::default(), - path: parse_quote!(AstParentKind::#name), - }); - - arms.push(Arm { - attrs: Default::default(), - pat, - guard: Default::default(), - fat_arrow_token: Token![=>](stmt.span()), - body: Box::new(Expr::Call(ExprCall { - attrs: Default::default(), - func: Box::new(path_expr), - paren_token: Default::default(), - args: { - let mut v = Punctuated::new(); - v.push(Expr::Unary(ExprUnary { - attrs: Default::default(), - op: UnOp::Deref(Token![*](def_site())), - expr: Box::new(Expr::Path(ExprPath { - attrs: Default::default(), - qself: None, - path: field_kind.clone().into(), - })), - })); - v - }, - })), - comma: Some(Token![,](stmt.span())), - }); - } - - let expr = Expr::Match(ExprMatch { - attrs: Default::default(), - match_token: Default::default(), - expr: parse_quote!(self), - brace_token: Default::default(), - arms, - }); - - vec![Stmt::Expr(expr, None)] - }, - }, - }); - - let set_index_item = ImplItem::Fn(ImplItemFn { - attrs: Default::default(), - vis: Visibility::Inherited, - defaultness: Default::default(), - sig: Signature { - constness: Default::default(), - asyncness: Default::default(), - unsafety: Default::default(), - abi: Default::default(), - fn_token: Default::default(), - ident: Ident::new("set_index", call_site()), - generics: Default::default(), - paren_token: Default::default(), - inputs: { - let mut v = Punctuated::new(); - v.push(FnArg::Receiver(parse_quote!(&mut self))); - v.push(FnArg::Typed(PatType { - attrs: Default::default(), - colon_token: Default::default(), - ty: parse_quote!(usize), - pat: Box::new(Pat::Ident(PatIdent { - attrs: Default::default(), - by_ref: Default::default(), - mutability: Default::default(), - ident: Ident::new("index", call_site()), - subpat: Default::default(), - })), - })); - - v - }, - variadic: Default::default(), - output: ReturnType::Default, - }, - block: Block { - brace_token: Default::default(), - stmts: { - let mut arms = Vec::new(); - - for stmt in stmts { - let item = match stmt { - Stmt::Item(item) => item, - _ => continue, - }; - let name = match item { - Item::Enum(ItemEnum { - ident, variants, .. - }) => { - if variants.iter().all(|v| v.fields.is_empty()) { - continue; - } - ident - } - Item::Struct(ItemStruct { ident, .. }) => ident, - _ => continue, - }; - - let field_kind = Ident::new("__field_kind", item.span()); - - let pat = Pat::TupleStruct(PatTupleStruct { - attrs: Default::default(), - qself: None, - path: parse_quote!(Self::#name), - paren_token: Default::default(), - elems: { - let mut v = Punctuated::new(); - - // Ignore node ref itself - v.push(Pat::Wild(PatWild { - attrs: Default::default(), - underscore_token: Token![_](stmt.span()), - })); - - v.push(Pat::Ident(PatIdent { - attrs: Default::default(), - ident: field_kind.clone(), - subpat: None, - by_ref: Default::default(), - mutability: Default::default(), - })); - - v - }, - }); - - arms.push(Arm { - attrs: Default::default(), - pat, - guard: Default::default(), - fat_arrow_token: Token![=>](stmt.span()), - body: parse_quote!(__field_kind.set_index(index)), - comma: Some(Token![,](stmt.span())), - }); - } - - let match_expr = Expr::Match(ExprMatch { - attrs: Default::default(), - match_token: Default::default(), - expr: parse_quote!(self), - brace_token: Default::default(), - arms, - }); - - vec![Stmt::Expr(match_expr, None)] - }, - }, - }); - - Some(ItemImpl { - attrs: Default::default(), - defaultness: Default::default(), - unsafety: Default::default(), - impl_token: Default::default(), - generics: Default::default(), - trait_: None, - self_ty: parse_quote!(AstParentNodeRef<'_>), - brace_token: Default::default(), - items: vec![kind_item, set_index_item], - }) -} - -fn make(mode: Mode, stmts: &[Stmt]) -> TokenStream { - let mut types = Vec::new(); - let mut methods = Vec::new(); - - for stmts in stmts { - let item = match stmts { - Stmt::Item(item) => item, - _ => unimplemented!("error reporting for something other than Item"), - }; - - let mtd = make_method(mode, item, &mut types); - let mtd = match mtd { - Some(v) => v, - None => continue, - }; - - methods.push(mtd); - } - - let mut tokens = quote!(); - let mut ref_methods = Vec::new(); - let mut optional_methods = Vec::new(); - let mut either_methods = Vec::new(); - let mut visit_all_methods = Vec::new(); - { - let mut new = Vec::new(); - for ty in &types { - add_required(&mut new, ty); - } - types.extend(new); - } - - // Remove `Box` - 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)); - - let types = types; - - methods.sort_by_cached_key(|v| v.sig.ident.to_string()); - methods.dedup_by_key(|v| v.sig.ident.to_string()); - - for ty in &types { - let sig = create_method_sig(mode, ty); - let name = sig.ident.clone(); - let s = name.to_string(); - if methods.iter().any(|m| m.sig.ident == *s) { - continue; - } - - methods.push(TraitItemFn { - attrs: Vec::new(), - sig, - default: Some(create_method_body(mode, ty)), - semi_token: None, - }); - } - - methods.sort_by_cached_key(|v| v.sig.ident.to_string()); - - for ty in &types { - let sig = create_method_sig(mode, ty); - let method = sig.ident.clone(); - - { - // &'_ mut V, Box - let call = mode.call_method(quote!((&mut **self)), quote!(n), &method); - - let block = parse_quote!({ #call }); - - ref_methods.push(ImplItemFn { - attrs: Vec::new(), - vis: Visibility::Inherited, - defaultness: None, - sig: sig.clone(), - block, - }); - } - - { - // Either - - either_methods.push(ImplItemFn { - attrs: Vec::new(), - vis: Visibility::Inherited, - defaultness: None, - sig: sig.clone(), - block: { - let call = mode.call_method(quote!(visitor), quote!(n), &method); - - parse_quote!({ - match self { - ::swc_visit::Either::Left(visitor) => #call, - ::swc_visit::Either::Right(visitor) => #call, - } - }) - }, - }); - } - - { - // Optional - - optional_methods.push(ImplItemFn { - attrs: Vec::new(), - vis: Visibility::Inherited, - defaultness: None, - sig: sig.clone(), - block: match mode { - Mode::VisitAll - | Mode::Visit(VisitorVariant::Normal) - | Mode::VisitMut(VisitorVariant::Normal) => parse_quote!({ - if self.enabled { - self.visitor.#method(n) - } - }), - - Mode::Visit(VisitorVariant::WithPath) - | Mode::VisitMut(VisitorVariant::WithPath) => parse_quote!({ - if self.enabled { - self.visitor.#method(n, __ast_path) - } - }), - - Mode::Fold(VisitorVariant::Normal) => parse_quote!({ - if self.enabled { - self.visitor.#method(n) - } else { - n - } - }), - - Mode::Fold(VisitorVariant::WithPath) => parse_quote!({ - if self.enabled { - self.visitor.#method(n, __ast_path) - } else { - n - } - }), - }, - }); - } - - { - // Visit <-> VisitAll using ::swc_visit::All - - visit_all_methods.push(ImplItemFn { - attrs: Vec::new(), - vis: Visibility::Inherited, - defaultness: None, - sig: sig.clone(), - block: parse_quote!({ - self.visitor.#method(n); - #method(self, n); - }), - }); - } - } - - methods.iter_mut().for_each(|v| { - v.attrs.push(Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(allow(non_shorthand_field_patterns, unused_variables)), - }); - - let mut fn_name = v.sig.ident.clone(); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - fn_name = Ident::new(&format!("{}_with_path", fn_name), def_site()); - } - - let default_body = replace( - &mut v.default, - Some(match mode { - Mode::Fold(VisitorVariant::Normal) - | Mode::VisitMut(VisitorVariant::Normal) - | Mode::Visit(VisitorVariant::Normal) => parse_quote!({#fn_name(self, n)}), - - Mode::Fold(VisitorVariant::WithPath) - | Mode::VisitMut(VisitorVariant::WithPath) - | Mode::Visit(VisitorVariant::WithPath) => { - parse_quote!({ #fn_name(self, n, __ast_path) }) - } - - Mode::VisitAll => Block { - brace_token: Default::default(), - stmts: Default::default(), - }, - }), - ); - - let arg_ty = v - .sig - .inputs - .iter() - .nth(1) - .map(|v| match *v { - FnArg::Typed(ref pat) => &pat.ty, - _ => unreachable!(), - }) - .unwrap(); - - match mode { - Mode::Fold(VisitorVariant::Normal) => { - let t = Ident::new(mode.trait_name(), call_site()); - tokens.extend(quote!( - /// 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: #arg_ty) -> #arg_ty { - #default_body - } - )) - } - - Mode::VisitMut(VisitorVariant::Normal) => { - let t = Ident::new(mode.trait_name(), call_site()); - tokens.extend(quote!( - #[allow(non_shorthand_field_patterns, unused_variables)] - pub fn #fn_name(_visitor: &mut V, n: #arg_ty) { - #default_body - } - )) - } - - Mode::Visit(VisitorVariant::Normal) => { - let t = Ident::new(mode.trait_name(), call_site()); - - tokens.extend(quote!( - /// 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: #arg_ty) { - #default_body - } - )) - } - - Mode::Fold(VisitorVariant::WithPath) => { - let t = Ident::new(mode.trait_name(), call_site()); - tokens.extend(quote!( - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - #[allow(non_shorthand_field_patterns, unused_variables)] - fn #fn_name( - _visitor: &mut V, - n: #arg_ty, - __ast_path: &mut AstKindPath, - ) -> #arg_ty { - #default_body - } - )) - } - - Mode::VisitMut(VisitorVariant::WithPath) => { - let t = Ident::new(mode.trait_name(), call_site()); - - tokens.extend(quote!( - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - #[allow(non_shorthand_field_patterns, unused_variables)] - fn #fn_name( - _visitor: &mut V, - n: #arg_ty, - __ast_path: &mut AstKindPath, - ) { - #default_body - } - )) - } - - Mode::Visit(VisitorVariant::WithPath) => { - let t = Ident::new(mode.trait_name(), call_site()); - tokens.extend(quote!( - #[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: ?::std::marker::Sized + #t>( - _visitor: &mut V, - n: #arg_ty, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - #default_body - } - )); - } - - Mode::VisitAll => {} - } - }); - - let mut attrs = Vec::new(); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - attrs.extend(feature_path_attrs()) - } - - tokens.extend( - ItemTrait { - attrs, - vis: Visibility::Public(Token![pub](def_site())), - unsafety: None, - auto_token: None, - trait_token: Default::default(), - ident: Ident::new(mode.trait_name(), call_site()), - generics: Default::default(), - colon_token: None, - supertraits: Default::default(), - brace_token: Default::default(), - items: methods.into_iter().map(TraitItem::Fn).collect(), - restriction: None, - } - .into_token_stream(), - ); - - { - // impl Visit for &'_ mut V - - 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: ?::std::marker::Sized + #trait_name {}); - - item.items - .extend(ref_methods.clone().into_iter().map(ImplItem::Fn)); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - item.attrs.extend(feature_path_attrs()) - } - - tokens.extend(item.into_token_stream()); - } - { - // impl Visit for Box - - let trait_name = Ident::new(mode.trait_name(), call_site()); - - let mut item: ItemImpl = parse_quote!( - impl #trait_name for ::std::boxed::Box where V: ?::std::marker::Sized + #trait_name {} - ); - - item.items.extend(ref_methods.into_iter().map(ImplItem::Fn)); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - item.attrs.extend(feature_path_attrs()) - } - - tokens.extend(item.into_token_stream()); - } - - { - // impl Trait for Optional - 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)); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - item.attrs.extend(feature_path_attrs()) - } - - tokens.extend(item.into_token_stream()); - } - - { - let trait_name = Ident::new(mode.trait_name(), call_site()); - // impl Trait for Either - let mut item: ItemImpl = parse_quote!( - impl #trait_name for ::swc_visit::Either - where - A: #trait_name, - B: #trait_name, - { - } - ); - - item.items - .extend(either_methods.into_iter().map(ImplItem::Fn)); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - item.attrs.extend(feature_path_attrs()) - } - - tokens.extend(item.into_token_stream()); - } - - // impl Visit for ::swc_visit::All where V: VisitAll - if mode == Mode::VisitAll { - let mut item: ItemImpl = parse_quote!( - impl Visit for ::swc_visit::All where V: VisitAll {} - ); - - item.items - .extend(visit_all_methods.into_iter().map(ImplItem::Fn)); - - tokens.extend(item.into_token_stream()); - tokens.extend(quote!( - pub use ::swc_visit::All; - )); - } - - { - // Add FoldWith, VisitWith - - let trait_decl = match mode { - Mode::Visit(VisitorVariant::Normal) => quote!( - /// A utility trait implemented for ast nodes, and allow to - /// visit them with a visitor. - pub trait VisitWith { - /// Calls a visitor method (v.visit_xxx) with self. - fn visit_with(&self, v: &mut V); - - /// Visit children nodes of self with `v` - fn visit_children_with(&self, v: &mut V); - } - - impl VisitWith for ::std::boxed::Box - where - V: ?::std::marker::Sized + Visit, - T: 'static + VisitWith, - { - fn visit_with(&self, v: &mut V) { - >::visit_with(&**self, v) - } - - /// Visit children nodes of self with `v` - fn visit_children_with(&self, v: &mut V) { - >::visit_children_with(&**self, v) - } - } - ), - - 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))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - pub trait VisitWithPath { - /// Calls a visitor method (v.visit_xxx) with self and the - /// ast path. - fn visit_with_path<'ast, 'r>( - &'ast self, - v: &mut V, - ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r; - - /// Visit children nodes with v and ast path appended - /// [AstParentNodeRef] describing `self`. The ast path will - /// be restored when this method returns. - /// - /// This is the default implementaton of a handler method in - /// [VisitAstPath]. - fn visit_children_with_path<'ast, 'r>( - &'ast self, - v: &mut V, - ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r; - } - - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl VisitWithPath for ::std::boxed::Box - where - V: ?::std::marker::Sized + VisitAstPath, - T: 'static + VisitWithPath, - { - fn visit_with_path<'ast, 'r>( - &'ast self, - v: &mut V, - ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - >::visit_with_path(&**self, v, ast_path) - } - - /// Visit children nodes of self with `v` - fn visit_children_with_path<'ast, 'r>( - &'ast self, - v: &mut V, - ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - >::visit_children_with_path(&**self, v, ast_path) - } - } - ), - - Mode::VisitAll => quote!( - /// A utility trait implemented for ast nodes, and allow to - /// visit them with a visitor. - pub trait VisitAllWith { - /// Calls a visitor method (v.visit_xxx) with self. - fn visit_all_with(&self, v: &mut V); - - /// Visit children nodes of self with `v` - fn visit_all_children_with(&self, v: &mut V); - } - - impl VisitAllWith for ::std::boxed::Box - where - V: ?::std::marker::Sized + VisitAll, - T: 'static + VisitAllWith, - { - fn visit_all_with(&self, v: &mut V) { - >::visit_all_with(&**self, v) - } - - /// Visit children nodes of self with `v` - fn visit_all_children_with(&self, v: &mut V) { - >::visit_all_children_with(&**self, v) - } - } - ), - Mode::Fold(VisitorVariant::Normal) => quote!( - /// A utility trait implemented for ast nodes, and allow to - /// visit them with a visitor. - pub trait FoldWith { - /// Calls a visitor method (v.fold_xxx) with self. - fn fold_with(self, v: &mut V) -> Self; - - /// Visit children nodes of self with `v` - fn fold_children_with(self, v: &mut V) -> Self; - } - - impl FoldWith for ::std::boxed::Box - where - V: ?::std::marker::Sized + Fold, - T: 'static + FoldWith, - { - fn fold_with(self, v: &mut V) -> Self { - ::swc_visit::util::map::Map::map(self, |value| { - >::fold_with(value, v) - }) - } - - /// Visit children nodes of self with `v` - fn fold_children_with(self, v: &mut V) -> Self { - ::swc_visit::util::map::Map::map(self, |value| { - >::fold_children_with(value, v) - }) - } - } - ), - 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))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - pub trait FoldWithPath { - /// Calls a visitor method (v.fold_xxx) with self and the - /// ast path. - fn fold_with_path(self, v: &mut V, ast_path: &mut AstKindPath) -> Self; - - /// Visit children nodes with v and ast path appended - /// [AstKind] of `self`. The ast path will - /// be restored when this method returns. - /// - /// This is the default implementaton of a handler method in - /// [FoldAstPath]. - fn fold_children_with_path(self, v: &mut V, ast_path: &mut AstKindPath) - -> Self; - } - - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - impl FoldWithPath for ::std::boxed::Box - where - V: ?::std::marker::Sized + FoldAstPath, - T: 'static + FoldWithPath, - { - fn fold_with_path(self, v: &mut V, ast_path: &mut AstKindPath) -> Self { - ::swc_visit::util::map::Map::map(self, |value| { - >::fold_with_path(value, v, ast_path) - }) - } - - /// Visit children nodes of self with `v` - fn fold_children_with_path( - self, - v: &mut V, - ast_path: &mut AstKindPath, - ) -> Self { - ::swc_visit::util::map::Map::map(self, |value| { - >::fold_children_with_path(value, v, ast_path) - }) - } - } - ), - Mode::VisitMut(VisitorVariant::Normal) => quote!( - /// A utility trait implemented for ast nodes, and allow to - /// visit them with a visitor. - pub trait VisitMutWith { - /// Calls a visitor method (v.visit_mut_xxx) with self. - fn visit_mut_with(&mut self, v: &mut V); - - fn visit_mut_children_with(&mut self, v: &mut V); - } - - impl VisitMutWith for ::std::boxed::Box - where - V: ?::std::marker::Sized + VisitMut, - T: 'static + VisitMutWith, - { - fn visit_mut_with(&mut self, v: &mut V) { - >::visit_mut_with(&mut **self, v); - } - - fn visit_mut_children_with(&mut self, v: &mut V) { - >::visit_mut_children_with(&mut **self, v); - } - } - ), - 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))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - pub trait VisitMutWithPath { - /// Calls a visitor method (v.visit_mut_xxx) with self and - /// the ast path. - fn visit_mut_with_path(&mut self, v: &mut V, ast_path: &mut AstKindPath); - - /// Visit children nodes with v and ast path appended - /// [AstKind] of `self`. The ast path will be restored when - /// this method returns. - /// - /// This is the default implementaton of a handler method in - /// [VisitMutAstPath]. - fn visit_mut_children_with_path( - &mut self, - v: &mut V, - ast_path: &mut AstKindPath, - ); - } - - #[cfg(any(feature = "path", docsrs))] - #[cfg_attr(docsrs, doc(cfg(feature = "path")))] - #[doc = "Delegating implementation"] - impl VisitMutWithPath for ::std::boxed::Box - where - V: ?::std::marker::Sized + VisitMutAstPath, - T: 'static + VisitMutWithPath, - { - fn visit_mut_with_path(&mut self, v: &mut V, ast_path: &mut AstKindPath) { - >::visit_mut_with_path(&mut **self, v, ast_path); - } - - fn visit_mut_children_with_path( - &mut self, - v: &mut V, - ast_path: &mut AstKindPath, - ) { - >::visit_mut_children_with_path( - &mut **self, - v, - ast_path, - ); - } - } - ), - }; - tokens.extend(trait_decl.into_token_stream()); - - let mut names = HashSet::new(); - - for ty in &types { - if extract_box(ty).is_some() { - continue; - } - - // Signature of visit_item / fold_item - let method_sig = method_sig(mode, ty); - let mut method_name = method_sig.ident; - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - method_name = Ident::new(&format!("{}_with_path", method_name), def_site()); - } - - // Prevent duplicate implementations. - let s = method_name.to_string(); - if names.contains(&s) { - continue; - } - names.insert(s); - - 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, - parse_quote!(self), - |expr| parse_quote!(#method_name(_visitor, #expr)), - ); - - if let Some(elem_ty) = extract_generic("Vec", ty) { - tokens.extend(quote!( - impl VisitWith for [#elem_ty] { - fn visit_with(&self, v: &mut V) { - #expr - } - - fn visit_children_with(&self, _visitor: &mut V) { - #default_body - } - } - )); - - tokens.extend(quote!( - impl VisitWith for #ty { - fn visit_with(&self, v: &mut V) { - <[#elem_ty] as VisitWith>::visit_with(&**self, v) - } - - fn visit_children_with(&self, _visitor: &mut V) { - <[#elem_ty] as VisitWith>::visit_children_with(&**self, _visitor) - } - } - )); - } else { - tokens.extend(quote!( - impl VisitWith for #ty { - fn visit_with(&self, v: &mut V) { - #expr - } - - fn visit_children_with(&self, _visitor: &mut V) { - #default_body - } - } - )); - } - } - - Mode::Visit(VisitorVariant::WithPath) => { - 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.extend(quote!( - #[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 - } - } - )); - - tokens.extend(quote!( - #[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, - { - (**self).visit_with_path(v, __ast_path) - } - - fn visit_children_with_path<'ast, 'r>( - &'ast self, - _visitor: &mut V, - __ast_path: &mut AstNodePath<'r>, - ) where - 'ast: 'r, - { - (**self).visit_children_with_path(_visitor, __ast_path) - } - } - )); - } else { - tokens.extend(quote!( - #[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 - } - } - )); - } - } - - Mode::VisitAll => { - let default_body = adjust_expr( - mode, - ty, - parse_quote!(self), - |expr| parse_quote!(#method_name(_visitor, #expr)), - ); - - tokens.extend(quote!( - 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 - } - } - )); - } - - Mode::VisitMut(VisitorVariant::Normal) => { - let default_body = adjust_expr( - mode, - ty, - parse_quote!(self), - |expr| parse_quote!(#method_name(_visitor, #expr)), - ); - - tokens.extend(quote!( - 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 - } - } - )); - } - - Mode::VisitMut(VisitorVariant::WithPath) => { - let default_body = adjust_expr( - mode, - ty, - parse_quote!(self), - |expr| parse_quote!(#method_name(_visitor, #expr, __ast_path)), - ); - - tokens.extend(quote!( - #[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 - } - } - )); - } - - Mode::Fold(VisitorVariant::Normal) => { - tokens.extend(quote!( - 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) - } - } - )); - } - - Mode::Fold(VisitorVariant::WithPath) => { - tokens.extend(quote!( - #[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) - } - } - )); - } - } - } - } - - tokens -} - -fn adjust_expr(mode: Mode, ty: &Type, mut expr: Expr, visit: F) -> Expr -where - F: FnOnce(Expr) -> Expr, -{ - if is_option(ty) { - expr = if is_opt_vec(ty) { - match mode { - Mode::Fold { .. } => expr, - Mode::VisitMut { .. } => expr, - Mode::Visit { .. } | Mode::VisitAll => { - parse_quote!(match &#expr { - Some(v) => Some(&**v), - None => None, - }) - } - } - } else { - match mode { - Mode::Fold { .. } => expr, - Mode::VisitMut { .. } => expr, - Mode::Visit { .. } | Mode::VisitAll => parse_quote!(match &#expr { - Some(v) => Some(v), - None => None, - - }), - } - }; - } - - if extract_box(ty).is_some() { - expr = match mode { - Mode::Visit { .. } | Mode::VisitAll => expr, - Mode::VisitMut { .. } => expr, - Mode::Fold { .. } => parse_quote!(*#expr), - }; - } - - expr = visit(expr); - - if extract_box(ty).is_some() { - expr = match mode { - Mode::Visit { .. } | Mode::VisitAll => expr, - Mode::VisitMut { .. } => expr, - Mode::Fold { .. } => parse_quote!(Box::new(#expr)), - }; - } - - expr -} - -/// -/// -/// - `Box` => visit(&node) or Box::new(visit(*node)) -/// - `Vec` => &*node or -fn visit_expr( - mode: Mode, - ty: &Type, - visitor: &Expr, - expr: Expr, - ast_path: Option<(Ident, Ident)>, -) -> Expr { - let visit_name = method_name(mode, ty); - - adjust_expr(mode, ty, expr, |expr| match mode { - Mode::Fold(VisitorVariant::Normal) - | Mode::VisitMut(VisitorVariant::Normal) - | Mode::Visit(VisitorVariant::Normal) - | Mode::VisitAll => parse_quote!(#visitor.#visit_name(#expr)), - - Mode::Fold(VisitorVariant::WithPath) - | Mode::VisitMut(VisitorVariant::WithPath) - | Mode::Visit(VisitorVariant::WithPath) => { - if let Some((type_name, field_name)) = ast_path { - let field_type_name = Ident::new(&format!("{}Field", type_name), type_name.span()); - - let field_name = - Ident::new(&field_name.to_string().to_pascal_case(), field_name.span()); - - let ast_path_expr: Expr = match mode { - Mode::Visit(..) => { - if is_vec_or_opt_vec(ty) { - parse_quote!(AstParentNodeRef::#type_name( - n, - self::fields::#field_type_name::#field_name(usize::MAX) - )) - } else { - parse_quote!(AstParentNodeRef::#type_name( - n, - self::fields::#field_type_name::#field_name - )) - } - } - _ => { - if is_vec_or_opt_vec(ty) { - parse_quote!(AstParentKind::#type_name(self::fields::#field_type_name::#field_name( - usize::MAX - ))) - } else { - parse_quote!(AstParentKind::#type_name(self::fields::#field_type_name::#field_name)) - } - } - }; - - parse_quote!({ - let mut __ast_path = __ast_path.with_guard(#ast_path_expr); - #visitor.#visit_name(#expr, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - } else { - parse_quote!(#visitor.#visit_name(#expr, __ast_path)) - } - } - }) -} - -fn make_arm_from_struct( - mode: Mode, - type_name: &Ident, - path: &Path, - variant_name: Option<&Ident>, - variant: &Fields, - use_ast_path: bool, -) -> Arm { - let mut stmts = Vec::new(); - let mut fields: Punctuated = Default::default(); - - for (i, field) in variant.iter().enumerate() { - let ty = &field.ty; - - let binding_ident = field - .ident - .clone() - .unwrap_or_else(|| Ident::new(&format!("_{}", i), call_site())); - - if !skip(ty) { - let expr: Expr = parse_quote!(#binding_ident); - - let ast_path = if use_ast_path { - Some(( - type_name.clone(), - field - .ident - .clone() - .unwrap_or_else(|| variant_name.cloned().unwrap()), - )) - } else { - None - }; - - 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 { .. } => parse_quote!(let #binding_ident = #expr;), - }); - } - - fields.push(FieldPat { - attrs: Vec::new(), - member: if field.ident.is_none() { - Member::Unnamed(Index { - index: i as _, - span: path.span(), - }) - } else { - Member::Named(field.ident.clone().unwrap()) - }, - colon_token: Some(Token![:](def_site())), - pat: Box::new(Pat::Ident(PatIdent { - attrs: Default::default(), - by_ref: None, - mutability: None, - ident: binding_ident, - subpat: None, - })), - }); - } - - match mode { - Mode::Fold { .. } => { - // Append return statement - stmts.push(parse_quote!(return #path { #fields };)) - } - Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => {} - } - - let block = Block { - brace_token: Default::default(), - stmts, - }; - - Arm { - attrs: Vec::new(), - pat: Pat::Struct(PatStruct { - attrs: Vec::new(), - qself: None, - path: path.clone(), - brace_token: Default::default(), - fields, - rest: None, - }), - guard: None, - fat_arrow_token: Default::default(), - body: Box::new(Expr::Block(ExprBlock { - attrs: Vec::new(), - label: None, - block, - })), - comma: None, - } -} - -fn method_sig(mode: Mode, ty: &Type) -> Signature { - Signature { - constness: None, - asyncness: None, - unsafety: None, - abi: None, - fn_token: Default::default(), - ident: method_name(mode, ty), - generics: if let Mode::Visit(VisitorVariant::WithPath) = mode { - parse_quote!(<'ast: 'r, 'r>) - } else { - Default::default() - }, - paren_token: Default::default(), - inputs: { - let mut p = Punctuated::default(); - p.push_value(parse_quote!(&mut self)); - p.push_punct(Token![,](def_site())); - match mode { - Mode::Fold { .. } => { - p.push_value(parse_quote!(n: #ty)); - } - - Mode::VisitMut { .. } => { - p.push_value(parse_quote!(n: &mut #ty)); - } - - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - p.push_value(parse_quote!(n: &#ty)); - } - - Mode::Visit(VisitorVariant::WithPath) => { - 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(parse_quote!(__ast_path: #ty)); - } - - p - }, - variadic: None, - output: match mode { - Mode::Fold { .. } => parse_quote!(-> #ty), - _ => ReturnType::Default, - }, - } -} - -fn method_sig_from_ident(mode: Mode, v: &Ident) -> Signature { - method_sig( - mode, - &Type::Path(TypePath { - qself: None, - path: v.clone().into(), - }), - ) -} - -/// Returns None if it's skipped. -fn make_method(mode: Mode, e: &Item, types: &mut Vec) -> Option { - let mut attrs = Vec::new(); - - { - attrs.push(make_doc_attr( - "This method can be overridden to customize the visitor behavior.", - )); - attrs.push(make_doc_attr("")); - } - - if let Some(trait_name) = mode.name_of_trait_for_ast() { - let doc_str = format!( - "This calls [`{}::{}`] on `n` by default. The default method visit children nodes \ - with `self`.", - trait_name, - mode.name_of_trait_children_method_for_ast().unwrap() - ); - attrs.push(make_doc_attr(&doc_str)); - } - - Some(match e { - Item::Struct(s) => { - let type_name = &s.ident; - types.push(Type::Path(TypePath { - qself: None, - path: type_name.clone().into(), - })); - for f in &s.fields { - if skip(&f.ty) { - continue; - } - - types.push(f.ty.clone()); - } - - let block = { - let arm = make_arm_from_struct( - mode, - &s.ident, - &s.ident.clone().into(), - None, - &s.fields, - true, - ); - - let mut match_expr: ExprMatch = parse_quote!(match n {}); - match_expr.arms.push(arm); - - Block { - brace_token: Default::default(), - stmts: vec![Stmt::Expr(match_expr.into(), None)], - } - }; - - let sig = method_sig_from_ident(mode, type_name); - - TraitItemFn { - attrs, - sig, - default: Some(block), - semi_token: None, - } - } - Item::Enum(e) => { - // - let type_name = &e.ident; - - types.push( - TypePath { - qself: None, - path: e.ident.clone().into(), - } - .into(), - ); - - // - - let block = { - let mut arms = Vec::new(); - - let skip_ast_path = e.variants.iter().all(|v| v.fields.is_empty()); - - for variant in &e.variants { - for f in &variant.fields { - if skip(&f.ty) { - continue; - } - types.push(f.ty.clone()); - } - - let enum_path = &e.ident; - let variant_path = &variant.ident; - - let arm = make_arm_from_struct( - mode, - &e.ident, - &parse_quote!(#enum_path::#variant_path), - Some(&variant.ident), - &variant.fields, - !skip_ast_path, - ); - - arms.push(arm); - } - - Block { - brace_token: Default::default(), - stmts: vec![Stmt::Expr( - Expr::Match(ExprMatch { - attrs: Vec::new(), - match_token: Default::default(), - expr: parse_quote!(n), - brace_token: Default::default(), - arms, - }), - None, - )], - } - }; - - TraitItemFn { - attrs, - sig: method_sig_from_ident(mode, type_name), - default: Some(block), - semi_token: None, - } - } - - Item::Use(ItemUse { - tree: UseTree::Name(tree), - .. - }) => { - let type_name = &tree.ident; - types.push(Type::Path(TypePath { - qself: None, - path: type_name.clone().into(), - })); - return None; - } - - _ => unimplemented!( - "proper error reporting for item other than struct / enum: {:?}", - e - ), - }) -} - -fn create_method_sig(mode: Mode, ty: &Type) -> Signature { - fn mk_exact(mode: Mode, ident: Ident, ty: &Type) -> Signature { - Signature { - constness: None, - asyncness: None, - unsafety: None, - abi: None, - fn_token: Default::default(), - ident, - generics: if let Mode::Visit(VisitorVariant::WithPath) = mode { - parse_quote!(<'ast: 'r, 'r>) - } else { - Default::default() - }, - paren_token: Default::default(), - inputs: { - let mut p = Punctuated::default(); - p.push_value(parse_quote!(&mut self)); - p.push_punct(Token![,](def_site())); - p.push_value(parse_quote!(n: #ty)); - - if let Some(VisitorVariant::WithPath) = mode.visitor_variant() { - p.push_punct(Token![,](def_site())); - let ty = ast_path_type(mode); - p.push_value(parse_quote!(__ast_path: #ty)); - } - - p - }, - variadic: None, - output: match mode { - Mode::Fold { .. } => parse_quote!(-> #ty), - _ => ReturnType::Default, - }, - } - } - - fn mk_ref(mode: Mode, ident: Ident, ty: &Type, mutable: bool) -> Signature { - if let Mode::Visit(VisitorVariant::WithPath) = mode { - return mk_exact(mode, ident, &parse_quote!(&'ast #ty)); - } - - mk_exact( - mode, - ident, - &Type::Reference(TypeReference { - and_token: Default::default(), - lifetime: None, - mutability: if mutable { - Some(Token![mut](def_site())) - } else { - None - }, - elem: Box::new(ty.clone()), - }), - ) - } - - match ty { - Type::Array(_) => unimplemented!("type: array type"), - Type::BareFn(_) => unimplemented!("type: fn type"), - Type::Group(_) => unimplemented!("type: group type"), - Type::ImplTrait(_) => unimplemented!("type: impl trait"), - Type::Infer(_) => unreachable!("infer type"), - Type::Macro(_) => unimplemented!("type: macro"), - Type::Never(_) => unreachable!("never type"), - Type::Paren(ty) => create_method_sig(mode, &ty.elem), - Type::Path(p) => { - let last = p.path.segments.last().unwrap(); - let ident = method_name(mode, ty); - - if !last.arguments.is_empty() { - if let Some(arg) = extract_box(ty) { - let ident = method_name(mode, arg); - match mode { - Mode::Fold { .. } => { - return mk_exact(mode, ident, arg); - } - - Mode::VisitMut { .. } => { - return mk_ref(mode, ident, arg, true); - } - - Mode::Visit { .. } | Mode::VisitAll => { - return mk_ref(mode, ident, arg, false); - } - } - } - - if let Some(arg) = extract_generic("Option", ty) { - let ident = method_name(mode, ty); - - if let Some(item) = extract_generic("Vec", arg) { - match mode { - Mode::Fold { .. } => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::option::Option<::std::vec::Vec<#item>>), - ); - } - Mode::VisitMut { .. } => { - return mk_exact( - mode, - ident, - &parse_quote!(&mut ::std::option::Option<::std::vec::Vec<#item>>), - ); - } - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::option::Option<&[#item]>), - ); - } - Mode::Visit(VisitorVariant::WithPath) => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::option::Option<&'ast [#item]>), - ); - } - } - } - - match mode { - Mode::Fold { .. } => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::option::Option<#arg>), - ); - } - Mode::VisitMut { .. } => { - return mk_exact( - mode, - ident, - &parse_quote!(&mut ::std::option::Option<#arg>), - ); - } - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::option::Option<&#arg>), - ); - } - Mode::Visit(VisitorVariant::WithPath) => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::option::Option<&'ast #arg>), - ); - } - } - } - - if last.ident == "Vec" { - match &last.arguments { - PathArguments::AngleBracketed(tps) => { - let arg = tps.args.first().unwrap(); - - match arg { - GenericArgument::Type(arg) => { - let ident = method_name(mode, ty); - - match mode { - Mode::Fold { .. } => { - return mk_exact( - mode, - ident, - &parse_quote!(::std::vec::Vec<#arg>), - ); - } - Mode::VisitMut { .. } => { - return mk_ref( - mode, - ident, - &parse_quote!(::std::vec::Vec<#arg>), - true, - ); - } - Mode::Visit { .. } | Mode::VisitAll => { - return mk_ref( - mode, - ident, - &parse_quote!([#arg]), - false, - ); - } - } - } - _ => unimplemented!("generic parameter other than type"), - } - } - _ => unimplemented!("Vec() -> Ret or Vec without a type parameter"), - } - } - } - - match mode { - Mode::Fold { .. } => mk_exact(mode, ident, ty), - Mode::VisitMut { .. } => mk_ref(mode, ident, ty, true), - Mode::Visit { .. } | Mode::VisitAll => mk_ref(mode, ident, ty, false), - } - } - Type::Ptr(_) => unimplemented!("type: pointer"), - Type::Reference(ty) => create_method_sig(mode, &ty.elem), - Type::Slice(_) => unimplemented!("type: slice"), - Type::TraitObject(_) => unimplemented!("type: trait object"), - Type::Tuple(_) => unimplemented!("type: trait tuple"), - Type::Verbatim(_) => unimplemented!("type: verbatim"), - _ => unimplemented!("Unknown type: {:?}", ty), - } -} - -fn create_method_body(mode: Mode, ty: &Type) -> Block { - if let Some(ty) = extract_generic("Arc", ty) { - match mode { - Mode::Visit(..) | Mode::VisitAll => { - let visit = method_name(mode, ty); - let visit = inject_ast_path_arg_if_required(mode, parse_quote!(_visitor.#visit(n))); - - return Block { - brace_token: Default::default(), - stmts: vec![Stmt::Expr(visit, None)], - }; - } - Mode::VisitMut { .. } => { - return Block { - brace_token: Default::default(), - stmts: Vec::new(), - } - } - Mode::Fold { .. } => return parse_quote!({ n }), - } - } - - match ty { - Type::Array(_) => unimplemented!("type: array type"), - Type::BareFn(_) => unimplemented!("type: fn type"), - Type::Group(_) => unimplemented!("type: group type"), - Type::ImplTrait(_) => unimplemented!("type: impl trait"), - Type::Infer(_) => unreachable!("infer type"), - Type::Macro(_) => unimplemented!("type: macro"), - Type::Never(_) => unreachable!("never type"), - Type::Paren(ty) => create_method_body(mode, &ty.elem), - Type::Path(p) => { - let last = p.path.segments.last().unwrap(); - - if !last.arguments.is_empty() { - if let Some(arg) = extract_box(ty) { - match mode { - Mode::Fold(..) => { - let method = method_name(mode, arg); - let inner = inject_ast_path_arg_if_required( - mode, - parse_quote!(_visitor.#method(*n)), - ); - - return parse_quote!(::swc_visit::util::map::Map::map(n, |n: #arg| #inner)); - } - - Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => { - return create_method_body(mode, arg); - } - } - } - - if last.ident == "Option" { - match &last.arguments { - PathArguments::AngleBracketed(tps) => { - let arg = tps.args.first().unwrap(); - - match arg { - GenericArgument::Type(arg) => { - let method = method_name(mode, arg); - - if let Mode::Fold(..) = mode { - if extract_box(arg).is_some() { - let inner = inject_ast_path_arg_if_required( - mode, - parse_quote!(_visitor.#method(n)), - ); - - return parse_quote!({ - match n { - Some(n) => Some( - ::swc_visit::util::map::Map::map(n, |n| { - #inner - }), - ), - None => None, - } - }); - } - } - - return match mode { - Mode::Fold(..) => { - let inner = inject_ast_path_arg_if_required( - mode, - parse_quote!(_visitor.#method(n)), - ); - - 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, - parse_quote!(_visitor.#method(n)), - ); - - parse_quote!({ - match n { - Some(n) => #inner, - None => {} - } - }) - } - }; - } - _ => unimplemented!("generic parameter other than type"), - } - } - _ => unimplemented!("Box() -> T or Box without a type parameter"), - } - } - - if let Some(arg) = extract_generic("Vec", ty) { - let method = method_name(mode, arg); - - match mode { - Mode::Fold(v) => { - if extract_box(arg).is_some() { - return match v { - VisitorVariant::Normal => parse_quote!({ - ::swc_visit::util::move_map::MoveMap::move_map(n, |v| { - ::swc_visit::util::map::Map::map(v, |v| { - _visitor.#method(v) - }) - }) - }), - 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.#method(v, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - }) - .collect() - }), - }; - } - } - Mode::Visit { .. } | Mode::VisitAll | Mode::VisitMut { .. } => {} - } - - return if is_option(arg) { - match mode { - Mode::Fold(VisitorVariant::Normal) => parse_quote!({ - ::swc_visit::util::move_map::MoveMap::move_map(n, |v| { - _visitor.#method(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.#method(v, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - .collect() - }), - - Mode::VisitMut(VisitorVariant::Normal) => { - parse_quote!({ n.iter_mut().for_each(|v| _visitor.#method(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.#method(v, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - }), - - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - parse_quote!({ n.iter().for_each(|v| _visitor.#method(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.#method(v.as_ref(), ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - }), - } - } else { - match mode { - Mode::Fold(VisitorVariant::Normal) => parse_quote!({ - ::swc_visit::util::move_map::MoveMap::move_map(n, |v| { - _visitor.#method(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.#method(v, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - .collect() - }), - - Mode::VisitMut(VisitorVariant::Normal) => { - parse_quote!({ n.iter_mut().for_each(|v| _visitor.#method(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.#method(v, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - }), - - Mode::Visit(VisitorVariant::Normal) | Mode::VisitAll => { - parse_quote!({ n.iter().for_each(|v| _visitor.#method(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.#method(v, ::std::ops::DerefMut::deref_mut(&mut __ast_path)) - }) - }) - } - } - }; - } - } - - match mode { - Mode::Fold { .. } => parse_quote!({ return n }), - Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => parse_quote!({}), - } - } - Type::Ptr(_) => unimplemented!("type: pointer"), - Type::Reference(ty) => create_method_body(mode, &ty.elem), - Type::Slice(_) => unimplemented!("type: slice"), - Type::TraitObject(_) => unimplemented!("type: trait object"), - Type::Tuple(_) => unimplemented!("type: trait tuple"), - Type::Verbatim(_) => unimplemented!("type: verbatim"), - _ => unimplemented!("Unknown type: {:?}", ty), - } -} - -fn add_required(types: &mut Vec, ty: &Type) { - if let Some(ty) = extract_generic("Option", ty) { - add_required(types, ty); - types.push(ty.clone()); - return; - } - if let Some(ty) = extract_generic("Vec", ty) { - add_required(types, ty); - types.push(ty.clone()); - return; - } - if let Some(ty) = extract_generic("Arc", ty) { - add_required(types, ty); - types.push(ty.clone()); - } -} - -fn is_option(ty: &Type) -> bool { - if let Type::Path(p) = ty { - let last = p.path.segments.last().unwrap(); - - if !last.arguments.is_empty() && last.ident == "Option" { - return true; - } - } - - false -} - -fn extract_box(ty: &Type) -> Option<&Type> { - extract_generic("Box", ty) -} - -fn extract_generic<'a>(name: &str, ty: &'a Type) -> Option<&'a Type> { - if let Type::Path(p) = ty { - let last = p.path.segments.last().unwrap(); - - if !last.arguments.is_empty() && last.ident == name { - match &last.arguments { - PathArguments::AngleBracketed(tps) => { - let arg = tps.args.first().unwrap(); - - match arg { - GenericArgument::Type(arg) => return Some(arg), - _ => unimplemented!("generic parameter other than type"), - } - } - _ => unimplemented!("Box() -> T or Box without a type parameter"), - } - } - } - - if let Type::Reference(r) = ty { - return extract_generic(name, &r.elem); - } - - None -} - -fn is_opt_vec(ty: &Type) -> bool { - if let Some(inner) = extract_generic("Option", ty) { - extract_generic("Vec", inner).is_some() - } else { - false - } -} - -fn is_vec_or_opt_vec(ty: &Type) -> bool { - is_opt_vec(ty) || extract_generic("Vec", ty).is_some() -} - -fn method_name_as_str(mode: Mode, ty: &Type) -> String { - fn suffix(ty: &Type) -> String { - // Box has same name as T - if let Some(ty) = extract_generic("Box", ty) { - return suffix(ty); - } - - if let Some(ty) = extract_generic("Arc", ty) { - return format!("arc_{}", suffix(ty)); - } - if let Some(ty) = extract_generic("Option", ty) { - return format!("opt_{}", suffix(ty)); - } - if let Some(ty) = extract_generic("Vec", ty) { - if let Some(ty) = extract_generic("Option", ty) { - return format!("opt_vec_{}", suffix(ty).to_plural()); - } - if suffix(ty).to_plural() == suffix(ty) { - return format!("{}_vec", suffix(ty).to_plural()); - } - return suffix(ty).to_plural(); - } - type_to_name(ty).to_snake_case() - } - - format!("{}_{}", mode.prefix(), suffix(ty)) -} - -fn ast_path_type(mode: Mode) -> Type { - match mode { - Mode::Visit(_) => parse_quote!(&mut AstNodePath<'r>), - Mode::VisitMut(_) | Mode::Fold(_) => parse_quote!(&mut AstKindPath), - _ => unreachable!(), - } -} - -fn method_name(mode: Mode, ty: &Type) -> Ident { - let span = ty.span(); - Ident::new(&method_name_as_str(mode, ty), span) -} - -fn type_to_name(ty: &Type) -> String { - match ty { - Type::Path(ty) => ty.path.segments.last().unwrap().ident.to_string(), - _ => unimplemented!("type_to_name for type other than path: {:?}", ty), - } -} - -fn skip(ty: &Type) -> bool { - match ty { - Type::Path(p) => { - let i = &p.path.segments.last().as_ref().unwrap().ident; - - p.path.segments.last().as_ref().unwrap().ident == "bool" - || i == "u128" - || i == "u64" - || i == "u32" - || i == "u16" - || i == "u8" - || i == "isize" - || i == "i128" - || i == "i64" - || i == "i32" - || i == "i16" - || i == "i8" - || i == "char" - || i == "f32" - || i == "f64" - } - Type::Reference(r) => skip(&r.elem), - _ => false, - } -} - -fn feature_path_attrs() -> Vec { - vec![ - Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(cfg(any(feature = "path", docsrs))), - }, - Attribute { - pound_token: Default::default(), - style: AttrStyle::Outer, - bracket_token: Default::default(), - meta: parse_quote!(cfg_attr(docsrs, doc(cfg(feature = "path")))), - }, - ] -} - -fn inject_ast_path_arg_if_required(mode: Mode, mut visit_expr: ExprMethodCall) -> Expr { - match mode.visitor_variant() { - Some(VisitorVariant::WithPath) => {} - _ => return Expr::MethodCall(visit_expr), - } - - visit_expr.args.push(parse_quote!(__ast_path)); - - Expr::MethodCall(visit_expr) -} diff --git a/crates/swc_xml_visit/src/generated.rs b/crates/swc_xml_visit/src/generated.rs index 35b9bd7d1509..b69bb436c331 100644 --- a/crates/swc_xml_visit/src/generated.rs +++ b/crates/swc_xml_visit/src/generated.rs @@ -8086,9 +8086,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -8098,9 +8098,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Vec { @@ -8110,9 +8110,9 @@ impl FoldWith for Vec { } fn fold_children_with(self, visitor: &mut V) -> Self { - self.into_iter() - .map(|item| >::fold_with(item, visitor)) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with(item, visitor) + }) } } impl FoldWith for Option { @@ -8151,15 +8151,13 @@ where T: FoldWith, { fn fold_with(self, visitor: &mut V) -> Self { - let v = >::fold_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| >::fold_with(inner, visitor)) } fn fold_children_with(self, visitor: &mut V) -> Self { - let v = >::fold_children_with(*self, visitor); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with(inner, visitor) + }) } } #[doc = r" A visitor trait for traversing the AST."] @@ -10153,13 +10151,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path( - item, visitor, __ast_path, - ) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -10171,11 +10165,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -10187,11 +10179,9 @@ impl FoldWithAstPath for Vec { } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - self.into_iter() - .map(|item| { - >::fold_with_ast_path(item, visitor, __ast_path) - }) - .collect() + swc_visit::util::move_map::MoveMap::move_map(self, |item| { + >::fold_with_ast_path(item, visitor, __ast_path) + }) } } #[cfg(any(docsrs, feature = "path"))] @@ -10242,15 +10232,15 @@ where T: FoldWithAstPath, { fn fold_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_with_ast_path(inner, visitor, __ast_path) + }) } fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self { - let v = >::fold_children_with_ast_path(*self, visitor, __ast_path); - let v = std::boxed::Box::new(v); - v + swc_visit::util::map::Map::map(self, |inner| { + >::fold_children_with_ast_path(inner, visitor, __ast_path) + }) } } #[doc = r" A visitor trait for traversing the AST."] diff --git a/tools/generate-code/src/generators/visitor.rs b/tools/generate-code/src/generators/visitor.rs index ecd5b1a7f141..599422574871 100644 --- a/tools/generate-code/src/generators/visitor.rs +++ b/tools/generate-code/src/generators/visitor.rs @@ -1011,9 +1011,9 @@ impl Generator { } TraitKind::Fold => { parse_quote!( - self.into_iter().map(|item| { + swc_visit::util::move_map::MoveMap::move_map(self, |item| { <#inner_ty as #visit_with_trait_name>::#visit_with_name(item, visitor #ast_path_arg) - }).collect() + }) ) } } @@ -1108,50 +1108,71 @@ impl Generator { { // Box => T - let deref_expr = match self.kind { - TraitKind::Visit | TraitKind::VisitAll => { - quote!(&**self) - } - TraitKind::VisitMut => { - quote!(&mut **self) - } + match self.kind { TraitKind::Fold => { - quote!(*self) - } - }; - - let restore_expr = match self.kind { - TraitKind::Visit | TraitKind::VisitAll => { - quote!() - } - TraitKind::VisitMut => { - quote!() - } - TraitKind::Fold => { - quote!( - let v = std::boxed::Box::new(v); - ) + items.push(parse_quote!( + #(#attrs)* + impl #visit_with_trait_name for std::boxed::Box + where V: ?Sized + #visit_trait_name, + T: #visit_with_trait_name { + fn #visit_with_name #lifetime (#receiver, visitor: &mut V #ast_path_param) #return_type { + swc_visit::util::map::Map::map(self, |inner| { + >::#visit_with_name(inner, visitor #ast_path_arg) + }) + } + fn #visit_with_children_name #lifetime (#receiver, visitor: &mut V #ast_path_param) #return_type { + swc_visit::util::map::Map::map(self, |inner| { + >::#visit_with_children_name(inner, visitor #ast_path_arg) + }) + } + } + )); } - }; - items.push(parse_quote!( - #(#attrs)* - impl #visit_with_trait_name for std::boxed::Box - where V: ?Sized + #visit_trait_name, - T: #visit_with_trait_name { - fn #visit_with_name #lifetime (#receiver, visitor: &mut V #ast_path_param) #return_type { - let v = >::#visit_with_name(#deref_expr, visitor #ast_path_arg); - #restore_expr - v - } + _ => { + let deref_expr = match self.kind { + TraitKind::Visit | TraitKind::VisitAll => { + quote!(&**self) + } + TraitKind::VisitMut => { + quote!(&mut **self) + } + TraitKind::Fold => { + unreachable!() + } + }; - fn #visit_with_children_name #lifetime (#receiver, visitor: &mut V #ast_path_param) #return_type { - let v = >::#visit_with_children_name(#deref_expr, visitor #ast_path_arg); - #restore_expr - v - } + let restore_expr = match self.kind { + TraitKind::Visit | TraitKind::VisitAll => { + quote!() + } + TraitKind::VisitMut => { + quote!() + } + TraitKind::Fold => { + unreachable!() + } + }; + + items.push(parse_quote!( + #(#attrs)* + impl #visit_with_trait_name for std::boxed::Box + where V: ?Sized + #visit_trait_name, + T: #visit_with_trait_name { + fn #visit_with_name #lifetime (#receiver, visitor: &mut V #ast_path_param) #return_type { + let v = >::#visit_with_name(#deref_expr, visitor #ast_path_arg); + #restore_expr + v + } + fn #visit_with_children_name #lifetime (#receiver, visitor: &mut V #ast_path_param) #return_type { + let v = >::#visit_with_children_name(#deref_expr, visitor #ast_path_arg); + #restore_expr + v + } + } + )); } - )); + } } if self.kind == TraitKind::Visit || self.kind == TraitKind::VisitAll {