From cf97f6d1adbd00ae3527e1b3ed18d1b40c084f6c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Mon, 16 Sep 2024 04:10:30 +0000 Subject: [PATCH] refactor(ast): import `syn` types in `ast` macro (#5792) Pure refactor. Import `syn` types with a use statement. This allows shortening the rest of the code, which, in my opinion, makes it easier to read. --- crates/oxc_ast_macros/src/ast.rs | 27 ++++++++++++--------------- crates/oxc_ast_macros/src/lib.rs | 3 ++- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/oxc_ast_macros/src/ast.rs b/crates/oxc_ast_macros/src/ast.rs index 6e8aea1f9c764..5207d67b3300a 100644 --- a/crates/oxc_ast_macros/src/ast.rs +++ b/crates/oxc_ast_macros/src/ast.rs @@ -1,12 +1,11 @@ use proc_macro2::TokenStream; use quote::quote; +use syn::{punctuated::Punctuated, token::Comma, Attribute, Fields, Ident, Item, ItemEnum}; -pub fn ast(input: &syn::Item) -> TokenStream { +pub fn ast(input: &Item) -> TokenStream { let (head, tail) = match input { - syn::Item::Enum(enum_) => (enum_repr(enum_), assert_generated_derives(&enum_.attrs)), - syn::Item::Struct(struct_) => { - (quote!(#[repr(C)]), assert_generated_derives(&struct_.attrs)) - } + Item::Enum(enum_) => (enum_repr(enum_), assert_generated_derives(&enum_.attrs)), + Item::Struct(struct_) => (quote!(#[repr(C)]), assert_generated_derives(&struct_.attrs)), _ => unreachable!(), }; @@ -19,8 +18,8 @@ pub fn ast(input: &syn::Item) -> TokenStream { } /// If `enum_` has any non-unit variant, returns `#[repr(C, u8)]`, otherwise returns `#[repr(u8)]`. -fn enum_repr(enum_: &syn::ItemEnum) -> TokenStream { - if enum_.variants.iter().any(|var| !matches!(var.fields, syn::Fields::Unit)) { +fn enum_repr(enum_: &ItemEnum) -> TokenStream { + if enum_.variants.iter().any(|var| !matches!(var.fields, Fields::Unit)) { quote!(#[repr(C, u8)]) } else { quote!(#[repr(u8)]) @@ -42,23 +41,21 @@ fn enum_repr(enum_: &syn::ItemEnum) -> TokenStream { /// /// If `GetSpan` is not in scope, or it is not the correct `oxc_span::GetSpan`, /// this will raise a compilation error. -fn assert_generated_derives(attrs: &[syn::Attribute]) -> TokenStream { +fn assert_generated_derives(attrs: &[Attribute]) -> TokenStream { #[inline] - fn parse(attr: &syn::Attribute) -> impl Iterator { - attr.parse_args_with( - syn::punctuated::Punctuated::::parse_terminated, - ) - .expect("`generate_derive` only accepts traits as single segment paths, Found an invalid argument") + fn parse(attr: &Attribute) -> impl Iterator { + attr.parse_args_with(Punctuated::::parse_terminated) + .expect("`#[generate_derive]` only accepts traits as single segment paths. Found an invalid argument.") .into_iter() } // TODO: benchmark this to see if a lazy static cell containing `HashMap` would perform better. #[inline] fn abs_trait( - ident: &syn::Ident, + ident: &Ident, ) -> (/* absolute type path */ TokenStream, /* possible generics */ TokenStream) { #[cold] - fn invalid_derive(ident: &syn::Ident) -> ! { + fn invalid_derive(ident: &Ident) -> ! { panic!( "Invalid derive trait(generate_derive): {ident}.\n\ Help: If you are trying to implement a new `generate_derive` trait, \ diff --git a/crates/oxc_ast_macros/src/lib.rs b/crates/oxc_ast_macros/src/lib.rs index d8c0efce8f9a0..f657bff3281d2 100644 --- a/crates/oxc_ast_macros/src/lib.rs +++ b/crates/oxc_ast_macros/src/lib.rs @@ -1,4 +1,5 @@ use proc_macro::TokenStream; +use syn::{parse_macro_input, Item}; mod ast; @@ -69,7 +70,7 @@ mod ast; /// 2. `tsify` #[proc_macro_attribute] pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream { - let input = syn::parse_macro_input!(input as syn::Item); + let input = parse_macro_input!(input as Item); let expanded = ast::ast(&input); TokenStream::from(expanded) }