Skip to content

Commit 6a36616

Browse files
committed
feat(syntax): derive CloneIn for the AST-related items. (#4730)
Follow-on after #4276, related to #4284.
1 parent 2e63618 commit 6a36616

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_ast_macros/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream {
5353
pub fn ast_derive(_item: TokenStream) -> TokenStream {
5454
TokenStream::new()
5555
}
56+
57+
/// Derive macro generating an impl of the trait `CloneIn`.
58+
///
59+
/// NOTE: This is an internal macro!
60+
/// # Panics
61+
///
62+
#[proc_macro_derive(CloneIn)]
63+
pub fn derive_clone_in(item: TokenStream) -> TokenStream {
64+
let item = syn::parse_macro_input!(item as syn::Item);
65+
match &item {
66+
syn::Item::Struct(syn::ItemStruct { ident, generics, .. })
67+
| syn::Item::Enum(syn::ItemEnum { ident, generics, .. })
68+
if generics.params.is_empty() =>
69+
{
70+
quote! {
71+
#[automatically_derived]
72+
impl<'alloc> ::oxc_allocator::CloneIn<'alloc> for #ident {
73+
type Cloned = #ident;
74+
75+
fn clone_in(&self, _: &'alloc ::oxc_allocator::Allocator) -> Self::Cloned {
76+
std::clone::Clone::clone(self)
77+
}
78+
}
79+
}
80+
.into()
81+
}
82+
_ => panic!("At the moment `CloneIn` derive macro only works for types without lifetimes and/or generic params"),
83+
}
84+
}

crates/oxc_syntax/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ doctest = false
2323
oxc_index = { workspace = true }
2424
oxc_span = { workspace = true }
2525
oxc_ast_macros = { workspace = true }
26+
oxc_allocator = { workspace = true }
2627

2728
unicode-id-start = { workspace = true }
2829
bitflags = { workspace = true }
@@ -40,3 +41,9 @@ wasm-bindgen = { workspace = true, optional = true }
4041
default = []
4142
to_js_string = ["dep:ryu-js"]
4243
serialize = ["bitflags/serde", "dep:serde", "dep:tsify", "dep:wasm-bindgen", "oxc_index/serialize"]
44+
45+
[package.metadata.cargo-shear]
46+
# We use `oxc_ast_macros::CloneIn` which expands to use `oxc_allocator`.
47+
# To fix this we have to expose `CloneIn` through `oxc_allocator`
48+
# (and probably move it to `oxc_allocator_derive`).
49+
ignored = ["oxc_allocator"]

crates/oxc_syntax/src/number.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use oxc_ast_macros::ast;
1+
use oxc_ast_macros::{ast, CloneIn};
22

33
#[ast]
4-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
55
pub enum NumberBase {
66
Float = 0,
77
Decimal = 1,
@@ -17,7 +17,7 @@ impl NumberBase {
1717
}
1818

1919
#[ast]
20-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
2121
pub enum BigintBase {
2222
Decimal = 0,
2323
Binary = 1,

crates/oxc_syntax/src/operator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#[cfg(feature = "serialize")]
55
use ::{serde::Serialize, tsify::Tsify};
66

7-
use oxc_ast_macros::ast;
7+
use oxc_ast_macros::{ast, CloneIn};
88

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

1111
#[ast]
12-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
1313
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
1414
pub enum AssignmentOperator {
1515
#[serde(rename = "=")]
@@ -88,7 +88,7 @@ impl AssignmentOperator {
8888
}
8989

9090
#[ast]
91-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
91+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
9292
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
9393
pub enum BinaryOperator {
9494
#[serde(rename = "==")]
@@ -276,7 +276,7 @@ impl GetPrecedence for BinaryOperator {
276276
}
277277

278278
#[ast]
279-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
279+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
280280
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
281281
pub enum LogicalOperator {
282282
#[serde(rename = "||")]
@@ -316,7 +316,7 @@ impl GetPrecedence for LogicalOperator {
316316
}
317317

318318
#[ast]
319-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
319+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
320320
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
321321
pub enum UnaryOperator {
322322
#[serde(rename = "-")]
@@ -369,7 +369,7 @@ impl UnaryOperator {
369369
}
370370

371371
#[ast]
372-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
372+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, CloneIn)]
373373
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
374374
pub enum UpdateOperator {
375375
#[serde(rename = "++")]

crates/oxc_syntax/src/reference.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bitflags::bitflags;
22
use nonmax::NonMaxU32;
3+
use oxc_ast_macros::CloneIn;
34
#[cfg(feature = "serialize")]
45
use serde::{Serialize, Serializer};
56

@@ -45,7 +46,7 @@ export type ReferenceFlag = {
4546
"#;
4647

4748
bitflags! {
48-
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
49+
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, CloneIn)]
4950
#[cfg_attr(feature = "serialize", derive(Serialize))]
5051
pub struct ReferenceFlag: u8 {
5152
const None = 0;

0 commit comments

Comments
 (0)