Skip to content

Commit

Permalink
refactor(ast): estree via on struct fields implement From
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Feb 4, 2025
1 parent 5046b09 commit 74d5176
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,10 @@ 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", &crate::serialize::OptionVecDefault(&self.specifiers))?;
map.serialize_entry(
"specifiers",
&crate::serialize::OptionVecDefault::from(&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: 9 additions & 3 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{
Serialize,
};

use oxc_allocator::Box;
use oxc_allocator::{Box as ArenaBox, Vec as ArenaVec};
use oxc_span::{Atom, Span};
use oxc_syntax::number::BigintBase;

Expand Down Expand Up @@ -211,7 +211,7 @@ struct SerFormalParameterRest<'a, 'b> {
#[serde(flatten)]
span: Span,
argument: &'b BindingPatternKind<'a>,
type_annotation: &'b Option<Box<'a, TSTypeAnnotation<'a>>>,
type_annotation: &'b Option<ArenaBox<'a, TSTypeAnnotation<'a>>>,
optional: bool,
}

Expand Down Expand Up @@ -241,7 +241,13 @@ 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>>);
pub struct OptionVecDefault<'a, 'b, T: Serialize>(pub &'b Option<ArenaVec<'a, T>>);

impl<'a, 'b, T: Serialize> From<&'b Option<ArenaVec<'a, T>>> for OptionVecDefault<'a, 'b, T> {
fn from(opt_vec: &'b Option<ArenaVec<'a, T>>) -> Self {
Self(opt_vec)
}
}

impl<T: Serialize> Serialize for OptionVecDefault<'_, '_, T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn generate_stmt_for_struct_field(
let mut value = quote!( &self.#field_name_ident );
if let Some(via_str) = field.estree.via.as_deref() {
let via_ty = parse_str::<Type>(via_str).unwrap();
value = quote!( &#via_ty(#value) );
value = quote!( &#via_ty::from(#value) );
} else if let Some(append_field_index) = field.estree.append_field_index {
let append_from_ident = struct_def.fields[append_field_index].ident();
value = quote! {
Expand Down

0 comments on commit 74d5176

Please sign in to comment.