From 84370a985912779c0794fac0e0df487b15911529 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 4 Feb 2025 22:31:48 +0000 Subject: [PATCH] refactor(ast_tools): rename `is_visited` to `has_visitor` (#8893) Pure refactor. Rename `is_visited` to `has_visitor`. The latter describes what it is more clearly. Some types e.g. `Option` *are* visited, but they don't have their own visitor method, so having `is_visited = false` is misleading. `has_visitor = false` is more accurate. --- tasks/ast_tools/src/generators/ast_builder.rs | 8 ++++---- tasks/ast_tools/src/generators/ast_kind.rs | 4 ++-- tasks/ast_tools/src/generators/visit.rs | 16 ++++++++-------- tasks/ast_tools/src/schema/extensions/visit.rs | 18 +++++++++--------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/tasks/ast_tools/src/generators/ast_builder.rs b/tasks/ast_tools/src/generators/ast_builder.rs index 8b65df1215129..448017ca43419 100644 --- a/tasks/ast_tools/src/generators/ast_builder.rs +++ b/tasks/ast_tools/src/generators/ast_builder.rs @@ -33,13 +33,13 @@ impl Generator for AstBuilderGenerator { .types .iter() .filter(|&type_def| { - let is_visited = match type_def { - TypeDef::Struct(struct_def) => struct_def.visit.is_visited(), - TypeDef::Enum(enum_def) => enum_def.visit.is_visited(), + let has_visitor = match type_def { + TypeDef::Struct(struct_def) => struct_def.visit.has_visitor(), + TypeDef::Enum(enum_def) => enum_def.visit.has_visitor(), _ => false, }; let is_blacklisted = BLACK_LIST.contains(&type_def.name()); - is_visited && !is_blacklisted + has_visitor && !is_blacklisted }) .map(|type_def| generate_builder_methods(type_def, schema)) .collect::(); diff --git a/tasks/ast_tools/src/generators/ast_kind.rs b/tasks/ast_tools/src/generators/ast_kind.rs index 9162c6699fa40..674967e185b1f 100644 --- a/tasks/ast_tools/src/generators/ast_kind.rs +++ b/tasks/ast_tools/src/generators/ast_kind.rs @@ -99,10 +99,10 @@ impl Generator for AstKindGenerator { for type_def in &mut schema.types { match type_def { TypeDef::Struct(struct_def) => { - struct_def.kind.has_kind = struct_def.visit.is_visited(); + struct_def.kind.has_kind = struct_def.visit.has_visitor(); } TypeDef::Enum(enum_def) => { - enum_def.kind.has_kind = enum_def.visit.is_visited(); + enum_def.kind.has_kind = enum_def.visit.has_visitor(); } _ => {} } diff --git a/tasks/ast_tools/src/generators/visit.rs b/tasks/ast_tools/src/generators/visit.rs index b63321ceb772a..36f0463790089 100644 --- a/tasks/ast_tools/src/generators/visit.rs +++ b/tasks/ast_tools/src/generators/visit.rs @@ -46,7 +46,7 @@ impl Generator for VisitGenerator { } /// Create names for `visit_*` methods and `walk_*` functions for all `Vec`s - /// whose inner type is visited. + /// whose inner type has a visitor. fn prepare(&self, schema: &mut Schema) { for type_id in schema.types.indices() { let Some(vec_def) = schema.types[type_id].as_vec() else { continue }; @@ -54,13 +54,13 @@ impl Generator for VisitGenerator { let inner_type = vec_def.inner_type(schema); let plural_snake_name = match inner_type { TypeDef::Struct(struct_def) => { - if !struct_def.visit.is_visited() { + if !struct_def.visit.has_visitor() { continue; } struct_def.plural_snake_name() } TypeDef::Enum(enum_def) => { - if !enum_def.visit.is_visited() { + if !enum_def.visit.has_visitor() { continue; } enum_def.plural_snake_name() @@ -136,7 +136,7 @@ fn parse_visit_attr(location: AttrLocation, part: AttrPart) -> Result<()> { /// Parse `#[scope]` attr. fn parse_scope_attr(location: AttrLocation, part: AttrPart) -> Result<()> { fn get_or_create_scope(struct_def: &mut StructDef) -> Result<&mut Scope> { - if !struct_def.visit.is_visited() { + if !struct_def.visit.has_visitor() { return Err(()); } @@ -566,8 +566,8 @@ impl VisitBuilder<'_> { let inner_visit_fn_name = inherits_type.visit.visitor_name(); let Some(inner_visit_fn_name) = inner_visit_fn_name else { panic!( - "When an enum inherits variants from another enum and the inheritor is visited, \ - the inherited enum must also be visited: `{}` inheriting from `{}`", + "When an enum inherits variants from another enum and the inheritor has a visitor, \ + the inherited enum must also have a visitor: `{}` inheriting from `{}`", enum_def.name(), inherits_type.name(), ); @@ -867,7 +867,7 @@ impl VisitBuilder<'_> { /// Generate visitor calls for a `Vec`. /// - /// If `Vec` has its own visitor (it does when inner type is a struct or enum which is visited), + /// If `Vec` has its own visitor (it does when inner type is a struct or enum which has a visitor), /// generates a call to that visitor e.g. `visitor.visit_statements(&it.statements)`. /// /// Otherwise, generates code to loop through the `Vec`'s elements and call the inner type's visitor: @@ -897,7 +897,7 @@ impl VisitBuilder<'_> { trailing_semicolon: bool, ) -> Option<(/* visit */ TokenStream, /* visit_mut */ TokenStream)> { if let Some(visit_fn_name) = vec_def.visit.visitor_name() { - // Inner type is a struct or enum which is visited. This `Vec` has own visitor. + // Inner type is a struct or enum which has a visitor. This `Vec` has its own visitor. let visit_fn_ident = create_ident(visit_fn_name); return Some(Self::generate_visit_with_visit_args( &visit_fn_ident, diff --git a/tasks/ast_tools/src/schema/extensions/visit.rs b/tasks/ast_tools/src/schema/extensions/visit.rs index 308abd15666cd..d175f40f4e9df 100644 --- a/tasks/ast_tools/src/schema/extensions/visit.rs +++ b/tasks/ast_tools/src/schema/extensions/visit.rs @@ -9,12 +9,12 @@ pub struct VisitStruct { } impl VisitStruct { - /// Returns `true` if this struct is visited. - pub fn is_visited(&self) -> bool { + /// Returns `true` if this struct has a visitor. + pub fn has_visitor(&self) -> bool { self.visitor_names.is_some() } - /// Get name of visitor method for this struct, if it is visited. + /// Get name of visitor method for this struct, if it has a visitor. pub fn visitor_name(&self) -> Option<&str> { self.visitor_names.as_ref().map(|names| names.visit.as_str()) } @@ -29,12 +29,12 @@ pub struct VisitEnum { } impl VisitEnum { - /// Returns `true` if this enum is visited. - pub fn is_visited(&self) -> bool { + /// Returns `true` if this enum has a visitor. + pub fn has_visitor(&self) -> bool { self.visitor_names.is_some() } - /// Get name of visitor method for this enum, if it is visited. + /// Get name of visitor method for this enum, if it has a visitor. pub fn visitor_name(&self) -> Option<&str> { self.visitor_names.as_ref().map(|names| names.visit.as_str()) } @@ -49,13 +49,13 @@ pub struct VisitVec { } impl VisitVec { - /// Returns `true` if this `Vec` is visited. + /// Returns `true` if this `Vec` has a visitor. #[expect(dead_code)] - pub fn is_visited(&self) -> bool { + pub fn has_visitor(&self) -> bool { self.visitor_names.is_some() } - /// Get name of visitor method for this `Vec`, if it is visited. + /// Get name of visitor method for this `Vec`, if it has a visitor. pub fn visitor_name(&self) -> Option<&str> { self.visitor_names.as_ref().map(|names| names.visit.as_str()) }