Skip to content

Commit

Permalink
Add braces when html_nested used on html element (#2270)
Browse files Browse the repository at this point in the history
* add braces when html_nested used on html element

* run rustfmt
  • Loading branch information
Madoshakalaka committed Dec 15, 2021
1 parent c2d39d7 commit 1ef364f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
21 changes: 14 additions & 7 deletions packages/yew-macro/src/html_tree/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,9 @@ impl ToTokens for HtmlElement {
TagName::Lit(name) => {
let name_span = name.span();
let name = name.to_ascii_lowercase_string();
match &*name {
let node = match &*name {
"input" => {
quote_spanned! {name_span=>
#[allow(clippy::redundant_clone, unused_braces)]
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_input(
#value,
Expand All @@ -315,8 +314,7 @@ impl ToTokens for HtmlElement {
}
}
"textarea" => {
quote_spanned! {name_span=>
#[allow(clippy::redundant_clone, unused_braces)]
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_textarea(
#value,
Expand All @@ -329,8 +327,7 @@ impl ToTokens for HtmlElement {
}
}
_ => {
quote_spanned! {name_span=>
#[allow(clippy::redundant_clone, unused_braces)]
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_other(
::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#name),
Expand All @@ -343,6 +340,16 @@ impl ToTokens for HtmlElement {
)
}
}
};
// the return value can be inlined without the braces when this is stable:
// https://github.com/rust-lang/rust/issues/15701
quote_spanned!{
name_span =>
{
#[allow(clippy::redundant_clone, unused_braces)]
let node = #node;
node
}
}
}
TagName::Expr(name) => {
Expand Down
14 changes: 13 additions & 1 deletion packages/yew-macro/tests/html_macro_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use yew::html;
use yew::{html, html_nested};

#[allow(dead_code)]
#[rustversion::attr(stable(1.51), test)]
Expand Down Expand Up @@ -28,3 +28,15 @@ fn dynamic_tags_catch_non_ascii() {
<@{"❤"}/>
};
}

/// test that compilation on html elements pass
/// fixes: https://github.com/yewstack/yew/issues/2268
#[test]
fn html_nested_macro_on_html_element() {
let _node = html_nested! {
<div/>
};
let _node = html_nested! {
<input/>
};
}

0 comments on commit 1ef364f

Please sign in to comment.