Skip to content

Commit

Permalink
refactor(ast_tools): reduce macro usage (#6895)
Browse files Browse the repository at this point in the history
1. Reduce the amount of code in `define_derive!` and `define_generator!` macros. This makes the code easier to read, and gives type hints in IDE.
2. Remove `generated_header!` macro and insert header as a blanket action, instead of repeated code in every generator.
  • Loading branch information
overlookmotel committed Oct 25, 2024
1 parent 5b21eb5 commit 3e7507f
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 188 deletions.
3 changes: 2 additions & 1 deletion crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! AST node factories
// Auto-generated code, DO NOT EDIT DIRECTLY!
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/ast_builder.rs`

//! AST node factories
#![allow(
clippy::default_trait_access,
clippy::too_many_arguments,
Expand Down
28 changes: 15 additions & 13 deletions tasks/ast_tools/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,20 @@ impl AstCodegen {
}
}

/// Creates a generated file warning + required information for a generated file.
macro_rules! generated_header {
() => {{
let file = file!().replace("\\", "/");
// TODO add generation date, AST source hash, etc here.
let edit_comment = format!("@ To edit this generated file you have to edit `{file}`");
quote::quote! {
//!@ Auto-generated code, DO NOT EDIT DIRECTLY!
#![doc = #edit_comment]
//!@@line_break
}
}};
/// Implemented by `define_derive!` and `define_generator!` macros
pub trait CodegenBase {
fn file_path() -> &'static str;
}

pub(crate) use generated_header;
/// Creates a generated file warning + required information for a generated file.
pub fn generate_header(file_path: &str) -> TokenStream {
let file_path = file_path.replace('\\', "/");

// TODO: Add generation date, AST source hash, etc here.
let edit_comment = format!("@ To edit this generated file you have to edit `{file_path}`");
quote::quote! {
//!@ Auto-generated code, DO NOT EDIT DIRECTLY!
#![doc = #edit_comment]
//!@@line_break
}
}
8 changes: 4 additions & 4 deletions tasks/ast_tools/src/derives/clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::Ident;

use super::{define_derive, Derive, DeriveOutput};
use super::{define_derive, Derive};
use crate::{
codegen::LateCtx,
markers::CloneInAttribute,
schema::{EnumDef, GetIdent, StructDef, TypeDef},
};

define_derive! {
pub struct DeriveCloneIn;
}
pub struct DeriveCloneIn;

define_derive!(DeriveCloneIn);

impl Derive for DeriveCloneIn {
fn trait_name() -> &'static str {
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_tools/src/derives/content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::quote;

use super::{define_derive, Derive, DeriveOutput};
use super::{define_derive, Derive};
use crate::{
codegen::LateCtx,
schema::{EnumDef, GetGenerics, StructDef, ToType, TypeDef},
util::ToIdent,
};

define_derive! {
pub struct DeriveContentEq;
}
pub struct DeriveContentEq;

define_derive!(DeriveContentEq);

const IGNORE_FIELDS: [(/* field name */ &str, /* field type */ &str); 6] = [
("span", "Span"),
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_tools/src/derives/content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::quote;

use super::{define_derive, Derive, DeriveOutput};
use super::{define_derive, Derive};
use crate::{
codegen::LateCtx,
schema::{EnumDef, GetGenerics, StructDef, ToType, TypeDef},
util::ToIdent,
};

define_derive! {
pub struct DeriveContentHash;
}
pub struct DeriveContentHash;

define_derive!(DeriveContentHash);

const IGNORE_FIELD_TYPES: [/* type name */ &str; 4] = [
"Span",
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::quote;

use super::{define_derive, Derive, DeriveOutput};
use super::{define_derive, Derive};
use crate::{
codegen::LateCtx,
markers::ESTreeStructAttribute,
Expand All @@ -12,9 +12,9 @@ use crate::{
},
};

define_derive! {
pub struct DeriveESTree;
}
pub struct DeriveESTree;

define_derive!(DeriveESTree);

impl Derive for DeriveESTree {
fn trait_name() -> &'static str {
Expand Down
14 changes: 7 additions & 7 deletions tasks/ast_tools/src/derives/get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;

use super::{define_derive, Derive, DeriveOutput};
use super::{define_derive, Derive};
use crate::{
codegen::LateCtx,
schema::{EnumDef, GetGenerics, StructDef, ToType, TypeDef},
util::{ToIdent, TypeWrapper},
};

define_derive! {
pub struct DeriveGetSpan;
}
pub struct DeriveGetSpan;

define_derive!(DeriveGetSpan);

impl Derive for DeriveGetSpan {
fn trait_name() -> &'static str {
Expand Down Expand Up @@ -47,9 +47,9 @@ impl Derive for DeriveGetSpan {
}
}

define_derive! {
pub struct DeriveGetSpanMut;
}
pub struct DeriveGetSpanMut;

define_derive!(DeriveGetSpanMut);

impl Derive for DeriveGetSpanMut {
fn trait_name() -> &'static str {
Expand Down
Loading

0 comments on commit 3e7507f

Please sign in to comment.