Skip to content

Commit

Permalink
fix(ast/estree): fix serializing import and export attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Feb 11, 2025
1 parent 2b47299 commit b63df27
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 160 deletions.
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,7 @@ pub struct ImportDeclaration<'a> {
pub phase: Option<ImportPhase>,
/// Some(vec![]) for empty assertion
#[ts]
#[estree(rename = "attributes", via = crate::serialize::ImportExportWithClause(&self.with_clause), ts_type = "Array<ImportAttribute>")]
pub with_clause: Option<Box<'a, WithClause<'a>>>,
/// `import type { foo } from 'bar'`
#[ts]
Expand Down Expand Up @@ -2402,6 +2403,7 @@ pub struct ExportNamedDeclaration<'a> {
pub export_kind: ImportOrExportKind,
/// Some(vec![]) for empty assertion
#[ts]
#[estree(rename = "attributes", via = crate::serialize::ImportExportWithClause(&self.with_clause), ts_type = "Array<ImportAttribute>")]
pub with_clause: Option<Box<'a, WithClause<'a>>>,
}

Expand Down
10 changes: 8 additions & 2 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,10 @@ impl Serialize for ImportDeclaration<'_> {
map.serialize_entry("specifiers", &crate::serialize::OptionVecDefault(&self.specifiers))?;
map.serialize_entry("source", &self.source)?;
map.serialize_entry("phase", &self.phase)?;
map.serialize_entry("withClause", &self.with_clause)?;
map.serialize_entry(
"attributes",
&crate::serialize::ImportExportWithClause(&self.with_clause),
)?;
map.serialize_entry("importKind", &self.import_kind)?;
map.end()
}
Expand Down Expand Up @@ -1799,7 +1802,10 @@ impl Serialize for ExportNamedDeclaration<'_> {
map.serialize_entry("specifiers", &self.specifiers)?;
map.serialize_entry("source", &self.source)?;
map.serialize_entry("exportKind", &self.export_kind)?;
map.serialize_entry("withClause", &self.with_clause)?;
map.serialize_entry(
"attributes",
&crate::serialize::ImportExportWithClause(&self.with_clause),
)?;
map.end()
}
}
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ pub fn import_expression_options<'a>(
arguments.first()
}

/// Serializer for `ImportDeclaration` and `ExportNamedDeclaration`'s `with_clause` field
/// (which is renamed to `attributes` in ESTree AST).
// https://github.com/estree/estree/blob/master/es2025.md#importdeclaration
// https://github.com/estree/estree/blob/master/es2025.md#exportnameddeclaration
pub struct ImportExportWithClause<'a>(pub &'a Option<ArenaBox<'a, WithClause<'a>>>);

impl Serialize for ImportExportWithClause<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if let Some(with_clause) = &self.0 {
with_clause.with_entries.serialize(serializer)
} else {
[(); 0].serialize(serializer)
}
}
}

// --------------------
// JSX
// --------------------
Expand Down
4 changes: 2 additions & 2 deletions npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ export interface ImportDeclaration extends Span {
specifiers: Array<ImportDeclarationSpecifier>;
source: StringLiteral;
phase: ImportPhase | null;
withClause: WithClause | null;
attributes: Array<ImportAttribute>;
importKind: ImportOrExportKind;
}

Expand Down Expand Up @@ -755,7 +755,7 @@ export interface ExportNamedDeclaration extends Span {
specifiers: Array<ExportSpecifier>;
source: StringLiteral | null;
exportKind: ImportOrExportKind;
withClause: WithClause | null;
attributes: Array<ImportAttribute>;
}

export interface ExportDefaultDeclaration extends Span {
Expand Down
Loading

0 comments on commit b63df27

Please sign in to comment.