From 0b6db0e17c96077331460c1a7f6665987cde9342 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:14:39 +0000 Subject: [PATCH] refactor(ast_tools): remove support for `#[visit(as)]` (#6773) #6740 and #6744 removed visitor type aliases (`#[visit(as(ClassHeritage))]`). Remove support for `#[visit(as(...))]` from `oxc_ast_tools`, since it's now dead code. --- tasks/ast_tools/src/generators/ast_kind.rs | 53 +++------------------- tasks/ast_tools/src/generators/visit.rs | 38 ++++------------ tasks/ast_tools/src/markers.rs | 7 +-- 3 files changed, 17 insertions(+), 81 deletions(-) diff --git a/tasks/ast_tools/src/generators/ast_kind.rs b/tasks/ast_tools/src/generators/ast_kind.rs index 4be43f8647d6f..7accca589febb 100644 --- a/tasks/ast_tools/src/generators/ast_kind.rs +++ b/tasks/ast_tools/src/generators/ast_kind.rs @@ -8,7 +8,6 @@ use crate::{ codegen::{generated_header, LateCtx}, output, schema::{GetIdent, ToType, TypeDef}, - util::ToIdent, Generator, GeneratorOutput, }; @@ -80,50 +79,6 @@ pub const BLACK_LIST: [&str; 61] = [ "JSXSpreadChild", ]; -pub fn blacklist((ident, _): &(Ident, Type)) -> bool { - !BLACK_LIST.contains(&ident.to_string().as_str()) -} - -pub fn process_types(def: &TypeDef, _: &LateCtx) -> Vec<(Ident, Type)> { - let aliases = match def { - TypeDef::Enum(enum_) => enum_ - .variants - .iter() - .filter(|it| it.markers.visit.visit_as.is_some()) - .map(|var| { - let field = var.fields.first().unwrap(); - let type_name = field.typ.name().inner_name(); - ( - var.markers.visit.visit_as.clone().expect("Already checked"), - parse_quote!(#type_name<'a>), - ) - }) - .collect_vec(), - TypeDef::Struct(struct_) => struct_ - .fields - .iter() - .filter(|it| it.markers.visit.visit_as.is_some()) - .map(|field| { - let type_name = field.typ.name().inner_name().to_ident(); - ( - field.markers.visit.visit_as.clone().expect("Already checked"), - parse_quote!(#type_name<'a>), - ) - }) - .collect_vec(), - }; - - Some(def) - .into_iter() - .map(|def| { - let ident = def.ident(); - let typ = def.to_type(); - (ident, typ) - }) - .chain(aliases) - .collect() -} - impl Generator for AstKindGenerator { fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput { let have_kinds: Vec<(Ident, Type)> = ctx @@ -133,8 +88,12 @@ impl Generator for AstKindGenerator { .filter( |maybe_kind| matches!(maybe_kind, kind @ (TypeDef::Enum(_) | TypeDef::Struct(_)) if kind.visitable()) ) - .flat_map(|it| process_types(it, ctx)) - .filter(blacklist) + .map(|def| { + let ident = def.ident(); + let typ = def.to_type(); + (ident, typ) + }) + .filter(|(ident, _)| !BLACK_LIST.contains(&ident.to_string().as_str())) .collect(); let types: Vec = diff --git a/tasks/ast_tools/src/generators/visit.rs b/tasks/ast_tools/src/generators/visit.rs index f699e879cbed4..28c1cf7fde5d6 100644 --- a/tasks/ast_tools/src/generators/visit.rs +++ b/tasks/ast_tools/src/generators/visit.rs @@ -159,7 +159,7 @@ impl<'a> VisitBuilder<'a> { .find(|it| it.name() == "Program") .expect("Couldn't find the `Program` type!"); - self.get_visitor(program, false, None); + self.get_visitor(program, false); (self.visits, self.walks) } @@ -182,12 +182,7 @@ impl<'a> VisitBuilder<'a> { } } - fn get_visitor( - &mut self, - def: &TypeDef, - collection: bool, - visit_as: Option, - ) -> Cow<'a, Ident> { + fn get_visitor(&mut self, def: &TypeDef, collection: bool) -> Cow<'a, Ident> { let cache_ix = usize::from(collection); let (ident, as_type) = { debug_assert!(def.visitable(), "{def:?}"); @@ -195,8 +190,6 @@ impl<'a> VisitBuilder<'a> { let ident = def.name().to_ident(); let as_type = def.to_type(); - let ident = visit_as.clone().unwrap_or(ident); - (ident, if collection { parse_quote!(Vec<'a, #as_type>) } else { as_type }) }; @@ -260,7 +253,7 @@ impl<'a> VisitBuilder<'a> { self.walks.push(TokenStream::default()); let (walk_body, may_inline) = if collection { - let singular_visit = self.get_visitor(def, false, None); + let singular_visit = self.get_visitor(def, false); let iter = if self.is_mut { quote!(it.iter_mut()) } else { quote!(it) }; ( quote! { @@ -272,8 +265,8 @@ impl<'a> VisitBuilder<'a> { ) } else { match def { - TypeDef::Enum(enum_) => self.generate_enum_walk(enum_, visit_as), - TypeDef::Struct(struct_) => self.generate_struct_walk(struct_, visit_as), + TypeDef::Enum(enum_) => self.generate_enum_walk(enum_), + TypeDef::Struct(struct_) => self.generate_struct_walk(struct_), } }; @@ -292,11 +285,7 @@ impl<'a> VisitBuilder<'a> { visit_name } - fn generate_enum_walk( - &mut self, - enum_: &EnumDef, - visit_as: Option, - ) -> (TokenStream, /* inline */ bool) { + fn generate_enum_walk(&mut self, enum_: &EnumDef) -> (TokenStream, /* inline */ bool) { let ident = enum_.ident(); let mut non_exhaustive = false; let variants_matches = enum_ @@ -324,7 +313,7 @@ impl<'a> VisitBuilder<'a> { let def = self.ctx.type_def(type_id)?; let visitable = def.visitable(); if visitable { - let visit = self.get_visitor(def, false, None); + let visit = self.get_visitor(def, false); let (args_def, args) = var .markers .visit @@ -363,7 +352,7 @@ impl<'a> VisitBuilder<'a> { } else { format_ident!("to_{snake_name}") }; - let visit = self.get_visitor(def, false, None); + let visit = self.get_visitor(def, false); Some(quote!(#match_macro => visitor.#visit(it.#to_child()))) } else { None @@ -373,7 +362,6 @@ impl<'a> VisitBuilder<'a> { let matches = variants_matches.into_iter().chain(inherit_matches).collect_vec(); let with_node_events = |tk| { - let ident = visit_as.unwrap_or(ident); if KIND_BLACK_LIST.contains(&ident.to_string().as_str()) { tk } else { @@ -394,12 +382,8 @@ impl<'a> VisitBuilder<'a> { ) } - fn generate_struct_walk( - &mut self, - struct_: &StructDef, - visit_as: Option, - ) -> (TokenStream, /* inline */ bool) { - let ident = visit_as.unwrap_or_else(|| struct_.ident()); + fn generate_struct_walk(&mut self, struct_: &StructDef) -> (TokenStream, /* inline */ bool) { + let ident = struct_.ident(); let scope_events = struct_.markers.scope.as_ref().map_or_else(Default::default, |markers| { let flags = markers @@ -456,7 +440,6 @@ impl<'a> VisitBuilder<'a> { } let typ_wrapper = &analysis.wrapper; let markers = &field.markers; - let visit_as = markers.visit.visit_as.clone(); let visit_args = markers.visit.visit_args.clone(); let have_enter_scope = markers.scope.enter_before; @@ -472,7 +455,6 @@ impl<'a> VisitBuilder<'a> { typ_wrapper, TypeWrapper::Vec | TypeWrapper::VecBox | TypeWrapper::OptVec ), - visit_as, ); let name = field.ident().expect("expected named fields!"); let borrowed_field = self.with_ref_pat(quote!(it.#name)); diff --git a/tasks/ast_tools/src/markers.rs b/tasks/ast_tools/src/markers.rs index d81cc48c2f7f5..903c236c24d7a 100644 --- a/tasks/ast_tools/src/markers.rs +++ b/tasks/ast_tools/src/markers.rs @@ -55,7 +55,6 @@ impl Parse for VisitArgs { /// A struct representing `#[visit(...)]` markers #[derive(Default, Debug)] pub struct VisitMarkers { - pub visit_as: Option, pub visit_args: Option, pub enter_before: bool, pub ignore: bool, @@ -299,7 +298,6 @@ where attr.map_or_else( || Ok(VisitMarkers::default()), |attr| { - let mut visit_as = None; let mut visit_args = None; let mut enter_before = false; let mut ignore = false; @@ -310,9 +308,6 @@ where for com in nested { if com.ident == "args" { visit_args = Some(parse2(com.args).unwrap()); - } else if com.ident == "as" { - visit_as = - Some(parse2(com.args).expect("Invalid `#[visit[as(...)]]` input!")); } else if com.ident == "enter_before" { enter_before = true; } else if com.ident == "ignore" { @@ -322,7 +317,7 @@ where } } }) - .map(|()| VisitMarkers { visit_as, visit_args, enter_before, ignore }) + .map(|()| VisitMarkers { visit_args, enter_before, ignore }) .normalize() }, )