fix: #[component] macro attributes propagation (#2835)

This commit is contained in:
Álvaro Mondéjar Rubio 2024-08-17 02:21:46 +02:00 committed by GitHub
parent 621f112f4c
commit d9c8d4ff66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,6 +19,7 @@ use syn::{
pub struct Model { pub struct Model {
is_island: bool, is_island: bool,
docs: Docs, docs: Docs,
unknown_attrs: UnknownAttrs,
vis: Visibility, vis: Visibility,
name: Ident, name: Ident,
props: Vec<Prop>, props: Vec<Prop>,
@ -32,6 +33,7 @@ impl Parse for Model {
convert_impl_trait_to_generic(&mut item.sig); convert_impl_trait_to_generic(&mut item.sig);
let docs = Docs::new(&item.attrs); let docs = Docs::new(&item.attrs);
let unknown_attrs = UnknownAttrs::new(&item.attrs);
let props = item let props = item
.sig .sig
@ -61,6 +63,7 @@ impl Parse for Model {
Ok(Self { Ok(Self {
is_island: false, is_island: false,
docs, docs,
unknown_attrs,
vis: item.vis.clone(), vis: item.vis.clone(),
name: convert_from_snake_case(&item.sig.ident), name: convert_from_snake_case(&item.sig.ident),
props, props,
@ -100,6 +103,7 @@ impl ToTokens for Model {
let Self { let Self {
is_island, is_island,
docs, docs,
unknown_attrs,
vis, vis,
name, name,
props, props,
@ -457,6 +461,7 @@ impl ToTokens for Model {
} }
} */ } */
#unknown_attrs
#docs_and_prop_docs #docs_and_prop_docs
#[allow(non_snake_case, clippy::too_many_arguments)] #[allow(non_snake_case, clippy::too_many_arguments)]
#[allow(clippy::needless_lifetimes)] #[allow(clippy::needless_lifetimes)]
@ -496,7 +501,10 @@ pub struct DummyModel {
impl Parse for DummyModel { impl Parse for DummyModel {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let attrs = input.call(Attribute::parse_outer)?; let mut attrs = input.call(Attribute::parse_outer)?;
// Drop unknown attributes like #[deprecated]
drain_filter(&mut attrs, |attr| !attr.path().is_ident("doc"));
let vis: Visibility = input.parse()?; let vis: Visibility = input.parse()?;
let sig: Signature = input.parse()?; let sig: Signature = input.parse()?;
@ -745,6 +753,37 @@ impl Docs {
} }
} }
pub struct UnknownAttrs(Vec<(TokenStream, Span)>);
impl UnknownAttrs {
pub fn new(attrs: &[Attribute]) -> Self {
let attrs = attrs
.iter()
.filter_map(|attr| {
if attr.path().is_ident("doc") {
if let Meta::NameValue(_) = &attr.meta {
return None;
}
}
Some((attr.into_token_stream(), attr.span()))
})
.collect_vec();
Self(attrs)
}
}
impl ToTokens for UnknownAttrs {
fn to_tokens(&self, tokens: &mut TokenStream) {
let s = self
.0
.iter()
.map(|(attr, span)| quote_spanned!(*span=> #attr))
.collect::<TokenStream>();
tokens.append_all(s);
}
}
#[derive(Clone, Debug, FromAttr)] #[derive(Clone, Debug, FromAttr)]
#[attribute(ident = prop)] #[attribute(ident = prop)]
struct PropOpt { struct PropOpt {