From be94c69f116ede6bec01373b1a47277256fd2db9 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 29 Nov 2023 11:38:28 -0600 Subject: [PATCH 1/3] make optional props accept T or Option --- examples/optional_props.rs | 6 +++++ packages/core-macro/src/props/mod.rs | 33 ++++++++-------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/examples/optional_props.rs b/examples/optional_props.rs index c5652a2e7..d5cf1e711 100644 --- a/examples/optional_props.rs +++ b/examples/optional_props.rs @@ -12,6 +12,12 @@ fn main() { fn app(cx: Scope) -> Element { cx.render(rsx! { + Button { + a: "asd".to_string(), + c: "asd".to_string(), + d: Some("asd".to_string()), + e: Some("asd".to_string()), + } Button { a: "asd".to_string(), c: "asd".to_string(), diff --git a/packages/core-macro/src/props/mod.rs b/packages/core-macro/src/props/mod.rs index 6e6c43d66..f8450e36a 100644 --- a/packages/core-macro/src/props/mod.rs +++ b/packages/core-macro/src/props/mod.rs @@ -783,31 +783,16 @@ Finally, call `.build()` to create the instance of `{name}`. None => quote!(), }; - // NOTE: both auto_into and strip_option affect `arg_type` and `arg_expr`, but the order of - // nesting is different so we have to do this little dance. - let arg_type = if field.builder_attr.strip_option { - field.type_from_inside_option(false).ok_or_else(|| { - Error::new_spanned( - field_type, - "can't `strip_option` - field is not `Option<...>`", + let arg_type = field_type; + let (arg_type, arg_expr) = + if field.builder_attr.auto_into || field.builder_attr.strip_option { + ( + quote!(impl ::core::convert::Into<#arg_type>), + quote!(#field_name.into()), ) - })? - } else { - field_type - }; - let (arg_type, arg_expr) = if field.builder_attr.auto_into { - ( - quote!(impl ::core::convert::Into<#arg_type>), - quote!(#field_name.into()), - ) - } else { - (quote!(#arg_type), quote!(#field_name)) - }; - let arg_expr = if field.builder_attr.strip_option { - quote!(Some(#arg_expr)) - } else { - arg_expr - }; + } else { + (quote!(#arg_type), quote!(#field_name)) + }; let repeated_fields_error_type_name = syn::Ident::new( &format!( From 533c7bab492d121b79b126458429fc5774c3a39e Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 29 Nov 2023 11:39:38 -0600 Subject: [PATCH 2/3] show more cases in the optional props example --- examples/optional_props.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/optional_props.rs b/examples/optional_props.rs index d5cf1e711..8108d437d 100644 --- a/examples/optional_props.rs +++ b/examples/optional_props.rs @@ -20,10 +20,16 @@ fn app(cx: Scope) -> Element { } Button { a: "asd".to_string(), + b: "asd".to_string(), c: "asd".to_string(), d: Some("asd".to_string()), e: "asd".to_string(), } + Button { + a: "asd".to_string(), + c: "asd".to_string(), + d: Some("asd".to_string()), + } }) } From a840e012d81d6b2de5b3ec7fcd91a65552c40a18 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 29 Nov 2023 11:46:18 -0600 Subject: [PATCH 3/3] fix clippy --- packages/core-macro/src/props/mod.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/core-macro/src/props/mod.rs b/packages/core-macro/src/props/mod.rs index f8450e36a..d7a580705 100644 --- a/packages/core-macro/src/props/mod.rs +++ b/packages/core-macro/src/props/mod.rs @@ -243,10 +243,6 @@ mod field_info { } .into() } - - pub fn type_from_inside_option(&self, check_option_name: bool) -> Option<&syn::Type> { - type_from_inside_option(self.ty, check_option_name) - } } #[derive(Debug, Default, Clone)]