Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ast_tools): reduce macro usage #6895

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading