fix(visit): Fix regression (#9404)

**Related issue:**

 - https://github.com/vercel/next.js/pull/68560
This commit is contained in:
Donny/강동윤 2024-08-09 13:33:32 +09:00 committed by GitHub
parent 10d99e5f2b
commit 041a7b7ff7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 1465 additions and 979 deletions

View File

@ -0,0 +1,8 @@
---
swc_css_visit: patch
swc_ecma_visit: patch
swc_html_visit: patch
swc_xml_visit: patch
---
fix(visit): Fix regression

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -3804,8 +3804,13 @@ impl<V: ?Sized + VisitAstPath> VisitWithAstPath<V> for [AttributeToken] {
visitor: &mut V,
__ast_path: &mut AstNodePath<'r>,
) {
self.iter().for_each(|item| {
<AttributeToken as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, __ast_path)
self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<AttributeToken as VisitWithAstPath<V>>::visit_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
}
}
@ -3828,8 +3833,9 @@ impl<V: ?Sized + VisitAstPath> VisitWithAstPath<V> for [Attribute] {
visitor: &mut V,
__ast_path: &mut AstNodePath<'r>,
) {
self.iter().for_each(|item| {
<Attribute as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, __ast_path)
self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Attribute as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, &mut *__ast_path)
})
}
}
@ -3852,8 +3858,9 @@ impl<V: ?Sized + VisitAstPath> VisitWithAstPath<V> for [Child] {
visitor: &mut V,
__ast_path: &mut AstNodePath<'r>,
) {
self.iter().for_each(|item| {
<Child as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, __ast_path)
self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Child as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, &mut *__ast_path)
})
}
}
@ -7260,9 +7267,12 @@ impl<V: ?Sized + VisitMutAstPath> VisitMutWithAstPath<V> for Vec<AttributeToken>
#[inline]
fn visit_mut_children_with_ast_path(&mut self, visitor: &mut V, __ast_path: &mut AstKindPath) {
self.iter_mut().for_each(|item| {
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<AttributeToken as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(
item, visitor, __ast_path,
item,
visitor,
&mut *__ast_path,
)
})
}
@ -7278,9 +7288,12 @@ impl<V: ?Sized + VisitMutAstPath> VisitMutWithAstPath<V> for Vec<Attribute> {
#[inline]
fn visit_mut_children_with_ast_path(&mut self, visitor: &mut V, __ast_path: &mut AstKindPath) {
self.iter_mut().for_each(|item| {
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Attribute as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(
item, visitor, __ast_path,
item,
visitor,
&mut *__ast_path,
)
})
}
@ -7296,8 +7309,13 @@ impl<V: ?Sized + VisitMutAstPath> VisitMutWithAstPath<V> for Vec<Child> {
#[inline]
fn visit_mut_children_with_ast_path(&mut self, visitor: &mut V, __ast_path: &mut AstKindPath) {
self.iter_mut().for_each(|item| {
<Child as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(item, visitor, __ast_path)
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Child as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
}
}
@ -10780,9 +10798,17 @@ impl<V: ?Sized + FoldAstPath> FoldWithAstPath<V> for Vec<AttributeToken> {
#[inline]
fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self {
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<AttributeToken as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, __ast_path)
})
self.into_iter()
.enumerate()
.map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<AttributeToken as FoldWithAstPath<V>>::fold_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
.collect()
}
}
#[cfg(any(docsrs, feature = "path"))]
@ -10796,9 +10822,17 @@ impl<V: ?Sized + FoldAstPath> FoldWithAstPath<V> for Vec<Attribute> {
#[inline]
fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self {
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<Attribute as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, __ast_path)
})
self.into_iter()
.enumerate()
.map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Attribute as FoldWithAstPath<V>>::fold_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
.collect()
}
}
#[cfg(any(docsrs, feature = "path"))]
@ -10812,9 +10846,13 @@ impl<V: ?Sized + FoldAstPath> FoldWithAstPath<V> for Vec<Child> {
#[inline]
fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self {
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<Child as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, __ast_path)
})
self.into_iter()
.enumerate()
.map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Child as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, &mut *__ast_path)
})
.collect()
}
}
#[cfg(any(docsrs, feature = "path"))]
@ -12275,6 +12313,15 @@ pub type AstNodePath<'ast> = swc_visit::AstNodePath<AstParentNodeRef<'ast>>;
#[cfg(any(docsrs, feature = "path"))]
pub mod fields {
use swc_html_ast::*;
#[inline(always)]
fn assert_initial_index(idx: usize, index: usize) {
#[cfg(debug_assertions)]
if !(idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
}
impl AttributeField {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
@ -12354,12 +12401,7 @@ pub mod fields {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
Self::Children(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
_ => {}
@ -12379,12 +12421,7 @@ pub mod fields {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
Self::Children(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
_ => {}
@ -12435,21 +12472,11 @@ pub mod fields {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
Self::Attributes(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
Self::Children(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
_ => {}

View File

@ -3701,8 +3701,13 @@ impl<V: ?Sized + VisitAstPath> VisitWithAstPath<V> for [AttributeToken] {
visitor: &mut V,
__ast_path: &mut AstNodePath<'r>,
) {
self.iter().for_each(|item| {
<AttributeToken as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, __ast_path)
self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<AttributeToken as VisitWithAstPath<V>>::visit_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
}
}
@ -3725,8 +3730,9 @@ impl<V: ?Sized + VisitAstPath> VisitWithAstPath<V> for [Attribute] {
visitor: &mut V,
__ast_path: &mut AstNodePath<'r>,
) {
self.iter().for_each(|item| {
<Attribute as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, __ast_path)
self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Attribute as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, &mut *__ast_path)
})
}
}
@ -3749,8 +3755,9 @@ impl<V: ?Sized + VisitAstPath> VisitWithAstPath<V> for [Child] {
visitor: &mut V,
__ast_path: &mut AstNodePath<'r>,
) {
self.iter().for_each(|item| {
<Child as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, __ast_path)
self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Child as VisitWithAstPath<V>>::visit_with_ast_path(item, visitor, &mut *__ast_path)
})
}
}
@ -6984,9 +6991,12 @@ impl<V: ?Sized + VisitMutAstPath> VisitMutWithAstPath<V> for Vec<AttributeToken>
#[inline]
fn visit_mut_children_with_ast_path(&mut self, visitor: &mut V, __ast_path: &mut AstKindPath) {
self.iter_mut().for_each(|item| {
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<AttributeToken as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(
item, visitor, __ast_path,
item,
visitor,
&mut *__ast_path,
)
})
}
@ -7002,9 +7012,12 @@ impl<V: ?Sized + VisitMutAstPath> VisitMutWithAstPath<V> for Vec<Attribute> {
#[inline]
fn visit_mut_children_with_ast_path(&mut self, visitor: &mut V, __ast_path: &mut AstKindPath) {
self.iter_mut().for_each(|item| {
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Attribute as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(
item, visitor, __ast_path,
item,
visitor,
&mut *__ast_path,
)
})
}
@ -7020,8 +7033,13 @@ impl<V: ?Sized + VisitMutAstPath> VisitMutWithAstPath<V> for Vec<Child> {
#[inline]
fn visit_mut_children_with_ast_path(&mut self, visitor: &mut V, __ast_path: &mut AstKindPath) {
self.iter_mut().for_each(|item| {
<Child as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(item, visitor, __ast_path)
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Child as VisitMutWithAstPath<V>>::visit_mut_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
}
}
@ -10365,9 +10383,17 @@ impl<V: ?Sized + FoldAstPath> FoldWithAstPath<V> for Vec<AttributeToken> {
#[inline]
fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self {
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<AttributeToken as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, __ast_path)
})
self.into_iter()
.enumerate()
.map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<AttributeToken as FoldWithAstPath<V>>::fold_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
.collect()
}
}
#[cfg(any(docsrs, feature = "path"))]
@ -10381,9 +10407,17 @@ impl<V: ?Sized + FoldAstPath> FoldWithAstPath<V> for Vec<Attribute> {
#[inline]
fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self {
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<Attribute as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, __ast_path)
})
self.into_iter()
.enumerate()
.map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Attribute as FoldWithAstPath<V>>::fold_with_ast_path(
item,
visitor,
&mut *__ast_path,
)
})
.collect()
}
}
#[cfg(any(docsrs, feature = "path"))]
@ -10397,9 +10431,13 @@ impl<V: ?Sized + FoldAstPath> FoldWithAstPath<V> for Vec<Child> {
#[inline]
fn fold_children_with_ast_path(self, visitor: &mut V, __ast_path: &mut AstKindPath) -> Self {
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<Child as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, __ast_path)
})
self.into_iter()
.enumerate()
.map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<Child as FoldWithAstPath<V>>::fold_with_ast_path(item, visitor, &mut *__ast_path)
})
.collect()
}
}
#[cfg(any(docsrs, feature = "path"))]
@ -11747,6 +11785,15 @@ pub type AstNodePath<'ast> = swc_visit::AstNodePath<AstParentNodeRef<'ast>>;
#[cfg(any(docsrs, feature = "path"))]
pub mod fields {
use swc_xml_ast::*;
#[inline(always)]
fn assert_initial_index(idx: usize, index: usize) {
#[cfg(debug_assertions)]
if !(idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
}
impl AttributeField {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
@ -11846,12 +11893,7 @@ pub mod fields {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
Self::Children(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
_ => {}
@ -11902,21 +11944,11 @@ pub mod fields {
pub(crate) fn set_index(&mut self, index: usize) {
match self {
Self::Attributes(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
Self::Children(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
}
_ => {}

View File

@ -997,26 +997,48 @@ impl Generator {
let inner = inner.as_ref();
let inner_ty = quote!(#inner);
match self.kind {
TraitKind::Visit | TraitKind::VisitAll => {
match (self.kind, self.variant) {
(TraitKind::Visit | TraitKind::VisitAll, Variant::Normal) => {
parse_quote!(self.iter().for_each(|item| {
<#inner_ty as #visit_with_trait_name<V>>::#visit_with_name(item, visitor #ast_path_arg)
}))
}
TraitKind::VisitMut => {
(TraitKind::Visit | TraitKind::VisitAll, Variant::AstPath) => {
parse_quote!(self.iter().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<#inner_ty as #visit_with_trait_name<V>>::#visit_with_name(item, visitor, &mut *__ast_path)
}))
}
(TraitKind::VisitMut, Variant::Normal) => {
parse_quote!(
self.iter_mut().for_each(|item| {
<#inner_ty as #visit_with_trait_name<V>>::#visit_with_name(item, visitor #ast_path_arg)
})
)
}
TraitKind::Fold => {
(TraitKind::VisitMut, Variant::AstPath) => {
parse_quote!(
self.iter_mut().enumerate().for_each(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<#inner_ty as #visit_with_trait_name<V>>::#visit_with_name(item, visitor, &mut *__ast_path)
})
)
}
(TraitKind::Fold, Variant::Normal) => {
parse_quote!(
swc_visit::util::move_map::MoveMap::move_map(self, |item| {
<#inner_ty as #visit_with_trait_name<V>>::#visit_with_name(item, visitor #ast_path_arg)
})
)
}
(TraitKind::Fold, Variant::AstPath) => {
parse_quote!(
self.into_iter().enumerate().map(|(__idx, item)| {
let mut __ast_path = __ast_path.with_index_guard(__idx);
<#inner_ty as #visit_with_trait_name<V>>::#visit_with_name(item, visitor, &mut *__ast_path)
}).collect()
)
}
}
}
"Option" => {
@ -1239,12 +1261,7 @@ fn field_variant(type_name: &Ident, field: &Field) -> Option<(TokenStream, Optio
);
let arg = parse_quote!(
Self::#variant_name(idx) => {
#[cfg(debug_assertions)]
if !(*idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
assert_initial_index(*idx, index);
*idx = index;
},
@ -1309,6 +1326,18 @@ fn define_fields(crate_name: &Ident, node_types: &[&Item]) -> Vec<Item> {
use #crate_name::*;
));
defs.push(parse_quote!(
#[inline(always)]
fn assert_initial_index(idx: usize, index: usize) {
#[cfg(debug_assertions)]
if !(idx == usize::MAX || index == usize::MAX) {
{
panic!("Should be usize::MAX");
}
}
}
));
for ty in node_types {
let type_name = match ty {
Item::Enum(data) => data.ident.clone(),