Use into_prop_value to convert str prop to Option<String> (#2080)

This commit is contained in:
Xavientois 2021-09-24 03:00:39 -04:00 committed by GitHub
parent a442ce5df5
commit 97498cdfcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 20 deletions

View File

@ -6,7 +6,7 @@ use syn::{
parse::{Parse, ParseStream},
spanned::Spanned,
token::Dot2,
Expr, ExprLit, Lit,
Expr,
};
struct BaseExpr {
@ -110,15 +110,8 @@ impl ComponentProps {
Some(expr) => {
let ident = Ident::new("__yew_props", props_ty.span());
let set_props = self.props.iter().map(|Prop { label, value, .. }| {
if is_string_literal(value) {
// String literals should be implicitly converted into `String`
quote_spanned! {value.span()=>
#ident.#label = ::std::convert::Into::into(#value);
}
} else {
quote_spanned! {value.span()=>
#ident.#label = #value;
}
quote_spanned! {value.span()=>
#ident.#label = ::yew::html::IntoPropValue::into_prop_value(#value);
}
});
let set_children = children_renderer.map(|children| {
@ -145,16 +138,6 @@ impl ComponentProps {
}
}
fn is_string_literal(expr: &Expr) -> bool {
matches!(
expr,
Expr::Lit(ExprLit {
lit: Lit::Str(_),
..
})
)
}
impl Parse for ComponentProps {
fn parse(input: ParseStream) -> syn::Result<Self> {
let props = validate(input.parse()?)?;

View File

@ -246,6 +246,15 @@ fn compile_pass() {
<div>{ "hello world" }</div>
</Container>
<Container int=1 ..::std::clone::Clone::clone(&props)>
<Child int=2 opt_str="hello" ..::std::clone::Clone::clone(&child_props) />
</Container>
<Container int=1 ..::std::clone::Clone::clone(&props)>
<Child int=2 vec={::std::vec![0]} ..::std::clone::Clone::clone(&child_props) />
</Container>
<Container int=1 ..props>
<Child int=2 string="hello" ..child_props />
</Container>