From 0b47a4acde50158e3c9aa20dfdf505d4b17a33a4 Mon Sep 17 00:00:00 2001 From: rzvxa Date: Wed, 28 Aug 2024 03:04:37 +0330 Subject: [PATCH] refactor(syntax): use `generate_derive` for `CloneIn` in types outside of `oxc_ast` crate. --- .github/.generated_ast_watch_list.yml | 1 + crates/oxc_ast/src/ast/literal.rs | 4 +- crates/oxc_ast/src/ast_impl/literal.rs | 8 ++ crates/oxc_ast_macros/src/lib.rs | 29 ---- .../src/generated/derive_clone_in.rs | 127 ++++++++++++++++++ crates/oxc_syntax/src/lib.rs | 3 + crates/oxc_syntax/src/number.rs | 9 +- crates/oxc_syntax/src/operator.rs | 18 ++- crates/oxc_syntax/src/reference.rs | 11 +- 9 files changed, 168 insertions(+), 42 deletions(-) create mode 100644 crates/oxc_syntax/src/generated/derive_clone_in.rs diff --git a/.github/.generated_ast_watch_list.yml b/.github/.generated_ast_watch_list.yml index 3b9a61fb66e04..df3fdfb8383ed 100644 --- a/.github/.generated_ast_watch_list.yml +++ b/.github/.generated_ast_watch_list.yml @@ -16,6 +16,7 @@ src: - 'crates/oxc_ast/src/generated/visit.rs' - 'crates/oxc_ast/src/generated/visit_mut.rs' - 'crates/oxc_ast/src/generated/derive_clone_in.rs' + - 'crates/oxc_syntax/src/generated/derive_clone_in.rs' - 'crates/oxc_ast/src/generated/derive_get_span.rs' - 'crates/oxc_ast/src/generated/derive_get_span_mut.rs' - 'tasks/ast_codegen/src/**' diff --git a/crates/oxc_ast/src/ast/literal.rs b/crates/oxc_ast/src/ast/literal.rs index 5678461b51c18..333f2111af118 100644 --- a/crates/oxc_ast/src/ast/literal.rs +++ b/crates/oxc_ast/src/ast/literal.rs @@ -11,7 +11,7 @@ use std::hash::Hash; use bitflags::bitflags; use oxc_allocator::CloneIn; -use oxc_ast_macros::{ast, CloneIn}; +use oxc_ast_macros::ast; use oxc_span::{Atom, GetSpan, GetSpanMut, Span}; use oxc_syntax::number::{BigintBase, NumberBase}; #[cfg(feature = "serialize")] @@ -137,7 +137,7 @@ bitflags! { /// Regular expression flags. /// /// - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, CloneIn)] + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct RegExpFlags: u8 { /// Global flag /// diff --git a/crates/oxc_ast/src/ast_impl/literal.rs b/crates/oxc_ast/src/ast_impl/literal.rs index 9a6c554bc6651..c95788307b4c8 100644 --- a/crates/oxc_ast/src/ast_impl/literal.rs +++ b/crates/oxc_ast/src/ast_impl/literal.rs @@ -10,6 +10,7 @@ use std::{ hash::{Hash, Hasher}, }; +use oxc_allocator::CloneIn; use oxc_span::{Atom, Span}; use oxc_syntax::number::NumberBase; @@ -222,3 +223,10 @@ impl<'a> fmt::Display for StringLiteral<'a> { self.value.fmt(f) } } + +impl<'alloc> CloneIn<'alloc> for RegExpFlags { + type Cloned = Self; + fn clone_in(&self, _: &'alloc oxc_allocator::Allocator) -> Self::Cloned { + *self + } +} diff --git a/crates/oxc_ast_macros/src/lib.rs b/crates/oxc_ast_macros/src/lib.rs index 6a711592cf435..f8b3000cc85b5 100644 --- a/crates/oxc_ast_macros/src/lib.rs +++ b/crates/oxc_ast_macros/src/lib.rs @@ -167,32 +167,3 @@ pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream { pub fn ast_derive(_item: TokenStream) -> TokenStream { TokenStream::new() } - -/// Derive macro generating an impl of the trait `CloneIn`. -/// -/// NOTE: This is an internal macro! -/// # Panics -/// -#[proc_macro_derive(CloneIn)] -pub fn derive_clone_in(item: TokenStream) -> TokenStream { - let item = syn::parse_macro_input!(item as syn::Item); - match &item { - syn::Item::Struct(syn::ItemStruct { ident, generics, .. }) - | syn::Item::Enum(syn::ItemEnum { ident, generics, .. }) - if generics.params.is_empty() => - { - quote! { - #[automatically_derived] - impl<'alloc> ::oxc_allocator::CloneIn<'alloc> for #ident { - type Cloned = #ident; - - fn clone_in(&self, _: &'alloc ::oxc_allocator::Allocator) -> Self::Cloned { - std::clone::Clone::clone(self) - } - } - } - .into() - } - _ => panic!("At the moment `CloneIn` derive macro only works for types without lifetimes and/or generic params"), - } -} diff --git a/crates/oxc_syntax/src/generated/derive_clone_in.rs b/crates/oxc_syntax/src/generated/derive_clone_in.rs new file mode 100644 index 0000000000000..739219d9359d2 --- /dev/null +++ b/crates/oxc_syntax/src/generated/derive_clone_in.rs @@ -0,0 +1,127 @@ +// Auto-generated code, DO NOT EDIT DIRECTLY! +// To edit this generated file you have to edit `tasks/ast_tools/src/derives/clone_in.rs` + +#![allow(clippy::default_trait_access)] + +use oxc_allocator::{Allocator, CloneIn}; + +#[allow(clippy::wildcard_imports)] +use crate::number::*; + +#[allow(clippy::wildcard_imports)] +use crate::operator::*; + +impl<'alloc> CloneIn<'alloc> for NumberBase { + type Cloned = NumberBase; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::Float => NumberBase::Float, + Self::Decimal => NumberBase::Decimal, + Self::Binary => NumberBase::Binary, + Self::Octal => NumberBase::Octal, + Self::Hex => NumberBase::Hex, + } + } +} + +impl<'alloc> CloneIn<'alloc> for BigintBase { + type Cloned = BigintBase; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::Decimal => BigintBase::Decimal, + Self::Binary => BigintBase::Binary, + Self::Octal => BigintBase::Octal, + Self::Hex => BigintBase::Hex, + } + } +} + +impl<'alloc> CloneIn<'alloc> for AssignmentOperator { + type Cloned = AssignmentOperator; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::Assign => AssignmentOperator::Assign, + Self::Addition => AssignmentOperator::Addition, + Self::Subtraction => AssignmentOperator::Subtraction, + Self::Multiplication => AssignmentOperator::Multiplication, + Self::Division => AssignmentOperator::Division, + Self::Remainder => AssignmentOperator::Remainder, + Self::ShiftLeft => AssignmentOperator::ShiftLeft, + Self::ShiftRight => AssignmentOperator::ShiftRight, + Self::ShiftRightZeroFill => AssignmentOperator::ShiftRightZeroFill, + Self::BitwiseOR => AssignmentOperator::BitwiseOR, + Self::BitwiseXOR => AssignmentOperator::BitwiseXOR, + Self::BitwiseAnd => AssignmentOperator::BitwiseAnd, + Self::LogicalAnd => AssignmentOperator::LogicalAnd, + Self::LogicalOr => AssignmentOperator::LogicalOr, + Self::LogicalNullish => AssignmentOperator::LogicalNullish, + Self::Exponential => AssignmentOperator::Exponential, + } + } +} + +impl<'alloc> CloneIn<'alloc> for BinaryOperator { + type Cloned = BinaryOperator; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::Equality => BinaryOperator::Equality, + Self::Inequality => BinaryOperator::Inequality, + Self::StrictEquality => BinaryOperator::StrictEquality, + Self::StrictInequality => BinaryOperator::StrictInequality, + Self::LessThan => BinaryOperator::LessThan, + Self::LessEqualThan => BinaryOperator::LessEqualThan, + Self::GreaterThan => BinaryOperator::GreaterThan, + Self::GreaterEqualThan => BinaryOperator::GreaterEqualThan, + Self::ShiftLeft => BinaryOperator::ShiftLeft, + Self::ShiftRight => BinaryOperator::ShiftRight, + Self::ShiftRightZeroFill => BinaryOperator::ShiftRightZeroFill, + Self::Addition => BinaryOperator::Addition, + Self::Subtraction => BinaryOperator::Subtraction, + Self::Multiplication => BinaryOperator::Multiplication, + Self::Division => BinaryOperator::Division, + Self::Remainder => BinaryOperator::Remainder, + Self::BitwiseOR => BinaryOperator::BitwiseOR, + Self::BitwiseXOR => BinaryOperator::BitwiseXOR, + Self::BitwiseAnd => BinaryOperator::BitwiseAnd, + Self::In => BinaryOperator::In, + Self::Instanceof => BinaryOperator::Instanceof, + Self::Exponential => BinaryOperator::Exponential, + } + } +} + +impl<'alloc> CloneIn<'alloc> for LogicalOperator { + type Cloned = LogicalOperator; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::Or => LogicalOperator::Or, + Self::And => LogicalOperator::And, + Self::Coalesce => LogicalOperator::Coalesce, + } + } +} + +impl<'alloc> CloneIn<'alloc> for UnaryOperator { + type Cloned = UnaryOperator; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::UnaryNegation => UnaryOperator::UnaryNegation, + Self::UnaryPlus => UnaryOperator::UnaryPlus, + Self::LogicalNot => UnaryOperator::LogicalNot, + Self::BitwiseNot => UnaryOperator::BitwiseNot, + Self::Typeof => UnaryOperator::Typeof, + Self::Void => UnaryOperator::Void, + Self::Delete => UnaryOperator::Delete, + } + } +} + +impl<'alloc> CloneIn<'alloc> for UpdateOperator { + type Cloned = UpdateOperator; + fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned { + match self { + Self::Increment => UpdateOperator::Increment, + Self::Decrement => UpdateOperator::Decrement, + } + } +} diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs index c495c78f1aac8..9bf010f66f11f 100644 --- a/crates/oxc_syntax/src/lib.rs +++ b/crates/oxc_syntax/src/lib.rs @@ -13,3 +13,6 @@ pub mod reference; pub mod scope; pub mod symbol; pub mod xml_entities; +mod generated { + mod derive_clone_in; +} diff --git a/crates/oxc_syntax/src/number.rs b/crates/oxc_syntax/src/number.rs index d13638316743c..515f3f870e923 100644 --- a/crates/oxc_syntax/src/number.rs +++ b/crates/oxc_syntax/src/number.rs @@ -1,7 +1,9 @@ -use oxc_ast_macros::{ast, CloneIn}; +use oxc_allocator::CloneIn; +use oxc_ast_macros::ast; #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] pub enum NumberBase { Float = 0, Decimal = 1, @@ -17,7 +19,8 @@ impl NumberBase { } #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] pub enum BigintBase { Decimal = 0, Binary = 1, diff --git a/crates/oxc_syntax/src/operator.rs b/crates/oxc_syntax/src/operator.rs index 14ddc11e153d6..e132c8e0a9fc2 100644 --- a/crates/oxc_syntax/src/operator.rs +++ b/crates/oxc_syntax/src/operator.rs @@ -4,12 +4,14 @@ #[cfg(feature = "serialize")] use ::{serde::Serialize, tsify::Tsify}; -use oxc_ast_macros::{ast, CloneIn}; +use oxc_allocator::CloneIn; +use oxc_ast_macros::ast; use crate::precedence::{GetPrecedence, Precedence}; #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] pub enum AssignmentOperator { #[serde(rename = "=")] @@ -88,7 +90,8 @@ impl AssignmentOperator { } #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] pub enum BinaryOperator { #[serde(rename = "==")] @@ -276,7 +279,8 @@ impl GetPrecedence for BinaryOperator { } #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] pub enum LogicalOperator { #[serde(rename = "||")] @@ -316,7 +320,8 @@ impl GetPrecedence for LogicalOperator { } #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] pub enum UnaryOperator { #[serde(rename = "-")] @@ -369,7 +374,8 @@ impl UnaryOperator { } #[ast] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[generate_derive(CloneIn)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] pub enum UpdateOperator { #[serde(rename = "++")] diff --git a/crates/oxc_syntax/src/reference.rs b/crates/oxc_syntax/src/reference.rs index df83f278a7bf8..8f7adbd359159 100644 --- a/crates/oxc_syntax/src/reference.rs +++ b/crates/oxc_syntax/src/reference.rs @@ -1,6 +1,6 @@ use bitflags::bitflags; use nonmax::NonMaxU32; -use oxc_ast_macros::CloneIn; +use oxc_allocator::CloneIn; #[cfg(feature = "serialize")] use serde::{Serialize, Serializer}; @@ -76,7 +76,7 @@ bitflags! { /// [`Read`]: ReferenceFlags::Read /// [`Write`]: ReferenceFlags::Write /// [`TSTypeQuery`]: ReferenceFlags::TSTypeQuery - #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, CloneIn)] + #[derive(Debug, Default, Clone, Copy, Eq, PartialEq)] #[cfg_attr(feature = "serialize", derive(Serialize))] pub struct ReferenceFlags: u8 { const None = 0; @@ -166,3 +166,10 @@ impl ReferenceFlags { self.intersects(Self::Value) } } + +impl<'alloc> CloneIn<'alloc> for ReferenceFlags { + type Cloned = Self; + fn clone_in(&self, _: &'alloc oxc_allocator::Allocator) -> Self::Cloned { + *self + } +}