Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(syntax): derive CloneIn for the AST-related items. #4730

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
std::clone::Clone::clone(self)
}
}
}
.into()
}
_ => panic!("At the moment `CloneIn` derive macro only works for types without lifetimes and/or generic params"),
}
}
7 changes: 7 additions & 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 All @@ -40,3 +41,9 @@ wasm-bindgen = { workspace = true, optional = true }
default = []
to_js_string = ["dep:ryu-js"]
serialize = ["bitflags/serde", "dep:serde", "dep:tsify", "dep:wasm-bindgen", "oxc_index/serialize"]

[package.metadata.cargo-shear]
# We use `oxc_ast_macros::CloneIn` which expands to use `oxc_allocator`.
# To fix this we have to expose `CloneIn` through `oxc_allocator`
# (and probably move it to `oxc_allocator_derive`).
ignored = ["oxc_allocator"]
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
Loading