refactor(ast): align #[estree(via)] behavior (#8599)

Follow-on after #8564. Aligned the behavior of `#[estree(via)]` for fields with `#[estree(via)]` for structs.
This commit is contained in:
sapphi-red 2025-01-19 20:44:32 +00:00
parent 4ff6e85137
commit 997859c42e
No known key found for this signature in database
GPG key ID: 67631A259A77AC6C
3 changed files with 5 additions and 6 deletions

View file

@ -2196,7 +2196,7 @@ pub struct ImportExpression<'a> {
pub struct ImportDeclaration<'a> {
pub span: Span,
/// `None` for `import 'foo'`, `Some([])` for `import {} from 'foo'`
#[estree(via = "OptionVecDefault", type = "Array<ImportDeclarationSpecifier>")]
#[estree(via = crate::serialize::OptionVecDefault, type = "Array<ImportDeclarationSpecifier>")]
pub specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
pub source: StringLiteral<'a>,
pub phase: Option<ImportPhase>,

View file

@ -9,7 +9,6 @@ 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};
@ -142,12 +141,12 @@ fn serialize_struct(def: &StructDef, schema: &Schema) -> TokenStream {
}
)?;
});
} else if let Some(with) = &field.markers.derive_attributes.estree.via {
let with_ident = with.to_ident();
} else if let Some(via) = &field.markers.derive_attributes.estree.via {
let via_tokens: TokenStream = via.parse().unwrap();
fields.push(quote! {
map.serialize_entry(
#name,
&crate::serialize::#with_ident(&self.#ident)
&#via_tokens(&self.#ident)
)?;
});
} else {

View file

@ -286,7 +286,7 @@ impl Parse for ESTreeFieldAttribute {
"via" => {
input.parse::<Token![=]>()?;
assert!(
via.replace(input.parse::<LitStr>()?.value()).is_none(),
via.replace(input.parse::<Path>()?.to_token_stream().to_string()).is_none(),
"Duplicate estree(with)"
);
}