Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ast/estree): use #[estree(append_to)] for TSModuleBlock #9020

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
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,11 +1205,11 @@ pub enum TSModuleDeclarationBody<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(custom_serialize)]
pub struct TSModuleBlock<'a> {
pub span: Span,
#[estree(skip)]
#[estree(rename = "body")]
pub directives: Vec<'a, Directive<'a>>,
#[estree(append_to = "directives")]
pub body: Vec<'a, Statement<'a>>,
}

Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3043,6 +3043,20 @@ impl Serialize for TSModuleDeclarationBody<'_> {
}
}

impl Serialize for TSModuleBlock<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "TSModuleBlock")?;
map.serialize_entry("start", &self.span.start)?;
map.serialize_entry("end", &self.span.end)?;
map.serialize_entry(
"body",
&AppendToConcat { array: &self.directives, after: &self.body },
)?;
map.end()
}
}

impl Serialize for TSTypeLiteral<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
Expand Down
49 changes: 0 additions & 49 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,55 +222,6 @@ impl<T: Serialize> Serialize for OptionVecDefault<'_, '_, T> {
}
}

/// Serialize `TSModuleBlock` to be ESTree compatible, with `body` and `directives` fields combined,
/// and directives output as `StringLiteral` expression statements
impl Serialize for TSModuleBlock<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let converted = SerTSModuleBlock {
span: self.span,
body: DirectivesAndStatements { directives: &self.directives, body: &self.body },
};
converted.serialize(serializer)
}
}

#[derive(Serialize)]
#[serde(tag = "type", rename = "TSModuleBlock")]
struct SerTSModuleBlock<'a, 'b> {
#[serde(flatten)]
span: Span,
body: DirectivesAndStatements<'a, 'b>,
}

struct DirectivesAndStatements<'a, 'b> {
directives: &'b [Directive<'a>],
body: &'b [Statement<'a>],
}

impl Serialize for DirectivesAndStatements<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut seq = serializer.serialize_seq(Some(self.directives.len() + self.body.len()))?;
for directive in self.directives {
seq.serialize_element(&DirectiveAsStatement {
span: directive.span,
expression: &directive.expression,
})?;
}
for stmt in self.body {
seq.serialize_element(stmt)?;
}
seq.end()
}
}

#[derive(Serialize)]
#[serde(tag = "type", rename = "ExpressionStatement")]
struct DirectiveAsStatement<'a, 'b> {
#[serde(flatten)]
span: Span,
expression: &'b StringLiteral<'a>,
}

/// Serializer for `ArrowFunctionExpression`'s `body` field.
///
/// Serializes as either an expression (if `expression` property is set),
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 @@ -1280,7 +1280,7 @@ export type TSModuleDeclarationBody = TSModuleDeclaration | TSModuleBlock;

export interface TSModuleBlock extends Span {
type: 'TSModuleBlock';
body: Array<Statement>;
body: Array<Directive | Statement>;
}

export interface TSTypeLiteral extends Span {
Expand Down
Loading