Skip to content
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
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "ModuleKind")]
pub source_type: SourceType,
#[estree(skip)]
pub source_text: &'a str,
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
16 changes: 15 additions & 1 deletion crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.module_kind().serialize(serializer)
}
}
1 change: 1 addition & 0 deletions napi/parser/test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`);
});
});

Expand Down
2 changes: 1 addition & 1 deletion npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

export interface Program extends Span {
type: 'Program';
sourceType: SourceType;
sourceType: ModuleKind;
hashbang: Hashbang | null;
directives: Array<Directive>;
body: Array<Statement>;
Expand Down