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
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::{macros::inherit_variants, *};
)]
#[derive(Debug)]
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(field_order(span, directives, source_type, hashbang))]
#[estree(field_order(span, directives, source_type, hashbang), via = ProgramConverter)]
pub struct Program<'a> {
pub span: Span,
pub source_type: SourceType,
Expand Down
12 changes: 1 addition & 11 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ use crate::ast::ts::*;

impl ESTree for Program<'_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut state = serializer.serialize_struct();
state.serialize_field("type", &JsonSafeString("Program"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field(
"body",
&AppendToConcat { array: &self.directives, after: &self.body },
);
self.source_type.serialize(FlatStructSerializer(&mut state));
state.serialize_field("hashbang", &self.hashbang);
state.end();
crate::serialize::ProgramConverter(self).serialize(serializer)
}
}

Expand Down
82 changes: 62 additions & 20 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oxc_ast_macros::ast_meta;
use oxc_estree::{
CompactJSSerializer, CompactTSSerializer, ESTree, JsonSafeString, LoneSurrogatesString,
PrettyJSSerializer, PrettyTSSerializer, SequenceSerializer, Serializer, StructSerializer,
ser::AppendToConcat,
};
use oxc_span::GetSpan;

Expand Down Expand Up @@ -37,16 +38,7 @@ const JSON_CAPACITY_RATIO_PRETTY: usize = 80;

impl Program<'_> {
/// Serialize AST to ESTree JSON, including TypeScript fields.
pub fn to_estree_ts_json(&mut self) -> String {
// Set start span to first token of first directive or statement. This is required because
// unlike Acorn, TS-ESLint excludes whitespace and comments from the `Program` start span.
// See https://github.com/oxc-project/oxc/pull/10134 for more info.
if let Some(first_directive) = self.directives.first() {
self.span.start = first_directive.span.start;
} else if let Some(first_stmt) = self.body.first() {
self.span.start = first_stmt.span().start;
}

pub fn to_estree_ts_json(&self) -> String {
let capacity = self.source_text.len() * JSON_CAPACITY_RATIO_COMPACT;
let mut serializer = CompactTSSerializer::with_capacity(capacity);
self.serialize(&mut serializer);
Expand All @@ -62,16 +54,7 @@ impl Program<'_> {
}

/// Serialize AST to pretty-printed ESTree JSON, including TypeScript fields.
pub fn to_pretty_estree_ts_json(&mut self) -> String {
// Set start span to first token of first directive or statement. This is required because
// unlike Acorn, TS-ESLint excludes whitespace and comments from the `Program` start span.
// See https://github.com/oxc-project/oxc/pull/10134 for more info.
if let Some(first_directive) = self.directives.first() {
self.span.start = first_directive.span.start;
} else if let Some(first_stmt) = self.body.first() {
self.span.start = first_stmt.span().start;
}

pub fn to_pretty_estree_ts_json(&self) -> String {
let capacity = self.source_text.len() * JSON_CAPACITY_RATIO_PRETTY;
let mut serializer = PrettyTSSerializer::with_capacity(capacity);
self.serialize(&mut serializer);
Expand All @@ -87,6 +70,65 @@ impl Program<'_> {
}
}

// --------------------
// Program
// --------------------

/// Serializer for `Program`.
///
/// In TS AST, set start span to start of first directive or statement.
/// This is required because unlike Acorn, TS-ESLint excludes whitespace and comments
/// from the `Program` start span.
/// See <https://github.com/oxc-project/oxc/pull/10134> for more info.
#[ast_meta]
#[estree(raw_deser = "
const body = DESER[Vec<Directive>](POS_OFFSET.directives);
body.push(...DESER[Vec<Statement>](POS_OFFSET.body));
let start = DESER[u32](POS_OFFSET.span.start);
/* IF_TS */
if (body.length > 0) start = body[0].start;
/* END_IF_TS */
const program = {
type: 'Program',
start,
end: DESER[u32](POS_OFFSET.span.end),
body,
sourceType: DESER[ModuleKind](POS_OFFSET.source_type.module_kind),
hashbang: DESER[Option<Hashbang>](POS_OFFSET.hashbang),
};
program
")]
pub struct ProgramConverter<'a, 'b>(pub &'b Program<'a>);

impl ESTree for ProgramConverter<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let program = self.0;
let span_start = if S::INCLUDE_TS_FIELDS {
if let Some(first_directive) = program.directives.first() {
first_directive.span.start
} else if let Some(first_stmt) = program.body.first() {
first_stmt.span().start
} else {
program.span.start
}
} else {
program.span.start
};

let mut state = serializer.serialize_struct();
state.serialize_field("type", &JsonSafeString("Program"));
state.serialize_field("start", &span_start);
state.serialize_field("end", &program.span.end);
state.serialize_field(
"body",
&AppendToConcat { array: &program.directives, after: &program.body },
);
state.serialize_field("sourceType", &program.source_type.module_kind());
state.serialize_field("hashbang", &program.hashbang);
state.end();
}
}

// --------------------
// Basic types
// --------------------
Expand Down
6 changes: 4 additions & 2 deletions napi/parser/deserialize-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ function deserialize(buffer, sourceTextInput, sourceLenInput) {
function deserializeProgram(pos) {
const body = deserializeVecDirective(pos + 88);
body.push(...deserializeVecStatement(pos + 120));
return {
let start = deserializeU32(pos);
const program = {
type: 'Program',
start: deserializeU32(pos),
start,
end: deserializeU32(pos + 4),
body,
sourceType: deserializeModuleKind(pos + 9),
hashbang: deserializeOptionHashbang(pos + 64),
};
return program;
}

function deserializeIdentifierName(pos) {
Expand Down
7 changes: 5 additions & 2 deletions napi/parser/deserialize-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ function deserialize(buffer, sourceTextInput, sourceLenInput) {
function deserializeProgram(pos) {
const body = deserializeVecDirective(pos + 88);
body.push(...deserializeVecStatement(pos + 120));
return {
let start = deserializeU32(pos);
if (body.length > 0) start = body[0].start;
const program = {
type: 'Program',
start: deserializeU32(pos),
start,
end: deserializeU32(pos + 4),
body,
sourceType: deserializeModuleKind(pos + 9),
hashbang: deserializeOptionHashbang(pos + 64),
};
return program;
}

function deserializeIdentifierName(pos) {
Expand Down
Loading