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(macro): handle patterns in inline_props component arguments #720

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 25 additions & 5 deletions packages/sycamore-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,31 @@
let props_struct_ident = format_ident!("{}_Props", item.sig.ident);

let inputs = item.sig.inputs.clone();
let props = inputs.into_iter().collect::<Vec<_>>();
let props = inputs
.into_iter()
.map(|arg| match arg {
FnArg::Typed(pat_type) => {
// Remove any mut or other pattern modifiers
let pat = match *pat_type.pat {
Pat::Ident(mut ident_pat) => {
ident_pat.mutability = None;
Pat::Ident(ident_pat)
}
// Handle other patterns if necessary
_ => panic!("Unsupported pattern type in inline_props"),

Check warning on line 275 in packages/sycamore-macro/src/component.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-macro/src/component.rs#L275

Added line #L275 was not covered by tests
};
syn::PatType {
pat: Box::new(pat),
..pat_type
}
}
FnArg::Receiver(_) => panic!("Components cannot have receivers"),

Check warning on line 282 in packages/sycamore-macro/src/component.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-macro/src/component.rs#L282

Added line #L282 was not covered by tests
})
.collect::<Vec<_>>();

let generics = &item.sig.generics;
let generics_phantoms = generics.params.iter().enumerate().filter_map(|(i, param)| {
let phantom_ident = format_ident!("__phantom{i}");
let phantom_ident = format_ident!("__phantom{}", i);
Comment on lines -268 to +288
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you changed this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you changed this

i think that is the wrong way of formatting. format_ident!("__phantom{i}") treats {i} as a literal, so the identifier becomes __phantom{i} exactly, without replacing i with its value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the {i} works because it generates the right code. Also format_ident! uses std::format! under the hood and the new format!() syntax is supported since Rust 1.58.

Copy link
Author

@KekmaTime KekmaTime Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the {i} works because it generates the right code. Also format_ident! uses std::format! under the hood and the new format!() syntax is supported since Rust 1.58.

im sorry i didnt know that 😓 ill fix it right away

match param {
syn::GenericParam::Type(ty) => {
let ty = &ty.ident;
Expand Down Expand Up @@ -300,9 +320,9 @@
// Rewrite component body.

// Get the ident (technically, patterns) of each prop.
let props_pats = props.iter().map(|arg| match arg {
FnArg::Receiver(_) => unreachable!("receiver cannot be a prop"),
FnArg::Typed(arg) => arg.pat.clone(),
let props_pats = props.iter().map(|arg| match &*arg.pat {
Pat::Ident(pat_ident) => pat_ident.ident.clone(),
_ => panic!("Unsupported pattern type in inline_props"),

Check warning on line 325 in packages/sycamore-macro/src/component.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-macro/src/component.rs#L325

Added line #L325 was not covered by tests
});
// Rewrite function signature.
let props_struct_generics = generics.split_for_impl().1;
Expand Down
Loading