mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(ast): move functions to top level in ast macro (#5793)
Pure refactor. Convert nested functions to top level, to make it easier to read.
This commit is contained in:
parent
cf97f6d1ad
commit
17cd9038b7
1 changed files with 48 additions and 47 deletions
|
|
@ -42,27 +42,35 @@ fn enum_repr(enum_: &ItemEnum) -> TokenStream {
|
||||||
/// If `GetSpan` is not in scope, or it is not the correct `oxc_span::GetSpan`,
|
/// If `GetSpan` is not in scope, or it is not the correct `oxc_span::GetSpan`,
|
||||||
/// this will raise a compilation error.
|
/// this will raise a compilation error.
|
||||||
fn assert_generated_derives(attrs: &[Attribute]) -> TokenStream {
|
fn assert_generated_derives(attrs: &[Attribute]) -> TokenStream {
|
||||||
#[inline]
|
// NOTE: At this level we don't care if a trait is derived multiple times, It is the
|
||||||
fn parse(attr: &Attribute) -> impl Iterator<Item = Ident> {
|
// responsibility of the `ast_tools` to raise errors for those.
|
||||||
|
let assertion = attrs
|
||||||
|
.iter()
|
||||||
|
.filter(|attr| attr.path().is_ident("generate_derive"))
|
||||||
|
.flat_map(parse_attr)
|
||||||
|
.map(|derive| {
|
||||||
|
let (abs_derive, generics) = abs_trait(&derive);
|
||||||
|
quote! {{
|
||||||
|
// NOTE: these are wrapped in a scope to avoid the need for unique identifiers.
|
||||||
|
trait AssertionTrait: #abs_derive #generics {}
|
||||||
|
impl<T: #derive #generics> AssertionTrait for T {}
|
||||||
|
}}
|
||||||
|
});
|
||||||
|
quote!(const _: () = { #(#assertion)* };)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn parse_attr(attr: &Attribute) -> impl Iterator<Item = Ident> {
|
||||||
attr.parse_args_with(Punctuated::<Ident, Comma>::parse_terminated)
|
attr.parse_args_with(Punctuated::<Ident, Comma>::parse_terminated)
|
||||||
.expect("`#[generate_derive]` only accepts traits as single segment paths. Found an invalid argument.")
|
.expect("`#[generate_derive]` only accepts traits as single segment paths. Found an invalid argument.")
|
||||||
.into_iter()
|
.into_iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: benchmark this to see if a lazy static cell containing `HashMap` would perform better.
|
// TODO: benchmark this to see if a lazy static cell containing `HashMap` would perform better.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn abs_trait(
|
fn abs_trait(
|
||||||
ident: &Ident,
|
ident: &Ident,
|
||||||
) -> (/* absolute type path */ TokenStream, /* possible generics */ TokenStream) {
|
) -> (/* absolute type path */ TokenStream, /* possible generics */ TokenStream) {
|
||||||
#[cold]
|
|
||||||
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, \
|
|
||||||
Make sure to add it to the list below."
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ident == "CloneIn" {
|
if ident == "CloneIn" {
|
||||||
(quote!(::oxc_allocator::CloneIn), quote!(<'static>))
|
(quote!(::oxc_allocator::CloneIn), quote!(<'static>))
|
||||||
} else if ident == "GetSpan" {
|
} else if ident == "GetSpan" {
|
||||||
|
|
@ -76,20 +84,13 @@ fn assert_generated_derives(attrs: &[Attribute]) -> TokenStream {
|
||||||
} else {
|
} else {
|
||||||
invalid_derive(ident)
|
invalid_derive(ident)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: At this level we don't care if a trait is derived multiple times, It is the
|
#[cold]
|
||||||
// responsibility of the `ast_tools` to raise errors for those.
|
fn invalid_derive(ident: &Ident) -> ! {
|
||||||
let assertion =
|
panic!(
|
||||||
attrs.iter().filter(|attr| attr.path().is_ident("generate_derive")).flat_map(parse).map(
|
"Invalid derive trait(generate_derive): {ident}.\n\
|
||||||
|derive| {
|
Help: If you are trying to implement a new `generate_derive` trait, \
|
||||||
let (abs_derive, generics) = abs_trait(&derive);
|
make sure to add it to the list in `abs_trait` function."
|
||||||
quote! {{
|
)
|
||||||
// NOTE: these are wrapped in a scope to avoid the need for unique identifiers.
|
|
||||||
trait AssertionTrait: #abs_derive #generics {}
|
|
||||||
impl<T: #derive #generics> AssertionTrait for T {}
|
|
||||||
}}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
quote!(const _: () = { #(#assertion)* };)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue