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)!: always return Array<ImportDeclarationSpecifier> for ImportDeclaration.specifiers #8560

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
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 @@ -2196,6 +2196,7 @@ pub struct ImportExpression<'a> {
pub struct ImportDeclaration<'a> {
pub span: Span,
/// `None` for `import 'foo'`, `Some([])` for `import {} from 'foo'`
#[estree(with = "OptionVecDefault", type = "Array<ImportDeclarationSpecifier>")]
pub specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
pub source: StringLiteral<'a>,
pub phase: Option<ImportPhase>,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ impl Serialize for ImportDeclaration<'_> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "ImportDeclaration")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("specifiers", &self.specifiers)?;
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)?;
Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ impl<E: Serialize, R: Serialize> Serialize for ElementsAndRest<'_, E, R> {
}
}

pub struct OptionVecDefault<'a, 'b, T: Serialize>(pub &'a Option<oxc_allocator::Vec<'b, T>>);

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

/// Serialize `TSModuleBlock` to be ESTree compatible, with `body` and `directives` fields combined,
/// and directives output as `StringLiteral` expression statements
impl Serialize for TSModuleBlock<'_> {
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 @@ -911,7 +911,7 @@ export interface ImportExpression extends Span {

export interface ImportDeclaration extends Span {
type: 'ImportDeclaration';
specifiers: Array<ImportDeclarationSpecifier> | null;
specifiers: Array<ImportDeclarationSpecifier>;
source: StringLiteral;
phase: ImportPhase | null;
withClause: WithClause | null;
Expand Down
9 changes: 9 additions & 0 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
serialize::{enum_variant_name, get_always_flatten_structs, get_type_tag},
EnumDef, FieldDef, GetGenerics, GetIdent, Schema, StructDef, TypeDef,
},
util::ToIdent,
};

use super::{define_derive, Derive};
Expand Down Expand Up @@ -141,6 +142,14 @@ fn serialize_struct(def: &StructDef, schema: &Schema) -> TokenStream {
}
)?;
});
} else if let Some(with) = &field.markers.derive_attributes.estree.with {
let with_ident = with.to_ident();
fields.push(quote! {
map.serialize_entry(
#name,
&crate::serialize::#with_ident(&self.#ident)
)?;
});
} else {
fields.push(quote! {
map.serialize_entry(#name, &self.#ident)?;
Expand Down
11 changes: 10 additions & 1 deletion tasks/ast_tools/src/markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub struct ESTreeFieldAttribute {
pub rename: Option<String>,
pub typescript_type: Option<String>,
pub append_to: Option<String>,
pub with: Option<String>,
}

impl Parse for ESTreeFieldAttribute {
Expand All @@ -236,6 +237,7 @@ impl Parse for ESTreeFieldAttribute {
let mut rename = None;
let mut typescript_type = None;
let mut append_to = None;
let mut with = None;

loop {
let is_type = input.peek(Token![type]);
Expand Down Expand Up @@ -281,6 +283,13 @@ impl Parse for ESTreeFieldAttribute {
"Duplicate estree(append_to)"
);
}
"with" => {
input.parse::<Token![=]>()?;
assert!(
with.replace(input.parse::<LitStr>()?.value()).is_none(),
"Duplicate estree(with)"
);
}
arg => panic!("Unsupported #[estree(...)] argument: {arg}"),
}
let comma = input.peek(Token![,]);
Expand All @@ -290,7 +299,7 @@ impl Parse for ESTreeFieldAttribute {
break;
}
}
Ok(Self { flatten, skip, rename, typescript_type, append_to })
Ok(Self { flatten, skip, rename, typescript_type, append_to, with })
}
}

Expand Down
Loading