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

fix(ast, ast_codegen): use generate_derive instead of visitable for generating span derives. #4747

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
20 changes: 10 additions & 10 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ pub enum PropertyKey<'a> {
/// Represents the kind of property in an object literal or class.
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum PropertyKind {
Expand Down Expand Up @@ -487,7 +487,7 @@ pub struct TemplateElement<'a> {
/// See [template-strings-cooked-vs-raw](https://exploringjs.com/js/book/ch_template-literals.html#template-strings-cooked-vs-raw)
#[ast]
#[derive(Debug, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct TemplateElementValue<'a> {
/// A raw interpretation where backslashes do not have special meaning.
Expand Down Expand Up @@ -1241,7 +1241,7 @@ pub struct VariableDeclaration<'a> {

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

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

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

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

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

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

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

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum AccessorPropertyType {
AccessorProperty = 0,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct RegExpLiteral<'a> {
/// <https://tc39.es/ecma262/multipage/text-processing.html#sec-regexp-regular-expression-objects>
#[ast]
#[derive(Debug, Clone, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct RegExp<'a> {
/// The regex pattern between the slashes
Expand All @@ -114,7 +114,7 @@ pub struct RegExp<'a> {

#[ast]
#[derive(Debug, Clone, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct EmptyObject;

Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ pub struct TSTypeOperator<'a> {

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

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

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

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

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSMappedTypeModifierOperator {
Expand Down Expand Up @@ -1394,7 +1394,7 @@ pub struct TSInstantiationExpression<'a> {
/// See [TypeScript - Type-Only Imports and Exports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html)
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[generate_derive(CloneIn)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum ImportOrExportKind {
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_ast/src/generated/derive_get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,20 @@ impl<'a> GetSpan for JSXFragment<'a> {
}
}

impl GetSpan for JSXOpeningFragment {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl GetSpan for JSXClosingFragment {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl<'a> GetSpan for JSXElementName<'a> {
fn span(&self) -> Span {
match self {
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_ast/src/generated/derive_get_span_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,20 @@ impl<'a> GetSpanMut for JSXFragment<'a> {
}
}

impl GetSpanMut for JSXOpeningFragment {
#[inline]
fn span_mut(&mut self) -> &mut Span {
&mut self.span
}
}

impl GetSpanMut for JSXClosingFragment {
#[inline]
fn span_mut(&mut self) -> &mut Span {
&mut self.span
}
}

impl<'a> GetSpanMut for JSXElementName<'a> {
fn span_mut(&mut self) -> &mut Span {
match self {
Expand Down
26 changes: 13 additions & 13 deletions tasks/ast_codegen/src/generators/derive_get_span.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use quote::quote;
use syn::Ident;

use crate::{
Expand All @@ -18,12 +18,10 @@ define_generator! {

impl Generator for DeriveGetSpan {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let trait_name = format_ident!("GetSpan");
let method_name = format_ident!("span");
let self_type = quote!(&self);
let result_type = quote!(Span);
let result_expr = quote!(self.span);
let out = derive(&trait_name, &method_name, &self_type, &result_type, &result_expr, ctx);
let out = derive("GetSpan", "span", &self_type, &result_type, &result_expr, ctx);

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

impl Generator for DeriveGetSpanMut {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let trait_name = format_ident!("GetSpanMut");
let method_name = format_ident!("span_mut");
let self_type = quote!(&mut self);
let result_type = quote!(&mut Span);
let result_expr = quote!(&mut self.span);
let out = derive(&trait_name, &method_name, &self_type, &result_type, &result_expr, ctx);
let out = derive("GetSpanMut", "span_mut", &self_type, &result_type, &result_expr, ctx);

GeneratorOutput::Stream((output(crate::AST_CRATE, "derive_get_span_mut.rs"), out))
}
}

fn derive(
trait_name: &Ident,
method_name: &Ident,
trait_name: &str,
method_name: &str,
self_type: &TokenStream,
result_type: &TokenStream,
result_expr: &TokenStream,
ctx: &LateCtx,
) -> TokenStream {
let trait_ident = trait_name.to_ident();
let method_ident = method_name.to_ident();
let impls: Vec<TokenStream> = ctx
.schema()
.into_iter()
.filter(|def| def.visitable())
.filter(|def| def.generates_derive(trait_name))
.map(|def| match &def {
TypeDef::Enum(def) => derive_enum(def, trait_name, method_name, self_type, result_type),
TypeDef::Enum(def) => {
derive_enum(def, &trait_ident, &method_ident, self_type, result_type)
}
TypeDef::Struct(def) => {
derive_struct(def, trait_name, method_name, self_type, result_type, result_expr)
derive_struct(def, &trait_ident, &method_ident, self_type, result_type, result_expr)
}
})
.collect();
Expand All @@ -74,7 +74,7 @@ fn derive(
insert!("#![allow(clippy::match_same_arms)]");
endl!();

use oxc_span::{#trait_name, Span};
use oxc_span::{#trait_ident, Span};
endl!();

use crate::ast::*;
Expand Down