From a822c9db0a4b1990ac6d5a63d0446b006cdb41f5 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:47:44 +0000 Subject: [PATCH] fix(ast): serialize `JSXElementName` to estree (#5882) closes #5354 --- crates/oxc_ast/src/ast/jsx.rs | 2 +- crates/oxc_ast/src/serialize.rs | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/oxc_ast/src/ast/jsx.rs b/crates/oxc_ast/src/ast/jsx.rs index 3eaf497aafc3f..9007fa32a157d 100644 --- a/crates/oxc_ast/src/ast/jsx.rs +++ b/crates/oxc_ast/src/ast/jsx.rs @@ -168,7 +168,7 @@ pub struct JSXClosingFragment { #[ast(visit)] #[derive(Debug)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)] -#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] +#[cfg_attr(feature = "serialize", derive(Tsify))] #[serde(untagged)] pub enum JSXElementName<'a> { /// `
` diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index 934deaa417966..da9c99e67e07e 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -8,9 +8,9 @@ use serde::{ use crate::ast::{ ArrayAssignmentTarget, ArrayPattern, AssignmentTargetMaybeDefault, AssignmentTargetProperty, AssignmentTargetRest, BindingPattern, BindingPatternKind, BindingProperty, BindingRestElement, - Directive, Elision, FormalParameter, FormalParameterKind, FormalParameters, - ObjectAssignmentTarget, ObjectPattern, Program, RegExpFlags, Statement, StringLiteral, - TSModuleBlock, TSTypeAnnotation, + Directive, Elision, FormalParameter, FormalParameterKind, FormalParameters, JSXElementName, + JSXIdentifier, ObjectAssignmentTarget, ObjectPattern, Program, RegExpFlags, Statement, + StringLiteral, TSModuleBlock, TSTypeAnnotation, }; pub struct EcmaFormatter; @@ -249,3 +249,19 @@ struct DirectiveAsStatement<'a, 'b> { span: Span, expression: &'b StringLiteral<'a>, } + +impl<'a> Serialize for JSXElementName<'a> { + fn serialize(&self, serializer: S) -> Result { + match self { + Self::Identifier(ident) => ident.serialize(serializer), + Self::IdentifierReference(ident) => { + JSXIdentifier { span: ident.span, name: ident.name.clone() }.serialize(serializer) + } + Self::NamespacedName(name) => name.serialize(serializer), + Self::MemberExpression(expr) => expr.serialize(serializer), + Self::ThisExpression(expr) => { + JSXIdentifier { span: expr.span, name: "this".into() }.serialize(serializer) + } + } + } +}