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

#[prop(default)] uses Default::default(), unless specified. #1776

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
36 changes: 32 additions & 4 deletions leptos_macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,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 @@ -805,14 +833,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 @@ -830,7 +858,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 @@ -849,7 +877,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