Skip to content

Commit

Permalink
fix(ast/estree): use #[estree(append_to)] for TSModuleBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Feb 10, 2025
1 parent 3cd4374 commit f31ab46
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 52 deletions.
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

0 comments on commit f31ab46

Please sign in to comment.