diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 69cef4313fb77c..1d97368afcfb7f 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -52,7 +52,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -242,7 +241,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -310,7 +308,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -387,7 +384,6 @@ pub struct TemplateElementValue<'a> { /// #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -507,7 +503,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -616,7 +611,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -635,7 +629,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -690,7 +683,6 @@ macro_rules! match_simple_assignment_target { pub use match_simple_assignment_target; #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -762,7 +754,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -865,7 +856,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -895,7 +885,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -966,7 +955,6 @@ pub struct BlockStatement<'a> { /// Declarations and the Variable Statement #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -1133,7 +1121,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -1167,7 +1154,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -1734,7 +1720,6 @@ pub struct StaticBlock<'a> { } #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -1963,7 +1948,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] diff --git a/crates/oxc_ast/src/ast/jsx.rs b/crates/oxc_ast/src/ast/jsx.rs index 329c16cd32399f..a7518b600bfec4 100644 --- a/crates/oxc_ast/src/ast/jsx.rs +++ b/crates/oxc_ast/src/ast/jsx.rs @@ -152,7 +152,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] diff --git a/crates/oxc_ast/src/ast/ts.rs b/crates/oxc_ast/src/ast/ts.rs index d0ae9edaf4f2af..32fe7a42a03521 100644 --- a/crates/oxc_ast/src/ast/ts.rs +++ b/crates/oxc_ast/src/ast/ts.rs @@ -81,7 +81,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -132,7 +131,6 @@ pub enum TSLiteral<'a> { } #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))] @@ -383,7 +381,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))] @@ -542,7 +539,6 @@ pub struct TSTypeReference<'a> { /// IdentifierReference /// NamespaceNameā€ƒ.ā€ƒIdentifierReference #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -926,7 +922,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] @@ -1099,7 +1094,6 @@ inherit_variants! { /// /// [`ast` module docs]: `super` #[ast(visit)] -#[repr(C, u8)] #[derive(Debug, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))] diff --git a/crates/oxc_ast_macros/src/lib.rs b/crates/oxc_ast_macros/src/lib.rs index c49d0b69fd1430..1b3609b4359d07 100644 --- a/crates/oxc_ast_macros/src/lib.rs +++ b/crates/oxc_ast_macros/src/lib.rs @@ -1,6 +1,12 @@ -use proc_macro::TokenStream; +use proc_macro::{TokenStream, TokenTree}; use std::str::FromStr; +enum ItemKind { + Enum, + Struct, + Unknown, +} + /// Attach to AST node type (struct or enum), to signal to codegen to create visitor for this type. /// /// Macro does not generate any code - it's purely a means to communicate information to the codegen. @@ -14,9 +20,47 @@ use std::str::FromStr; #[proc_macro_attribute] #[allow(clippy::missing_panics_doc)] pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream { - let mut stream = TokenStream::from_str("#[derive(::oxc_ast_macros::Ast)]").unwrap(); + let mut input = input.into_iter(); + let mut stream = TokenStream::new(); + let mut output = TokenStream::from_str("#[derive(::oxc_ast_macros::Ast)]").unwrap(); + + let mut item_kind = ItemKind::Unknown; + + while let Some(next) = input.next() { + if let TokenTree::Ident(ident) = &next { + match ident.to_string().as_str() { + "enum" => { + assert!(matches!(item_kind, ItemKind::Unknown)); + item_kind = ItemKind::Enum; + stream.extend(Some(next)); + break; + } + "struct" => { + assert!(matches!(item_kind, ItemKind::Unknown)); + item_kind = ItemKind::Struct; + stream.extend(Some(next)); + break; + } + _ => {} + } + } + + stream.extend(Some(next)); + } + + // append the remained of the input tokens to the stream stream.extend(input); - stream + + let repr = match item_kind { + ItemKind::Enum => TokenStream::from_str("#[repr(C, u8)]").unwrap(), + // ItemKind::Struct => TokenStream::from_str("#[repr(C)]").unwrap(), + ItemKind::Struct => TokenStream::default(), + ItemKind::Unknown => unreachable!(), + }; + + output.extend(repr); + output.extend(stream); + output } /// Dummy derive macro for a non-existent trait `Ast`.