From 93e8747bde3f86fa3dc495f5fa24b6c63d8cd2d0 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 6 Feb 2025 17:04:28 +0900 Subject: [PATCH 1/2] fix(ast): fix `Prograrm.sourceType` serialization --- crates/oxc_ast/src/ast/js.rs | 1 + crates/oxc_ast/src/generated/derive_estree.rs | 5 ++++- crates/oxc_ast/src/serialize.rs | 16 +++++++++++++++- napi/parser/test/parse.test.ts | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 83b53aeca8253..26ac2e67821f3 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -31,6 +31,7 @@ use super::{macros::inherit_variants, *}; #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)] pub struct Program<'a> { pub span: Span, + #[estree(via = crate::serialize::ESTreeSourceType, ts_type = "SourceType")] pub source_type: SourceType, #[estree(skip)] pub source_text: &'a str, diff --git a/crates/oxc_ast/src/generated/derive_estree.rs b/crates/oxc_ast/src/generated/derive_estree.rs index 2cc3a335594ff..f5014d7f183c2 100644 --- a/crates/oxc_ast/src/generated/derive_estree.rs +++ b/crates/oxc_ast/src/generated/derive_estree.rs @@ -18,7 +18,10 @@ impl Serialize for Program<'_> { map.serialize_entry("type", "Program")?; map.serialize_entry("start", &self.span.start)?; map.serialize_entry("end", &self.span.end)?; - map.serialize_entry("sourceType", &self.source_type)?; + map.serialize_entry( + "sourceType", + &crate::serialize::ESTreeSourceType::from(&self.source_type), + )?; map.serialize_entry("hashbang", &self.hashbang)?; map.serialize_entry("directives", &self.directives)?; map.serialize_entry("body", &self.body)?; diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index 8ed74e7bf4ff1..e3e1a1a7f83ec 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -7,7 +7,7 @@ use serde::{ }; use oxc_allocator::{Box as ArenaBox, Vec as ArenaVec}; -use oxc_span::{Atom, Span}; +use oxc_span::{Atom, SourceType, Span}; use oxc_syntax::number::BigintBase; use crate::ast::{ @@ -323,3 +323,17 @@ impl Serialize for JSXMemberExpressionObject<'_> { } } } + +pub struct ESTreeSourceType<'a>(pub &'a SourceType); + +impl<'a> From<&'a SourceType> for ESTreeSourceType<'a> { + fn from(inner: &'a SourceType) -> Self { + Self(inner) + } +} + +impl Serialize for ESTreeSourceType<'_> { + fn serialize(&self, serializer: S) -> Result { + self.0.module_kind().serialize(serializer) + } +} diff --git a/napi/parser/test/parse.test.ts b/napi/parser/test/parse.test.ts index e921eafe507a5..81c566a323347 100644 --- a/napi/parser/test/parse.test.ts +++ b/napi/parser/test/parse.test.ts @@ -33,6 +33,7 @@ describe('parse', () => { `async function test(x, { y }, [ z ], ...rest) {}`, ); expect(ret.program.body[0]).matchSnapshot(); + expect(ret.program.sourceType).toMatchInlineSnapshot(`"module"`); }); }); From a702d5a730edf610ab1d3257162d297f9edc8cb3 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 6 Feb 2025 17:24:16 +0900 Subject: [PATCH 2/2] fix: fix ts_type --- crates/oxc_ast/src/ast/js.rs | 2 +- npm/oxc-types/types.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 26ac2e67821f3..57ddc2d8594d2 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -31,7 +31,7 @@ use super::{macros::inherit_variants, *}; #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)] pub struct Program<'a> { pub span: Span, - #[estree(via = crate::serialize::ESTreeSourceType, ts_type = "SourceType")] + #[estree(via = crate::serialize::ESTreeSourceType, ts_type = "ModuleKind")] pub source_type: SourceType, #[estree(skip)] pub source_text: &'a str, diff --git a/npm/oxc-types/types.d.ts b/npm/oxc-types/types.d.ts index ed79ac1a8fd01..13b7aa001417a 100644 --- a/npm/oxc-types/types.d.ts +++ b/npm/oxc-types/types.d.ts @@ -3,7 +3,7 @@ export interface Program extends Span { type: 'Program'; - sourceType: SourceType; + sourceType: ModuleKind; hashbang: Hashbang | null; directives: Array; body: Array;