Skip to content

Commit

Permalink
improve fix macro messages
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldSEnder committed Mar 26, 2022
1 parent 108142e commit 8499e8f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/yew-macro/src/html_tree/html_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ impl ToTokens for HtmlComponent {
children,
} = self;

let props_ty = quote_spanned!(ty.span()=> <#ty as ::yew::html::IntoComponent>::Properties);
let props_ty =
quote_spanned! {ty.span()=> <#ty as ::yew::html::IntoComponent>::Properties };
let comp_ty = quote_spanned! {ty.span()=> <#ty as ::yew::html::IntoComponent>::Component };
let children_renderer = if children.is_empty() {
None
} else {
Expand All @@ -101,11 +103,13 @@ impl ToTokens for HtmlComponent {
let build_props = props.build_properties_tokens(&props_ty, children_renderer);

let special_props = props.special();
let comp_ref_ty = quote_spanned! {ty.span()=> ::yew::html::ComponentRef::< #comp_ty > };
let node_ref = if let Some(node_ref) = &special_props.node_ref {
let value = &node_ref.value;
quote_spanned! {value.span()=> ::yew::html::IntoPropValue::into_prop_value(#value) }
let value_quoted = quote_spanned! {value.span()=> #value };
quote! { ::yew::html::IntoPropValue::< #comp_ref_ty >::into_prop_value( #value_quoted ) }
} else {
quote! { <::yew::html::ComponentRef< <#ty as ::yew::html::IntoComponent>::Component > as ::std::default::Default>::default() }
quote_spanned! {ty.span()=> < #comp_ref_ty as ::std::default::Default>::default() }
};

let key = if let Some(key) = &special_props.key {
Expand All @@ -121,7 +125,7 @@ impl ToTokens for HtmlComponent {
tokens.extend(quote_spanned! {ty.span()=>
{
let __yew_props = #build_props;
::yew::virtual_dom::VChild::<#ty>::new(__yew_props, #node_ref, #key)
::yew::virtual_dom::VChild::< #ty >::new(__yew_props, #node_ref, #key)
}
});
}
Expand Down
10 changes: 8 additions & 2 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,17 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
<&'static str as IntoPropValue<Option<String>>>
and 15 others

error[E0308]: mismatched types
error[E0277]: the trait bound `(): IntoPropValue<ComponentRef<Child>>` is not satisfied
--> tests/html_macro/component-fail.rs:80:31
|
80 | html! { <Child int=1 ref={()} /> };
| ^^ expected struct `NodeRef`, found `()`
| ^^ the trait `IntoPropValue<ComponentRef<Child>>` is not implemented for `()`
|
note: required by `into_prop_value`
--> $WORKSPACE/packages/yew/src/html/conversion.rs
|
| fn into_prop_value(self) -> T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `u32: IntoPropValue<i32>` is not satisfied
--> tests/html_macro/component-fail.rs:82:24
Expand Down
10 changes: 5 additions & 5 deletions packages/yew-macro/tests/html_macro/component-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn compile_pass() {
};

let props = <<Child as ::yew::Component>::Properties as ::std::default::Default>::default();
let node_ref = <::yew::NodeRef as ::std::default::Default>::default();
let node_ref = <::yew::ComponentRef<Child> as ::std::default::Default>::default();
::yew::html! {
<>
<Child ..::std::clone::Clone::clone(&props) />
Expand Down Expand Up @@ -218,15 +218,15 @@ fn compile_pass() {
</>
};

let node_ref = <::yew::NodeRef as ::std::default::Default>::default();
let node_ref = <::yew::ComponentRef<Child> as ::std::default::Default>::default();
::yew::html! {
<>
<Child int=1 ref={node_ref} />
</>
};

let int = 1;
let node_ref = <::yew::NodeRef as ::std::default::Default>::default();
let node_ref = <::yew::ComponentRef<Child> as ::std::default::Default>::default();
::yew::html! {
<>
<Child {int} ref={node_ref} />
Expand Down Expand Up @@ -323,12 +323,12 @@ fn compile_pass() {
::std::vec![
ChildrenVariants::Child(::yew::virtual_dom::VChild::new(
<ChildProperties as ::std::default::Default>::default(),
<::yew::NodeRef as ::std::default::Default>::default(),
<::yew::ComponentRef<Child> as ::std::default::Default>::default(),
::std::option::Option::None,
)),
ChildrenVariants::AltChild(::yew::virtual_dom::VChild::new(
(),
<::yew::NodeRef as ::std::default::Default>::default(),
<::yew::ComponentRef<AltChild> as ::std::default::Default>::default(),
::std::option::Option::None
)),
]
Expand Down

0 comments on commit 8499e8f

Please sign in to comment.