Skip to content

Commit

Permalink
Support ident with subpatterns as props
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Nov 17, 2024
1 parent 41fef95 commit 9b85e17
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
14 changes: 12 additions & 2 deletions packages/sycamore-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,18 @@ fn inline_props_impl(item: &mut ItemFn, attrs: Punctuated<Meta, Token![,]>) -> R

// 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(),
FnArg::Receiver(_) => unreachable!(),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L353 was not covered by tests
FnArg::Typed(arg) => match &*arg.pat {
Pat::Ident(pat) => {
if pat.subpat.is_some() {
let ident = &pat.ident;
quote! { #ident: #pat }

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

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-macro/src/component.rs#L357-L358

Added lines #L357 - L358 were not covered by tests
} else {
quote! { #pat }
}
}
_ => unreachable!(),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L363 was not covered by tests
},
});
// Rewrite function signature.
let props_struct_generics = generics.split_for_impl().1;
Expand Down
11 changes: 11 additions & 0 deletions packages/sycamore-macro/tests/component/inline-props-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ fn ReceiverProp(self) -> View {
view! {}
}

struct Foo {
bar: i32,
}

#[component(inline_props)]
fn PatternWithoutIdent(Foo { bar }: Foo) -> View {
view! {
(bar)
}
}

fn main() {}
20 changes: 20 additions & 0 deletions packages/sycamore-macro/tests/component/inline-props-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,23 @@ error: expected `inline_props`
|
3 | #[component(not_inline_props)]
| ^^^^^^^^^^^^^^^^

error: receiver cannot be a prop
--> tests/component/inline-props-fail.rs:9:17
|
9 | fn ReceiverProp(self) -> View {
| ^^^^

error: unexpected pattern, you must specify an identifier for the prop
--> tests/component/inline-props-fail.rs:18:24
|
18 | fn PatternWithoutIdent(Foo { bar }: Foo) -> View {
| ^^^

error: `self` parameter is only allowed in associated functions
--> tests/component/inline-props-fail.rs:9:17
|
9 | fn ReceiverProp(self) -> View {
| ^^^^ not semantically valid as function parameter
|
= note: associated functions are those in `impl` or `trait` definitions
10 changes: 8 additions & 2 deletions packages/sycamore-macro/tests/component/inline-props-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,19 @@ fn PropsWithAttributes(#[prop(default)] dummy: String) -> View {
}
}

#[derive(Debug)]
struct Foo {
bar: u32,
}

#[component(inline_props)]
fn PropsWithPatterns(mut _a: u32, _b @ Foo { bar }: Foo) -> View {
view! {}
fn PropsWithPatterns(mut a: u32, b @ Foo { bar }: Foo) -> View {
let _ = &mut a;
view! {
(a)
(format!("{b:?}"))
(bar)
}
}

fn main() {}

0 comments on commit 9b85e17

Please sign in to comment.