mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(ast_tools): shorten code by hoisting imports (#6903)
Pure refactor.
This commit is contained in:
parent
07dcc0cae3
commit
45333bc8d2
8 changed files with 54 additions and 54 deletions
|
|
@ -1,7 +1,9 @@
|
|||
use convert_case::{Case, Casing};
|
||||
use itertools::Itertools;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use syn::{parse_str, ItemUse};
|
||||
|
||||
use crate::{
|
||||
codegen::LateCtx,
|
||||
|
|
@ -49,15 +51,14 @@ pub trait Derive {
|
|||
.chain(it.strip_suffix("::mod").unwrap_or(it).split("::").skip(1))
|
||||
.chain(["*"])
|
||||
.join("::");
|
||||
let use_module: syn::ItemUse =
|
||||
syn::parse_str(format!("use {local_path};").as_str()).unwrap();
|
||||
quote::quote! {
|
||||
let use_module: ItemUse = parse_str(format!("use {local_path};").as_str()).unwrap();
|
||||
quote! {
|
||||
///@@line_break
|
||||
#use_module
|
||||
}
|
||||
});
|
||||
|
||||
quote::quote! {
|
||||
quote! {
|
||||
#prelude
|
||||
|
||||
#(#use_modules)*
|
||||
|
|
@ -103,7 +104,7 @@ pub trait Derive {
|
|||
tokens: Self::template(
|
||||
modules,
|
||||
streams.into_iter().fold(TokenStream::new(), |mut acc, it| {
|
||||
acc.extend(quote::quote! {
|
||||
acc.extend(quote! {
|
||||
///@@line_break
|
||||
});
|
||||
acc.extend(it);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use syn::{
|
|||
parenthesized,
|
||||
parse::{Parse, ParseStream},
|
||||
parse2,
|
||||
punctuated::Punctuated,
|
||||
punctuated::{self, Punctuated},
|
||||
spanned::Spanned,
|
||||
token, Attribute, Expr, Ident, LitStr, Meta, MetaNameValue, Token,
|
||||
};
|
||||
|
|
@ -38,7 +38,7 @@ impl Parse for VisitArg {
|
|||
pub struct VisitArgs(Punctuated<VisitArg, Token![,]>);
|
||||
|
||||
impl IntoIterator for VisitArgs {
|
||||
type IntoIter = syn::punctuated::IntoIter<Self::Item>;
|
||||
type IntoIter = punctuated::IntoIter<Self::Item>;
|
||||
type Item = VisitArg;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ fn generate_header(generator_path: &str) -> TokenStream {
|
|||
|
||||
// TODO: Add generation date, AST source hash, etc here.
|
||||
let edit_comment = format!("@ To edit this generated file you have to edit `{generator_path}`");
|
||||
quote::quote! {
|
||||
quote! {
|
||||
//!@ Auto-generated code, DO NOT EDIT DIRECTLY!
|
||||
#![doc = #edit_comment]
|
||||
//!@@line_break
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use serde::Serialize;
|
||||
use syn::Ident;
|
||||
|
||||
use super::{with_either, TypeName};
|
||||
use crate::{
|
||||
|
|
@ -120,7 +121,7 @@ pub struct VariantDef {
|
|||
}
|
||||
|
||||
impl VariantDef {
|
||||
pub fn ident(&self) -> syn::Ident {
|
||||
pub fn ident(&self) -> Ident {
|
||||
self.name.to_ident()
|
||||
}
|
||||
|
||||
|
|
@ -176,7 +177,7 @@ impl From<&syn::Visibility> for Visibility {
|
|||
}
|
||||
|
||||
impl FieldDef {
|
||||
pub fn ident(&self) -> Option<syn::Ident> {
|
||||
pub fn ident(&self) -> Option<Ident> {
|
||||
self.name.as_ref().map(ToIdent::to_ident)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use syn::parse_quote;
|
||||
use syn::{parse_quote, Generics};
|
||||
|
||||
use super::{
|
||||
defs::{EnumDef, StructDef, TypeDef},
|
||||
|
|
@ -10,7 +10,7 @@ pub trait GetGenerics {
|
|||
false
|
||||
}
|
||||
|
||||
fn generics(&self) -> Option<syn::Generics> {
|
||||
fn generics(&self) -> Option<Generics> {
|
||||
if self.has_lifetime() {
|
||||
Some(parse_quote!(<'a>))
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use syn::Ident;
|
||||
|
||||
use super::{
|
||||
defs::{EnumDef, StructDef, TypeDef},
|
||||
with_either,
|
||||
|
|
@ -5,23 +7,23 @@ use super::{
|
|||
use crate::util::ToIdent;
|
||||
|
||||
pub trait GetIdent {
|
||||
fn ident(&self) -> syn::Ident;
|
||||
fn ident(&self) -> Ident;
|
||||
}
|
||||
|
||||
impl GetIdent for TypeDef {
|
||||
fn ident(&self) -> syn::Ident {
|
||||
fn ident(&self) -> Ident {
|
||||
with_either!(self, it => it.ident())
|
||||
}
|
||||
}
|
||||
|
||||
impl GetIdent for StructDef {
|
||||
fn ident(&self) -> syn::Ident {
|
||||
fn ident(&self) -> Ident {
|
||||
self.name.to_ident()
|
||||
}
|
||||
}
|
||||
|
||||
impl GetIdent for EnumDef {
|
||||
fn ident(&self) -> syn::Ident {
|
||||
fn ident(&self) -> Ident {
|
||||
self.name.to_ident()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,13 @@ use std::fmt;
|
|||
use quote::ToTokens;
|
||||
use rustc_hash::FxHashSet;
|
||||
use serde::Serialize;
|
||||
use syn::{
|
||||
punctuated::Punctuated, Attribute, Expr, ExprLit, Field, Ident, Lit, Meta, MetaNameValue,
|
||||
Token, Type, Variant,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
codegen,
|
||||
codegen::EarlyCtx,
|
||||
layout::KnownLayout,
|
||||
markers::{
|
||||
get_derive_attributes, get_estree_attribute, get_scope_attribute, get_scope_markers,
|
||||
|
|
@ -121,18 +125,18 @@ impl<'a> IntoIterator for &'a Schema {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_struct_outer_markers(attrs: &Vec<syn::Attribute>) -> Result<StructOuterMarkers> {
|
||||
fn parse_struct_outer_markers(attrs: &Vec<Attribute>) -> Result<StructOuterMarkers> {
|
||||
Ok(StructOuterMarkers {
|
||||
scope: get_scope_attribute(attrs).transpose()?,
|
||||
estree: get_estree_attribute(attrs).transpose()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_enum_outer_markers(attrs: &Vec<syn::Attribute>) -> Result<EnumOuterMarkers> {
|
||||
fn parse_enum_outer_markers(attrs: &Vec<Attribute>) -> Result<EnumOuterMarkers> {
|
||||
Ok(EnumOuterMarkers { estree: get_estree_attribute(attrs).transpose()?.unwrap_or_default() })
|
||||
}
|
||||
|
||||
fn parse_inner_markers(attrs: &Vec<syn::Attribute>) -> Result<InnerMarkers> {
|
||||
fn parse_inner_markers(attrs: &Vec<Attribute>) -> Result<InnerMarkers> {
|
||||
Ok(InnerMarkers {
|
||||
span: attrs.iter().any(|a| a.path().is_ident("span")),
|
||||
visit: get_visit_markers(attrs)?,
|
||||
|
|
@ -142,7 +146,7 @@ fn parse_inner_markers(attrs: &Vec<syn::Attribute>) -> Result<InnerMarkers> {
|
|||
}
|
||||
|
||||
// lower `AstType` to `TypeDef`.
|
||||
pub fn lower_ast_types(ctx: &codegen::EarlyCtx) -> Schema {
|
||||
pub fn lower_ast_types(ctx: &EarlyCtx) -> Schema {
|
||||
let defs = ctx
|
||||
.mods()
|
||||
.borrow()
|
||||
|
|
@ -153,7 +157,7 @@ pub fn lower_ast_types(ctx: &codegen::EarlyCtx) -> Schema {
|
|||
Schema { defs }
|
||||
}
|
||||
|
||||
fn lower_ast_type(ty: &rust::AstType, ctx: &codegen::EarlyCtx) -> TypeDef {
|
||||
fn lower_ast_type(ty: &rust::AstType, ctx: &EarlyCtx) -> TypeDef {
|
||||
match ty {
|
||||
rust::AstType::Enum(it) => TypeDef::Enum(lower_ast_enum(it, ctx)),
|
||||
rust::AstType::Struct(it) => TypeDef::Struct(lower_ast_struct(it, ctx)),
|
||||
|
|
@ -161,7 +165,7 @@ fn lower_ast_type(ty: &rust::AstType, ctx: &codegen::EarlyCtx) -> TypeDef {
|
|||
}
|
||||
}
|
||||
|
||||
fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &codegen::EarlyCtx) -> EnumDef {
|
||||
fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &EarlyCtx) -> EnumDef {
|
||||
let (size_64, align_64, offsets_64) = meta
|
||||
.layout_64
|
||||
.clone()
|
||||
|
|
@ -199,10 +203,7 @@ fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &codegen::Ea
|
|||
}
|
||||
}
|
||||
|
||||
fn lower_ast_struct(
|
||||
it @ rust::Struct { item, meta }: &rust::Struct,
|
||||
ctx: &codegen::EarlyCtx,
|
||||
) -> StructDef {
|
||||
fn lower_ast_struct(it @ rust::Struct { item, meta }: &rust::Struct, ctx: &EarlyCtx) -> StructDef {
|
||||
let (size_64, align_64, offsets_64) = meta
|
||||
.layout_64
|
||||
.clone()
|
||||
|
|
@ -234,7 +235,7 @@ fn lower_ast_struct(
|
|||
}
|
||||
}
|
||||
|
||||
fn lower_variant<F>(variant: &syn::Variant, enum_dbg_name: F, ctx: &codegen::EarlyCtx) -> VariantDef
|
||||
fn lower_variant<F>(variant: &Variant, enum_dbg_name: F, ctx: &EarlyCtx) -> VariantDef
|
||||
where
|
||||
F: Fn() -> String,
|
||||
{
|
||||
|
|
@ -243,7 +244,7 @@ where
|
|||
discriminant: variant.discriminant.as_ref().map_or_else(
|
||||
|| panic!("expected explicit enum discriminants on {}", enum_dbg_name()),
|
||||
|(_, disc)| match disc {
|
||||
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }) => {
|
||||
Expr::Lit(ExprLit { lit: Lit::Int(lit), .. }) => {
|
||||
lit.base10_parse().expect("invalid base10 enum discriminant")
|
||||
}
|
||||
_ => panic!("invalid enum discriminant {:?} on {}", disc, enum_dbg_name()),
|
||||
|
|
@ -254,7 +255,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn lower_inherit(inherit: &rust::Inherit, ctx: &codegen::EarlyCtx) -> InheritDef {
|
||||
fn lower_inherit(inherit: &rust::Inherit, ctx: &EarlyCtx) -> InheritDef {
|
||||
match inherit {
|
||||
rust::Inherit::Linked { super_, variants } => InheritDef {
|
||||
super_: create_type_ref(super_, ctx),
|
||||
|
|
@ -269,7 +270,7 @@ fn lower_inherit(inherit: &rust::Inherit, ctx: &codegen::EarlyCtx) -> InheritDef
|
|||
}
|
||||
}
|
||||
|
||||
fn lower_field(field: &syn::Field, ctx: &codegen::EarlyCtx) -> FieldDef {
|
||||
fn lower_field(field: &Field, ctx: &EarlyCtx) -> FieldDef {
|
||||
FieldDef {
|
||||
name: field
|
||||
.ident
|
||||
|
|
@ -282,7 +283,7 @@ fn lower_field(field: &syn::Field, ctx: &codegen::EarlyCtx) -> FieldDef {
|
|||
}
|
||||
}
|
||||
|
||||
fn create_type_ref(ty: &syn::Type, ctx: &codegen::EarlyCtx) -> TypeRef {
|
||||
fn create_type_ref(ty: &Type, ctx: &EarlyCtx) -> TypeRef {
|
||||
let ident = ty.get_ident();
|
||||
let id = ident.as_ident().and_then(|id| ctx.type_id(&id.to_string()));
|
||||
let transparent_id = ctx.type_id(&ident.inner_ident().to_string());
|
||||
|
|
@ -296,21 +297,16 @@ fn create_type_ref(ty: &syn::Type, ctx: &codegen::EarlyCtx) -> TypeRef {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_docs(attrs: &[syn::Attribute]) -> Vec<String> {
|
||||
fn get_docs(attrs: &[Attribute]) -> Vec<String> {
|
||||
attrs
|
||||
.iter()
|
||||
.filter_map(|attr| {
|
||||
if let syn::Meta::NameValue(syn::MetaNameValue {
|
||||
path,
|
||||
value: syn::Expr::Lit(lit),
|
||||
..
|
||||
}) = &attr.meta
|
||||
{
|
||||
if let Meta::NameValue(MetaNameValue { path, value: Expr::Lit(lit), .. }) = &attr.meta {
|
||||
if !path.is_ident("doc") {
|
||||
return None;
|
||||
}
|
||||
match &lit.lit {
|
||||
syn::Lit::Str(lit) => Some(lit.value().trim().to_string()),
|
||||
Lit::Str(lit) => Some(lit.value().trim().to_string()),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
|
|
@ -320,15 +316,15 @@ fn get_docs(attrs: &[syn::Attribute]) -> Vec<String> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn parse_generate_derive(attrs: &[syn::Attribute]) -> Vec<String> {
|
||||
fn parse_generate_derive(attrs: &[Attribute]) -> Vec<String> {
|
||||
let mut derives = FxHashSet::default();
|
||||
for attr in attrs {
|
||||
if !attr.path().is_ident("generate_derive") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let args: syn::punctuated::Punctuated<syn::Ident, syn::Token![,]> =
|
||||
attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated).unwrap();
|
||||
let args: Punctuated<Ident, Token![,]> =
|
||||
attr.parse_args_with(Punctuated::parse_terminated).unwrap();
|
||||
|
||||
for arg in args {
|
||||
derives.insert(arg.to_string());
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::ToTokens;
|
||||
use syn::parse_quote;
|
||||
use syn::{parse_quote, parse_str, Type};
|
||||
|
||||
use super::{
|
||||
defs::{EnumDef, StructDef, TypeDef, TypeRef},
|
||||
|
|
@ -8,21 +8,21 @@ use super::{
|
|||
};
|
||||
|
||||
pub trait ToType {
|
||||
fn to_type(&self) -> syn::Type;
|
||||
fn to_type_elide(&self) -> syn::Type;
|
||||
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> syn::Type;
|
||||
fn to_type(&self) -> Type;
|
||||
fn to_type_elide(&self) -> Type;
|
||||
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> Type;
|
||||
}
|
||||
|
||||
impl ToType for TypeRef {
|
||||
fn to_type(&self) -> syn::Type {
|
||||
syn::parse_str(self.raw()).unwrap()
|
||||
fn to_type(&self) -> Type {
|
||||
parse_str(self.raw()).unwrap()
|
||||
}
|
||||
|
||||
fn to_type_elide(&self) -> syn::Type {
|
||||
fn to_type_elide(&self) -> Type {
|
||||
self.to_type_with_explicit_generics(proc_macro2::TokenStream::default())
|
||||
}
|
||||
|
||||
fn to_type_with_explicit_generics(&self, generics: proc_macro2::TokenStream) -> syn::Type {
|
||||
fn to_type_with_explicit_generics(&self, generics: proc_macro2::TokenStream) -> Type {
|
||||
let ident = self.name().first_ident();
|
||||
parse_quote!(#ident #generics)
|
||||
}
|
||||
|
|
@ -38,15 +38,15 @@ macro_rules! auto_impl_to_type {
|
|||
($($ty:ty,)+) => (
|
||||
$(
|
||||
impl ToType for $ty {
|
||||
fn to_type(&self) -> syn::Type {
|
||||
fn to_type(&self) -> Type {
|
||||
self.to_type_with_explicit_generics(self.generics().to_token_stream())
|
||||
}
|
||||
|
||||
fn to_type_elide(&self) -> syn::Type {
|
||||
fn to_type_elide(&self) -> Type {
|
||||
self.to_type_with_explicit_generics(TokenStream::default())
|
||||
}
|
||||
|
||||
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> syn::Type {
|
||||
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> Type {
|
||||
let name = self.ident();
|
||||
parse_quote!(#name #generics)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue