Skip to content

Commit

Permalink
fix(ast): estree compat Program.sourceType (#8919)
Browse files Browse the repository at this point in the history
part of #2854
ref: https://github.com/estree/estree/blob/master/es2015.md#programs

I tried to move `via` customization on `struct SourceType`, but couldn't
find a way maybe because struct is defined in `oxc_span`. For now, I
made this as `via` on `Program::source_type` field.
  • Loading branch information
hi-ogawa authored Feb 6, 2025
1 parent e30cf6a commit a520986
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
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"`);
});

it('estree MemberExpression', async () => {
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

0 comments on commit a520986

Please sign in to comment.