Skip to content

Commit

Permalink
feat(syntax): derive CloneIn for the AST-related items.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Aug 7, 2024
1 parent 9bcaa81 commit 599295c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions crates/oxc_ast_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,32 @@ 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 {
self.clone()
}
}
}
.into()
}
_ => panic!("At the moment `CloneIn` derive macro only works for types without lifetimes and/or generic params"),
}
}
1 change: 1 addition & 0 deletions crates/oxc_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ doctest = false
oxc_index = { workspace = true }
oxc_span = { workspace = true }
oxc_ast_macros = { workspace = true }
oxc_allocator = { workspace = true }

unicode-id-start = { workspace = true }
bitflags = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_syntax/src/number.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast_macros::ast;
use oxc_ast_macros::{ast, CloneIn};

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
pub enum NumberBase {
Float = 0,
Decimal = 1,
Expand All @@ -17,7 +17,7 @@ impl NumberBase {
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
pub enum BigintBase {
Decimal = 0,
Binary = 1,
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_syntax/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#[cfg(feature = "serialize")]
use ::{serde::Serialize, tsify::Tsify};

use oxc_ast_macros::ast;
use oxc_ast_macros::{ast, CloneIn};

use crate::precedence::{GetPrecedence, Precedence};

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum AssignmentOperator {
#[serde(rename = "=")]
Expand Down Expand Up @@ -88,7 +88,7 @@ impl AssignmentOperator {
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum BinaryOperator {
#[serde(rename = "==")]
Expand Down Expand Up @@ -276,7 +276,7 @@ impl GetPrecedence for BinaryOperator {
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum LogicalOperator {
#[serde(rename = "||")]
Expand Down Expand Up @@ -316,7 +316,7 @@ impl GetPrecedence for LogicalOperator {
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum UnaryOperator {
#[serde(rename = "-")]
Expand Down Expand Up @@ -369,7 +369,7 @@ impl UnaryOperator {
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum UpdateOperator {
#[serde(rename = "++")]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bitflags::bitflags;
use nonmax::NonMaxU32;
use oxc_ast_macros::CloneIn;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};

Expand Down Expand Up @@ -45,7 +46,7 @@ export type ReferenceFlag = {
"#;

bitflags! {
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct ReferenceFlag: u8 {
const None = 0;
Expand Down

0 comments on commit 599295c

Please sign in to comment.