Skip to content

Commit

Permalink
Forward attributes on function parameters to generated prop struct in…
Browse files Browse the repository at this point in the history
… `inline_props` (#753)

* Forward attributes on function parameters to generated prop struct

* Fix error and add test

* cargo fmt
  • Loading branch information
lukechu10 authored Nov 3, 2024
1 parent 47ca5f1 commit ebb99b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
27 changes: 12 additions & 15 deletions packages/sycamore-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,22 @@ fn inline_props_impl(item: &mut ItemFn, attrs: Punctuated<Meta, Token![,]>) -> R
let props = inputs.clone().into_iter().collect::<Vec<_>>();
let generics: &mut Generics = &mut item.sig.generics;
let mut fields = Vec::new();
inputs.iter().for_each(|arg| match arg {
inputs.into_iter().for_each(|arg| match arg {
FnArg::Receiver(_) => {
unreachable!("receiver cannot be a prop")
}
FnArg::Typed(pat_type) => {
let pat = &*pat_type.pat;
let ty = &*pat_type.ty;
match pat {
Pat::Ident(ident_pat) => super::inline_props::push_field(
&mut fields,
generics,
ident_pat.clone().ident,
ty.clone(),
),
_ => {
unreachable!("unexpected pattern!")
}
FnArg::Typed(pat_type) => match *pat_type.pat {
Pat::Ident(ident_pat) => super::inline_props::push_field(
&mut fields,
generics,
pat_type.attrs,
ident_pat.clone().ident,
*pat_type.ty,
),
_ => {
unreachable!("unexpected pattern!")
}
}
},
});

let generics_phantoms = generics.params.iter().enumerate().filter_map(|(i, param)| {
Expand Down
12 changes: 9 additions & 3 deletions packages/sycamore-macro/src/inline_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::Span;
use quote::format_ident;
use syn::punctuated::Punctuated;
use syn::{
Field, GenericParam, Generics, Ident, Path, PathArguments, PathSegment, Token, Type,
Attribute, Field, GenericParam, Generics, Ident, Path, PathArguments, PathSegment, Token, Type,
TypeImplTrait, TypeParam, TypePath, Visibility,
};

Expand Down Expand Up @@ -85,11 +85,17 @@ pub fn add_generic(generics: &mut Generics, impl_type: TypeImplTrait) -> Type {
})
}

pub fn push_field(fields: &mut Vec<Field>, generics: &mut Generics, ident: Ident, ty: Type) {
pub fn push_field(
fields: &mut Vec<Field>,
generics: &mut Generics,
attrs: Vec<Attribute>,
ident: Ident,
ty: Type,
) {
let ty = resolve_type(generics, ty);

fields.push(Field {
attrs: Vec::new(),
attrs,
vis: Visibility::Public(Token![pub](Span::call_site())),
mutability: syn::FieldMutability::None,
ident: Some(ident),
Expand Down
15 changes: 15 additions & 0 deletions packages/sycamore-macro/tests/component/inline-props-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@ fn AdditionalStructAttributes(dummy: String) -> View {
}
}

#[component(inline_props)]
fn PropsWithAttributes(
#[prop(default)]
dummy: String,
) -> View {
fn call_component() -> View {
view! {
PropsWithAttributes {}
}
}
view! {
(dummy)
}
}

fn main() {}

0 comments on commit ebb99b3

Please sign in to comment.