Skip to content

Commit

Permalink
refactor(ast_tools): remove support for #[visit(as)] (#6773)
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
overlookmotel committed Oct 22, 2024
1 parent e221ea3 commit 0b6db0e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 81 deletions.
53 changes: 6 additions & 47 deletions tasks/ast_tools/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
codegen::{generated_header, LateCtx},
output,
schema::{GetIdent, ToType, TypeDef},
util::ToIdent,
Generator, GeneratorOutput,
};

Expand Down Expand Up @@ -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
Expand All @@ -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<Variant> =
Expand Down
38 changes: 10 additions & 28 deletions tasks/ast_tools/src/generators/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -182,21 +182,14 @@ impl<'a> VisitBuilder<'a> {
}
}

fn get_visitor(
&mut self,
def: &TypeDef,
collection: bool,
visit_as: Option<Ident>,
) -> 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:?}");

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 })
};

Expand Down Expand Up @@ -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! {
Expand All @@ -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_),
}
};

Expand All @@ -292,11 +285,7 @@ impl<'a> VisitBuilder<'a> {
visit_name
}

fn generate_enum_walk(
&mut self,
enum_: &EnumDef,
visit_as: Option<Ident>,
) -> (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_
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -394,12 +382,8 @@ impl<'a> VisitBuilder<'a> {
)
}

fn generate_struct_walk(
&mut self,
struct_: &StructDef,
visit_as: Option<Ident>,
) -> (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
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down
7 changes: 1 addition & 6 deletions tasks/ast_tools/src/markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl Parse for VisitArgs {
/// A struct representing `#[visit(...)]` markers
#[derive(Default, Debug)]
pub struct VisitMarkers {
pub visit_as: Option<Ident>,
pub visit_args: Option<VisitArgs>,
pub enter_before: bool,
pub ignore: bool,
Expand Down Expand Up @@ -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;
Expand All @@ -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" {
Expand All @@ -322,7 +317,7 @@ where
}
}
})
.map(|()| VisitMarkers { visit_as, visit_args, enter_before, ignore })
.map(|()| VisitMarkers { visit_args, enter_before, ignore })
.normalize()
},
)
Expand Down

0 comments on commit 0b6db0e

Please sign in to comment.