From 94792e915356772a909ff44b37ff19ebaacded2e Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 19 Oct 2023 14:58:31 +0800 Subject: [PATCH] refactor(ast): split syntax_directed_operations into separate files --- crates/oxc_ast/src/lib.rs | 2 + crates/oxc_ast/src/span.rs | 1 - .../bound_names.rs} | 133 ------------------ .../is_simple_parameter_list.rs | 12 ++ .../src/syntax_directed_operations/mod.rs | 11 ++ .../private_bound_identifiers.rs | 51 +++++++ .../syntax_directed_operations/prop_name.rs | 70 +++++++++ crates/oxc_ast/src/visit.rs | 1 - crates/oxc_ast/src/visit_mut.rs | 1 - 9 files changed, 146 insertions(+), 136 deletions(-) rename crates/oxc_ast/src/{syntax_directed_operations.rs => syntax_directed_operations/bound_names.rs} (51%) create mode 100644 crates/oxc_ast/src/syntax_directed_operations/is_simple_parameter_list.rs create mode 100644 crates/oxc_ast/src/syntax_directed_operations/mod.rs create mode 100644 crates/oxc_ast/src/syntax_directed_operations/private_bound_identifiers.rs create mode 100644 crates/oxc_ast/src/syntax_directed_operations/prop_name.rs diff --git a/crates/oxc_ast/src/lib.rs b/crates/oxc_ast/src/lib.rs index 70f7012eb..1ecd17d9c 100644 --- a/crates/oxc_ast/src/lib.rs +++ b/crates/oxc_ast/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(clippy::wildcard_imports)] + //! # Oxc AST //! //! This is almost similar to [estree](https://github.com/estree/estree) except a few places: diff --git a/crates/oxc_ast/src/span.rs b/crates/oxc_ast/src/span.rs index bf638710c..e362da444 100644 --- a/crates/oxc_ast/src/span.rs +++ b/crates/oxc_ast/src/span.rs @@ -1,6 +1,5 @@ use oxc_span::{GetSpan, Span}; -#[allow(clippy::wildcard_imports)] use crate::ast::*; impl<'a> GetSpan for Statement<'a> { diff --git a/crates/oxc_ast/src/syntax_directed_operations.rs b/crates/oxc_ast/src/syntax_directed_operations/bound_names.rs similarity index 51% rename from crates/oxc_ast/src/syntax_directed_operations.rs rename to crates/oxc_ast/src/syntax_directed_operations/bound_names.rs index ebbc9525b..26a04717b 100644 --- a/crates/oxc_ast/src/syntax_directed_operations.rs +++ b/crates/oxc_ast/src/syntax_directed_operations/bound_names.rs @@ -1,8 +1,3 @@ -//! [ECMA262 Syntax-Directed Operations](https://tc39.es/ecma262/#sec-syntax-directed-operations) - -use oxc_span::Span; - -#[allow(clippy::wildcard_imports)] use crate::ast::*; /// [`BoundName`](https://tc39.es/ecma262/#sec-static-semantics-boundnames) @@ -162,131 +157,3 @@ impl<'a> BoundNames for ExportNamedDeclaration<'a> { } } } - -/// [`IsSimpleParameterList`](https://tc39.es/ecma262/#sec-static-semantics-issimpleparameterlist) -pub trait IsSimpleParameterList { - fn is_simple_parameter_list(&self) -> bool; -} - -impl<'a> IsSimpleParameterList for FormalParameters<'a> { - fn is_simple_parameter_list(&self) -> bool { - self.items.iter().all(|pat| pat.pattern.kind.is_binding_identifier()) && self.rest.is_none() - } -} - -/// [`PropName`](https://tc39.es/ecma262/#sec-static-semantics-propname) -pub trait PropName { - fn prop_name(&self) -> Option<(&str, Span)>; -} - -impl<'a> PropName for ObjectPropertyKind<'a> { - fn prop_name(&self) -> Option<(&str, Span)> { - match self { - ObjectPropertyKind::ObjectProperty(prop) => prop.prop_name(), - ObjectPropertyKind::SpreadProperty(_) => None, - } - } -} - -impl<'a> PropName for ObjectProperty<'a> { - fn prop_name(&self) -> Option<(&str, Span)> { - if self.kind != PropertyKind::Init || self.method || self.shorthand || self.computed { - return None; - } - self.key.prop_name() - } -} - -impl<'a> PropName for PropertyKey<'a> { - fn prop_name(&self) -> Option<(&str, Span)> { - match self { - PropertyKey::Identifier(ident) => Some((&ident.name, ident.span)), - PropertyKey::PrivateIdentifier(_) => None, - PropertyKey::Expression(expr) => match &expr { - Expression::Identifier(ident) => Some((&ident.name, ident.span)), - Expression::StringLiteral(lit) => Some((&lit.value, lit.span)), - _ => None, - }, - } - } -} - -impl<'a> PropName for ClassElement<'a> { - fn prop_name(&self) -> Option<(&str, Span)> { - match self { - ClassElement::MethodDefinition(def) => def.prop_name(), - ClassElement::TSAbstractMethodDefinition(def) => def.method_definition.prop_name(), - ClassElement::PropertyDefinition(def) => def.prop_name(), - ClassElement::TSAbstractPropertyDefinition(def) => def.property_definition.prop_name(), - _ => None, - } - } -} - -impl<'a> PropName for MethodDefinition<'a> { - fn prop_name(&self) -> Option<(&str, Span)> { - if self.computed { - return None; - } - self.key.prop_name() - } -} - -impl<'a> PropName for PropertyDefinition<'a> { - fn prop_name(&self) -> Option<(&str, Span)> { - if self.computed { - return None; - } - self.key.prop_name() - } -} - -/// [`PrivateBoundIdentifiers`](https://tc39.es/ecma262/#sec-static-semantics-privateboundidentifiers) -pub trait PrivateBoundIdentifiers { - fn private_bound_identifiers(&self) -> Option; -} - -impl<'a> PrivateBoundIdentifiers for ClassElement<'a> { - fn private_bound_identifiers(&self) -> Option { - match self { - ClassElement::StaticBlock(_) | ClassElement::TSIndexSignature(_) => None, - ClassElement::MethodDefinition(def) => def.private_bound_identifiers(), - ClassElement::PropertyDefinition(def) => def.private_bound_identifiers(), - ClassElement::AccessorProperty(def) => def.private_bound_identifiers(), - ClassElement::TSAbstractMethodDefinition(def) => { - def.method_definition.private_bound_identifiers() - } - ClassElement::TSAbstractPropertyDefinition(def) => { - def.property_definition.private_bound_identifiers() - } - } - } -} - -impl<'a> PrivateBoundIdentifiers for MethodDefinition<'a> { - fn private_bound_identifiers(&self) -> Option { - self.value.body.as_ref()?; - if let PropertyKey::PrivateIdentifier(ident) = &self.key { - return Some((*ident).clone()); - } - None - } -} - -impl<'a> PrivateBoundIdentifiers for PropertyDefinition<'a> { - fn private_bound_identifiers(&self) -> Option { - if let PropertyKey::PrivateIdentifier(ident) = &self.key { - return Some((*ident).clone()); - } - None - } -} - -impl<'a> PrivateBoundIdentifiers for AccessorProperty<'a> { - fn private_bound_identifiers(&self) -> Option { - if let PropertyKey::PrivateIdentifier(ident) = &self.key { - return Some((*ident).clone()); - } - None - } -} diff --git a/crates/oxc_ast/src/syntax_directed_operations/is_simple_parameter_list.rs b/crates/oxc_ast/src/syntax_directed_operations/is_simple_parameter_list.rs new file mode 100644 index 000000000..be773071f --- /dev/null +++ b/crates/oxc_ast/src/syntax_directed_operations/is_simple_parameter_list.rs @@ -0,0 +1,12 @@ +use crate::ast::*; + +/// [`IsSimpleParameterList`](https://tc39.es/ecma262/#sec-static-semantics-issimpleparameterlist) +pub trait IsSimpleParameterList { + fn is_simple_parameter_list(&self) -> bool; +} + +impl<'a> IsSimpleParameterList for FormalParameters<'a> { + fn is_simple_parameter_list(&self) -> bool { + self.items.iter().all(|pat| pat.pattern.kind.is_binding_identifier()) && self.rest.is_none() + } +} diff --git a/crates/oxc_ast/src/syntax_directed_operations/mod.rs b/crates/oxc_ast/src/syntax_directed_operations/mod.rs new file mode 100644 index 000000000..f44cc8bd4 --- /dev/null +++ b/crates/oxc_ast/src/syntax_directed_operations/mod.rs @@ -0,0 +1,11 @@ +//! [ECMA262 Syntax-Directed Operations](https://tc39.es/ecma262/#sec-syntax-directed-operations) + +mod bound_names; +mod is_simple_parameter_list; +mod private_bound_identifiers; +mod prop_name; + +pub use self::{ + bound_names::BoundNames, is_simple_parameter_list::IsSimpleParameterList, + private_bound_identifiers::PrivateBoundIdentifiers, prop_name::PropName, +}; diff --git a/crates/oxc_ast/src/syntax_directed_operations/private_bound_identifiers.rs b/crates/oxc_ast/src/syntax_directed_operations/private_bound_identifiers.rs new file mode 100644 index 000000000..8e53dd2b2 --- /dev/null +++ b/crates/oxc_ast/src/syntax_directed_operations/private_bound_identifiers.rs @@ -0,0 +1,51 @@ +use crate::ast::*; + +/// [`PrivateBoundIdentifiers`](https://tc39.es/ecma262/#sec-static-semantics-privateboundidentifiers) +pub trait PrivateBoundIdentifiers { + fn private_bound_identifiers(&self) -> Option; +} + +impl<'a> PrivateBoundIdentifiers for ClassElement<'a> { + fn private_bound_identifiers(&self) -> Option { + match self { + ClassElement::StaticBlock(_) | ClassElement::TSIndexSignature(_) => None, + ClassElement::MethodDefinition(def) => def.private_bound_identifiers(), + ClassElement::PropertyDefinition(def) => def.private_bound_identifiers(), + ClassElement::AccessorProperty(def) => def.private_bound_identifiers(), + ClassElement::TSAbstractMethodDefinition(def) => { + def.method_definition.private_bound_identifiers() + } + ClassElement::TSAbstractPropertyDefinition(def) => { + def.property_definition.private_bound_identifiers() + } + } + } +} + +impl<'a> PrivateBoundIdentifiers for MethodDefinition<'a> { + fn private_bound_identifiers(&self) -> Option { + self.value.body.as_ref()?; + if let PropertyKey::PrivateIdentifier(ident) = &self.key { + return Some((*ident).clone()); + } + None + } +} + +impl<'a> PrivateBoundIdentifiers for PropertyDefinition<'a> { + fn private_bound_identifiers(&self) -> Option { + if let PropertyKey::PrivateIdentifier(ident) = &self.key { + return Some((*ident).clone()); + } + None + } +} + +impl<'a> PrivateBoundIdentifiers for AccessorProperty<'a> { + fn private_bound_identifiers(&self) -> Option { + if let PropertyKey::PrivateIdentifier(ident) = &self.key { + return Some((*ident).clone()); + } + None + } +} diff --git a/crates/oxc_ast/src/syntax_directed_operations/prop_name.rs b/crates/oxc_ast/src/syntax_directed_operations/prop_name.rs new file mode 100644 index 000000000..4c4ee2ad2 --- /dev/null +++ b/crates/oxc_ast/src/syntax_directed_operations/prop_name.rs @@ -0,0 +1,70 @@ +use oxc_span::Span; + +use crate::ast::*; + +/// [`PropName`](https://tc39.es/ecma262/#sec-static-semantics-propname) +pub trait PropName { + fn prop_name(&self) -> Option<(&str, Span)>; +} + +impl<'a> PropName for ObjectPropertyKind<'a> { + fn prop_name(&self) -> Option<(&str, Span)> { + match self { + ObjectPropertyKind::ObjectProperty(prop) => prop.prop_name(), + ObjectPropertyKind::SpreadProperty(_) => None, + } + } +} + +impl<'a> PropName for ObjectProperty<'a> { + fn prop_name(&self) -> Option<(&str, Span)> { + if self.kind != PropertyKind::Init || self.method || self.shorthand || self.computed { + return None; + } + self.key.prop_name() + } +} + +impl<'a> PropName for PropertyKey<'a> { + fn prop_name(&self) -> Option<(&str, Span)> { + match self { + PropertyKey::Identifier(ident) => Some((&ident.name, ident.span)), + PropertyKey::PrivateIdentifier(_) => None, + PropertyKey::Expression(expr) => match &expr { + Expression::Identifier(ident) => Some((&ident.name, ident.span)), + Expression::StringLiteral(lit) => Some((&lit.value, lit.span)), + _ => None, + }, + } + } +} + +impl<'a> PropName for ClassElement<'a> { + fn prop_name(&self) -> Option<(&str, Span)> { + match self { + ClassElement::MethodDefinition(def) => def.prop_name(), + ClassElement::TSAbstractMethodDefinition(def) => def.method_definition.prop_name(), + ClassElement::PropertyDefinition(def) => def.prop_name(), + ClassElement::TSAbstractPropertyDefinition(def) => def.property_definition.prop_name(), + _ => None, + } + } +} + +impl<'a> PropName for MethodDefinition<'a> { + fn prop_name(&self) -> Option<(&str, Span)> { + if self.computed { + return None; + } + self.key.prop_name() + } +} + +impl<'a> PropName for PropertyDefinition<'a> { + fn prop_name(&self) -> Option<(&str, Span)> { + if self.computed { + return None; + } + self.key.prop_name() + } +} diff --git a/crates/oxc_ast/src/visit.rs b/crates/oxc_ast/src/visit.rs index ac0b709e6..a0178474d 100644 --- a/crates/oxc_ast/src/visit.rs +++ b/crates/oxc_ast/src/visit.rs @@ -8,7 +8,6 @@ use oxc_allocator::Vec; use oxc_span::Span; use oxc_syntax::scope::ScopeFlags; -#[allow(clippy::wildcard_imports)] use crate::{ast::*, ast_kind::AstKind}; /// Syntax tree traversal diff --git a/crates/oxc_ast/src/visit_mut.rs b/crates/oxc_ast/src/visit_mut.rs index 4a64524a8..921a571c5 100644 --- a/crates/oxc_ast/src/visit_mut.rs +++ b/crates/oxc_ast/src/visit_mut.rs @@ -3,7 +3,6 @@ use oxc_allocator::Vec; use oxc_span::Span; -#[allow(clippy::wildcard_imports)] use crate::ast::*; /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in place.