Skip to content

Commit

Permalink
refactor(ast_tools): move prelude above derive in Derives (#7471)
Browse files Browse the repository at this point in the history
Pure refactor. Prelude is printed first, so it makes sense to define it first.
  • Loading branch information
overlookmotel committed Nov 25, 2024
1 parent 7236d14 commit a68a217
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
14 changes: 7 additions & 7 deletions tasks/ast_tools/src/derives/clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ impl Derive for DeriveCloneIn {
"CloneIn"
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
}
}

fn prelude() -> TokenStream {
quote! {
#![allow(clippy::default_trait_access)]
Expand All @@ -34,6 +27,13 @@ impl Derive for DeriveCloneIn {
use oxc_allocator::{Allocator, CloneIn};
}
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
}
}
}

fn derive_enum(def: &EnumDef) -> TokenStream {
Expand Down
18 changes: 9 additions & 9 deletions tasks/ast_tools/src/derives/content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ impl Derive for DeriveContentEq {
"ContentEq"
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
let (other, body) = match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
};

impl_content_eq(def, other, &body)
}

fn prelude() -> TokenStream {
quote! {
// NOTE: writing long match expressions formats better than using `matches` macro.
Expand All @@ -45,6 +36,15 @@ impl Derive for DeriveContentEq {
use oxc_span::cmp::ContentEq;
}
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
let (other, body) = match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
};

impl_content_eq(def, other, &body)
}
}

fn derive_enum(def: &EnumDef) -> (&str, TokenStream) {
Expand Down
18 changes: 9 additions & 9 deletions tasks/ast_tools/src/derives/content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ impl Derive for DeriveContentHash {
"ContentHash"
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
let (hasher, body) = match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
};

impl_content_hash(def, hasher, &body)
}

fn prelude() -> TokenStream {
quote! {
#![allow(clippy::match_same_arms)]
Expand All @@ -45,6 +36,15 @@ impl Derive for DeriveContentHash {
use oxc_span::hash::ContentHash;
}
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
let (hasher, body) = match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
};

impl_content_hash(def, hasher, &body)
}
}

fn derive_enum(def: &EnumDef) -> (&str, TokenStream) {
Expand Down
18 changes: 9 additions & 9 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ impl Derive for DeriveESTree {
"estree".to_string()
}

fn prelude() -> TokenStream {
quote! {
#![allow(unused_imports, unused_mut, clippy::match_same_arms)]

///@@line_break
use serde::{Serialize, Serializer, ser::SerializeMap};
}
}

fn derive(&mut self, def: &TypeDef, schema: &Schema) -> TokenStream {
if let TypeDef::Struct(def) = def {
if def
Expand Down Expand Up @@ -54,15 +63,6 @@ impl Derive for DeriveESTree {
}
}
}

fn prelude() -> TokenStream {
quote! {
#![allow(unused_imports, unused_mut, clippy::match_same_arms)]

///@@line_break
use serde::{Serialize, Serializer, ser::SerializeMap};
}
}
}

fn serialize_struct(def: &StructDef, schema: &Schema) -> TokenStream {
Expand Down
36 changes: 18 additions & 18 deletions tasks/ast_tools/src/derives/get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ impl Derive for DeriveGetSpan {
"GetSpan"
}

fn prelude() -> TokenStream {
quote! {
#![allow(clippy::match_same_arms)]

///@@line_break
use oxc_span::{Span, GetSpan};
}
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
let self_type = quote!(&self);
let result_type = quote!(Span);
Expand All @@ -36,15 +45,6 @@ impl Derive for DeriveGetSpan {
reference,
)
}

fn prelude() -> TokenStream {
quote! {
#![allow(clippy::match_same_arms)]

///@@line_break
use oxc_span::{Span, GetSpan};
}
}
}

pub struct DeriveGetSpanMut;
Expand All @@ -56,6 +56,15 @@ impl Derive for DeriveGetSpanMut {
"GetSpanMut"
}

fn prelude() -> TokenStream {
quote! {
#![allow(clippy::match_same_arms)]

///@@line_break
use oxc_span::{Span, GetSpanMut};
}
}

fn derive(&mut self, def: &TypeDef, _: &Schema) -> TokenStream {
let self_type = quote!(&mut self);
let result_type = quote!(&mut Span);
Expand All @@ -74,15 +83,6 @@ impl Derive for DeriveGetSpanMut {
reference,
)
}

fn prelude() -> TokenStream {
quote! {
#![allow(clippy::match_same_arms)]

///@@line_break
use oxc_span::{Span, GetSpanMut};
}
}
}

#[expect(clippy::too_many_arguments)]
Expand Down
4 changes: 2 additions & 2 deletions tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ pub trait Derive {
Self::trait_name().to_case(Case::Snake)
}

fn derive(&mut self, def: &TypeDef, schema: &Schema) -> TokenStream;

fn prelude() -> TokenStream {
TokenStream::default()
}

fn derive(&mut self, def: &TypeDef, schema: &Schema) -> TokenStream;

// Standard methods

fn template(module_paths: Vec<&str>, impls: TokenStream) -> TokenStream {
Expand Down

0 comments on commit a68a217

Please sign in to comment.