Skip to content

Commit 862fe61

Browse files
committed
fix(ast, ast_codegen): use generate_derive instead of visitable for generating span derives.
1 parent 9a94c20 commit 862fe61

File tree

6 files changed

+59
-31
lines changed

6 files changed

+59
-31
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ pub enum PropertyKey<'a> {
426426
/// Represents the kind of property in an object literal or class.
427427
#[ast]
428428
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
429-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
429+
#[generate_derive(CloneIn)]
430430
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
431431
#[serde(rename_all = "camelCase")]
432432
pub enum PropertyKind {
@@ -487,7 +487,7 @@ pub struct TemplateElement<'a> {
487487
/// See [template-strings-cooked-vs-raw](https://exploringjs.com/js/book/ch_template-literals.html#template-strings-cooked-vs-raw)
488488
#[ast]
489489
#[derive(Debug, Hash)]
490-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
490+
#[generate_derive(CloneIn)]
491491
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
492492
pub struct TemplateElementValue<'a> {
493493
/// A raw interpretation where backslashes do not have special meaning.
@@ -1241,7 +1241,7 @@ pub struct VariableDeclaration<'a> {
12411241

12421242
#[ast]
12431243
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1244-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1244+
#[generate_derive(CloneIn)]
12451245
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
12461246
#[serde(rename_all = "camelCase")]
12471247
pub enum VariableDeclarationKind {
@@ -1730,7 +1730,7 @@ pub struct Function<'a> {
17301730

17311731
#[ast]
17321732
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1733-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1733+
#[generate_derive(CloneIn)]
17341734
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
17351735
pub enum FunctionType {
17361736
FunctionDeclaration = 0,
@@ -1774,7 +1774,7 @@ pub struct FormalParameter<'a> {
17741774

17751775
#[ast]
17761776
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1777-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1777+
#[generate_derive(CloneIn)]
17781778
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
17791779
pub enum FormalParameterKind {
17801780
/// <https://tc39.es/ecma262/#prod-FormalParameters>
@@ -1912,7 +1912,7 @@ pub struct Class<'a> {
19121912

19131913
#[ast]
19141914
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1915-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1915+
#[generate_derive(CloneIn)]
19161916
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
19171917
pub enum ClassType {
19181918
/// Class declaration statement
@@ -2012,7 +2012,7 @@ pub struct MethodDefinition<'a> {
20122012

20132013
#[ast]
20142014
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2015-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
2015+
#[generate_derive(CloneIn)]
20162016
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
20172017
pub enum MethodDefinitionType {
20182018
MethodDefinition = 0,
@@ -2103,7 +2103,7 @@ pub struct PropertyDefinition<'a> {
21032103

21042104
#[ast]
21052105
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2106-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
2106+
#[generate_derive(CloneIn)]
21072107
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
21082108
pub enum PropertyDefinitionType {
21092109
PropertyDefinition = 0,
@@ -2112,7 +2112,7 @@ pub enum PropertyDefinitionType {
21122112

21132113
#[ast]
21142114
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2115-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
2115+
#[generate_derive(CloneIn)]
21162116
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
21172117
#[serde(rename_all = "camelCase")]
21182118
pub enum MethodDefinitionKind {
@@ -2228,7 +2228,7 @@ pub use match_module_declaration;
22282228

22292229
#[ast]
22302230
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2231-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
2231+
#[generate_derive(CloneIn)]
22322232
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
22332233
pub enum AccessorPropertyType {
22342234
AccessorProperty = 0,

crates/oxc_ast/src/ast/literal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub struct RegExpLiteral<'a> {
103103
/// <https://tc39.es/ecma262/multipage/text-processing.html#sec-regexp-regular-expression-objects>
104104
#[ast]
105105
#[derive(Debug, Clone, Hash)]
106-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
106+
#[generate_derive(CloneIn)]
107107
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
108108
pub struct RegExp<'a> {
109109
/// The regex pattern between the slashes
@@ -114,7 +114,7 @@ pub struct RegExp<'a> {
114114

115115
#[ast]
116116
#[derive(Debug, Clone, Hash)]
117-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
117+
#[generate_derive(CloneIn)]
118118
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
119119
pub struct EmptyObject;
120120

crates/oxc_ast/src/ast/ts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ pub struct TSTypeOperator<'a> {
383383

384384
#[ast]
385385
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
386-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
386+
#[generate_derive(CloneIn)]
387387
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
388388
#[serde(rename_all = "camelCase")]
389389
pub enum TSTypeOperatorOperator {
@@ -762,7 +762,7 @@ pub struct TSTypeAliasDeclaration<'a> {
762762

763763
#[ast]
764764
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
765-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
765+
#[generate_derive(CloneIn)]
766766
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
767767
#[serde(rename_all = "camelCase")]
768768
pub enum TSAccessibility {
@@ -884,7 +884,7 @@ pub struct TSCallSignatureDeclaration<'a> {
884884

885885
#[ast]
886886
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
887-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
887+
#[generate_derive(CloneIn)]
888888
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
889889
#[serde(rename_all = "camelCase")]
890890
pub enum TSMethodSignatureKind {
@@ -1006,7 +1006,7 @@ pub struct TSModuleDeclaration<'a> {
10061006

10071007
#[ast]
10081008
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1009-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1009+
#[generate_derive(CloneIn)]
10101010
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
10111011
#[serde(rename_all = "camelCase")]
10121012
pub enum TSModuleDeclarationKind {
@@ -1203,7 +1203,7 @@ pub struct TSMappedType<'a> {
12031203

12041204
#[ast]
12051205
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1206-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1206+
#[generate_derive(CloneIn)]
12071207
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
12081208
#[serde(rename_all = "camelCase")]
12091209
pub enum TSMappedTypeModifierOperator {
@@ -1394,7 +1394,7 @@ pub struct TSInstantiationExpression<'a> {
13941394
/// See [TypeScript - Type-Only Imports and Exports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html)
13951395
#[ast]
13961396
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1397-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1397+
#[generate_derive(CloneIn)]
13981398
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
13991399
#[serde(rename_all = "camelCase")]
14001400
pub enum ImportOrExportKind {

crates/oxc_ast/src/generated/derive_get_span.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,20 @@ impl<'a> GetSpan for JSXFragment<'a> {
20222022
}
20232023
}
20242024

2025+
impl GetSpan for JSXOpeningFragment {
2026+
#[inline]
2027+
fn span(&self) -> Span {
2028+
self.span
2029+
}
2030+
}
2031+
2032+
impl GetSpan for JSXClosingFragment {
2033+
#[inline]
2034+
fn span(&self) -> Span {
2035+
self.span
2036+
}
2037+
}
2038+
20252039
impl<'a> GetSpan for JSXElementName<'a> {
20262040
fn span(&self) -> Span {
20272041
match self {

crates/oxc_ast/src/generated/derive_get_span_mut.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,20 @@ impl<'a> GetSpanMut for JSXFragment<'a> {
20222022
}
20232023
}
20242024

2025+
impl GetSpanMut for JSXOpeningFragment {
2026+
#[inline]
2027+
fn span_mut(&mut self) -> &mut Span {
2028+
&mut self.span
2029+
}
2030+
}
2031+
2032+
impl GetSpanMut for JSXClosingFragment {
2033+
#[inline]
2034+
fn span_mut(&mut self) -> &mut Span {
2035+
&mut self.span
2036+
}
2037+
}
2038+
20252039
impl<'a> GetSpanMut for JSXElementName<'a> {
20262040
fn span_mut(&mut self) -> &mut Span {
20272041
match self {

tasks/ast_codegen/src/generators/derive_get_span.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use proc_macro2::TokenStream;
2-
use quote::{format_ident, quote};
2+
use quote::quote;
33
use syn::Ident;
44

55
use crate::{
@@ -18,12 +18,10 @@ define_generator! {
1818

1919
impl Generator for DeriveGetSpan {
2020
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
21-
let trait_name = format_ident!("GetSpan");
22-
let method_name = format_ident!("span");
2321
let self_type = quote!(&self);
2422
let result_type = quote!(Span);
2523
let result_expr = quote!(self.span);
26-
let out = derive(&trait_name, &method_name, &self_type, &result_type, &result_expr, ctx);
24+
let out = derive("GetSpan", "span", &self_type, &result_type, &result_expr, ctx);
2725

2826
GeneratorOutput::Stream((output(crate::AST_CRATE, "derive_get_span.rs"), out))
2927
}
@@ -35,33 +33,35 @@ define_generator! {
3533

3634
impl Generator for DeriveGetSpanMut {
3735
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
38-
let trait_name = format_ident!("GetSpanMut");
39-
let method_name = format_ident!("span_mut");
4036
let self_type = quote!(&mut self);
4137
let result_type = quote!(&mut Span);
4238
let result_expr = quote!(&mut self.span);
43-
let out = derive(&trait_name, &method_name, &self_type, &result_type, &result_expr, ctx);
39+
let out = derive("GetSpanMut", "span_mut", &self_type, &result_type, &result_expr, ctx);
4440

4541
GeneratorOutput::Stream((output(crate::AST_CRATE, "derive_get_span_mut.rs"), out))
4642
}
4743
}
4844

4945
fn derive(
50-
trait_name: &Ident,
51-
method_name: &Ident,
46+
trait_name: &str,
47+
method_name: &str,
5248
self_type: &TokenStream,
5349
result_type: &TokenStream,
5450
result_expr: &TokenStream,
5551
ctx: &LateCtx,
5652
) -> TokenStream {
53+
let trait_ident = trait_name.to_ident();
54+
let method_ident = method_name.to_ident();
5755
let impls: Vec<TokenStream> = ctx
5856
.schema()
5957
.into_iter()
60-
.filter(|def| def.visitable())
58+
.filter(|def| def.generates_derive(trait_name))
6159
.map(|def| match &def {
62-
TypeDef::Enum(def) => derive_enum(def, trait_name, method_name, self_type, result_type),
60+
TypeDef::Enum(def) => {
61+
derive_enum(def, &trait_ident, &method_ident, self_type, result_type)
62+
}
6363
TypeDef::Struct(def) => {
64-
derive_struct(def, trait_name, method_name, self_type, result_type, result_expr)
64+
derive_struct(def, &trait_ident, &method_ident, self_type, result_type, result_expr)
6565
}
6666
})
6767
.collect();
@@ -74,7 +74,7 @@ fn derive(
7474
insert!("#![allow(clippy::match_same_arms)]");
7575
endl!();
7676

77-
use oxc_span::{#trait_name, Span};
77+
use oxc_span::{#trait_ident, Span};
7878
endl!();
7979

8080
use crate::ast::*;

0 commit comments

Comments
 (0)