Skip to content

Commit

Permalink
feat: Allow #[prop(default)] without assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rowashere committed Sep 23, 2023
1 parent d99269a commit 7b06ae8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
36 changes: 32 additions & 4 deletions leptos_macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,34 @@ impl Docs {
}
}

#[derive(Clone, Debug)]
struct DefaultExpr(Option<syn::Expr>);

impl DefaultExpr {
fn is_some(&self) -> bool {
self.0.is_some()
}
}

impl attribute_derive::ConvertParsed for DefaultExpr {
type Type = syn::Expr;
fn convert(value: Self::Type) -> syn::Result<Self> {
Ok(Self(Some(value)))
}

fn default_by_default() -> bool {
true
}

fn default() -> Self {
Self(None)
}

fn as_flag() -> Option<Self::Type> {
Some(parse_quote!(Default::default()))
}
}

#[derive(Clone, Debug, AttributeDerive)]
#[attribute(ident = prop)]
struct PropOpt {
Expand All @@ -742,14 +770,14 @@ struct PropOpt {
#[attribute(conflicts = [optional, optional_no_strip])]
strip_option: bool,
#[attribute(example = "5 * 10")]
default: Option<syn::Expr>,
default: DefaultExpr,
into: bool,
attrs: bool,
}

struct TypedBuilderOpts {
default: bool,
default_with_value: Option<syn::Expr>,
default_with_value: DefaultExpr,
strip_option: bool,
into: bool,
}
Expand All @@ -767,7 +795,7 @@ impl TypedBuilderOpts {

impl TypedBuilderOpts {
fn to_serde_tokens(&self) -> TokenStream {
let default = if let Some(v) = &self.default_with_value {
let default = if let Some(v) = &self.default_with_value.0 {
let v = v.to_token_stream().to_string();
quote! { default=#v, }
} else if self.default {
Expand All @@ -786,7 +814,7 @@ impl TypedBuilderOpts {

impl ToTokens for TypedBuilderOpts {
fn to_tokens(&self, tokens: &mut TokenStream) {
let default = if let Some(v) = &self.default_with_value {
let default = if let Some(v) = &self.default_with_value.0 {
let v = v.to_token_stream().to_string();
quote! { default_code=#v, }
} else if self.default {
Expand Down
8 changes: 8 additions & 0 deletions leptos_macro/tests/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ fn Component(
_ = into;
}

#[component]
fn ImplicitDefault(#[prop(default)] default: String) -> impl IntoView {
let _ = default;
}

#[test]
fn component() {
let cp = ComponentProps::builder().into("").strip_option(9).build();
Expand All @@ -24,4 +29,7 @@ fn component() {
assert_eq!(cp.strip_option, Some(9));
assert_eq!(cp.default, NonZeroUsize::new(10).unwrap());
assert_eq!(cp.into, "");

let cp = ImplicitDefaultProps::builder().build();
assert_eq!(cp.default, String::default());
}
8 changes: 0 additions & 8 deletions leptos_macro/tests/ui/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ fn optional_no_strip_and_strip_option(
_ = conflicting;
}

#[component]
fn default_without_value(
,
#[prop(default)] default: bool,
) -> impl IntoView {
_ = default;
}

#[component]
fn default_with_invalid_value(
,
Expand Down
7 changes: 0 additions & 7 deletions leptos_macro/tests/ui/component_absolute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ fn optional_no_strip_and_strip_option(
_ = conflicting;
}

#[::leptos::component]
fn default_without_value(
#[prop(default)] default: bool,
) -> impl ::leptos::IntoView {
_ = default;
}

#[::leptos::component]
fn default_with_invalid_value(
#[prop(default= |)] default: bool,
Expand Down

0 comments on commit 7b06ae8

Please sign in to comment.