Skip to content

Commit 4c3d693

Browse files
Remove extra braces in html_nested macro (#2169)
* removed unused braces from html_nested marco * allow specifying test name * also fix for html! macro * also fix for html! macro * remove misplaced #[allow(unused_braces)]
1 parent 7e2542c commit 4c3d693

File tree

7 files changed

+92
-25
lines changed

7 files changed

+92
-25
lines changed

packages/yew-macro/Makefile.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
clear = true
33
toolchain = "1.51"
44
command = "cargo"
5-
args = ["test"]
5+
# test target can be optionally specified like `cargo make test html_macro`,
6+
args = ["test", "${@}"]
67

78
[tasks.test-lint]
89
clear = true

packages/yew-macro/src/html_tree/html_component.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl ToTokens for HtmlComponent {
120120

121121
tokens.extend(quote_spanned! {ty.span()=>
122122
{
123-
#[allow(clippy::unit_arg)]
124-
::yew::virtual_dom::VChild::<#ty>::new(#build_props, #node_ref, #key)
123+
let __yew_props = #build_props;
124+
::yew::virtual_dom::VChild::<#ty>::new(__yew_props, #node_ref, #key)
125125
}
126126
});
127127
}

packages/yew-macro/src/html_tree/html_element.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ impl ToTokens for HtmlElement {
346346
}
347347
}
348348
TagName::Expr(name) => {
349-
#[allow(unused_braces)]
350349
let vtag = Ident::new("__yew_vtag", name.span());
351350
let expr = &name.expr;
352351
let vtag_name = Ident::new("__yew_vtag_name", expr.span());
@@ -362,6 +361,10 @@ impl ToTokens for HtmlElement {
362361
// this way we get a nice error message (with the correct span) when the expression
363362
// doesn't return a valid value
364363
quote_spanned! {expr.span()=> {
364+
#[allow(unused_braces)]
365+
// e.g. html!{<@{"div"}/>} will set `#expr` to `{"div"}`
366+
// (note the extra braces). Hence the need for the `allow`.
367+
// Anyways to remove the braces?
365368
let mut #vtag_name = ::std::convert::Into::<
366369
::std::borrow::Cow::<'static, ::std::primitive::str>
367370
>::into(#expr);

packages/yew-macro/src/html_tree/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl ToTokens for HtmlRootVNode {
171171
fn to_tokens(&self, tokens: &mut TokenStream) {
172172
let new_tokens = self.0.to_token_stream();
173173
tokens.extend(quote! {{
174-
#[allow(clippy::useless_conversion, unused_braces)]
174+
#[allow(clippy::useless_conversion)]
175175
<::yew::virtual_dom::VNode as ::std::convert::From<_>>::from(#new_tokens)
176176
}});
177177
}

packages/yew-macro/tests/function_component_attr/generic-props-fail.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0412]: cannot find type `INVALID` in this scope
2-
--> $DIR/generic-props-fail.rs:25:19
2+
--> tests/function_component_attr/generic-props-fail.rs:25:19
33
|
44
20 | fn compile_fail() {
55
| - help: you might be missing a type parameter: `<INVALID>`
@@ -8,16 +8,24 @@ error[E0412]: cannot find type `INVALID` in this scope
88
| ^^^^^^^ not found in this scope
99

1010
error[E0599]: no method named `build` found for struct `PropsBuilder<PropsBuilderStep_missing_required_prop_a>` in the current scope
11-
--> $DIR/generic-props-fail.rs:22:14
11+
--> tests/function_component_attr/generic-props-fail.rs:22:14
1212
|
1313
3 | #[derive(Clone, Properties, PartialEq)]
1414
| ---------- method `build` not found for this
1515
...
1616
22 | html! { <Comp<Props> /> };
1717
| ^^^^ method not found in `PropsBuilder<PropsBuilderStep_missing_required_prop_a>`
1818

19+
error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
20+
--> tests/function_component_attr/generic-props-fail.rs:27:14
21+
|
22+
27 | html! { <Comp<MissingTypeBounds> /> };
23+
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
24+
|
25+
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`
26+
1927
error[E0599]: the function or associated item `new` exists for struct `VChild<FunctionComponent<comp<MissingTypeBounds>>>`, but its trait bounds were not satisfied
20-
--> $DIR/generic-props-fail.rs:27:14
28+
--> tests/function_component_attr/generic-props-fail.rs:27:14
2129
|
2230
27 | html! { <Comp<MissingTypeBounds> /> };
2331
| ^^^^ function or associated item cannot be called on `VChild<FunctionComponent<comp<MissingTypeBounds>>>` due to unsatisfied trait bounds
@@ -30,22 +38,14 @@ error[E0599]: the function or associated item `new` exists for struct `VChild<Fu
3038
= note: the following trait bounds were not satisfied:
3139
`FunctionComponent<comp<MissingTypeBounds>>: yew::Component`
3240

33-
error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
34-
--> $DIR/generic-props-fail.rs:27:14
35-
|
36-
27 | html! { <Comp<MissingTypeBounds> /> };
37-
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
38-
|
39-
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`
40-
4141
error[E0107]: missing generics for type alias `Comp`
42-
--> $DIR/generic-props-fail.rs:30:14
42+
--> tests/function_component_attr/generic-props-fail.rs:30:14
4343
|
4444
30 | html! { <Comp /> };
4545
| ^^^^ expected 1 type argument
4646
|
4747
note: type alias defined here, with 1 type parameter: `P`
48-
--> $DIR/generic-props-fail.rs:8:22
48+
--> tests/function_component_attr/generic-props-fail.rs:8:22
4949
|
5050
8 | #[function_component(Comp)]
5151
| ^^^^
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![no_implicit_prelude]
2+
3+
// Shadow primitives
4+
#[allow(non_camel_case_types)]
5+
pub struct bool;
6+
#[allow(non_camel_case_types)]
7+
pub struct char;
8+
#[allow(non_camel_case_types)]
9+
pub struct f32;
10+
#[allow(non_camel_case_types)]
11+
pub struct f64;
12+
#[allow(non_camel_case_types)]
13+
pub struct i128;
14+
#[allow(non_camel_case_types)]
15+
pub struct i16;
16+
#[allow(non_camel_case_types)]
17+
pub struct i32;
18+
#[allow(non_camel_case_types)]
19+
pub struct i64;
20+
#[allow(non_camel_case_types)]
21+
pub struct i8;
22+
#[allow(non_camel_case_types)]
23+
pub struct isize;
24+
#[allow(non_camel_case_types)]
25+
pub struct str;
26+
#[allow(non_camel_case_types)]
27+
pub struct u128;
28+
#[allow(non_camel_case_types)]
29+
pub struct u16;
30+
#[allow(non_camel_case_types)]
31+
pub struct u32;
32+
#[allow(non_camel_case_types)]
33+
pub struct u64;
34+
#[allow(non_camel_case_types)]
35+
pub struct u8;
36+
#[allow(non_camel_case_types)]
37+
pub struct usize;
38+
39+
pub struct MyComponent;
40+
impl ::yew::Component for MyComponent {
41+
type Message = ();
42+
type Properties = ();
43+
fn create(_ctx: &::yew::Context<Self>) -> Self {
44+
::std::unimplemented!()
45+
}
46+
fn view(&self, _ctx: &::yew::Context<Self>) -> ::yew::Html {
47+
::std::unimplemented!()
48+
}
49+
}
50+
51+
// can test "unused braces" warning inside the macro
52+
// https://github.com/yewstack/yew/issues/2157
53+
fn make_my_component()-> ::yew::virtual_dom::VChild<MyComponent>{
54+
::yew::html_nested!{<MyComponent/>}
55+
}
56+
57+
// can test "unused braces" warning inside the macro
58+
// https://github.com/yewstack/yew/issues/2157
59+
fn make_my_component_html()-> ::yew::Html{
60+
::yew::html!{<MyComponent/>}
61+
}
62+
63+
fn main(){}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
error[E0277]: the trait bound `Unimplemented: yew::Component` is not satisfied
2+
--> tests/html_macro/component-unimplemented-fail.rs:6:14
3+
|
4+
6 | html! { <Unimplemented /> };
5+
| ^^^^^^^^^^^^^ the trait `yew::Component` is not implemented for `Unimplemented`
6+
17
error[E0599]: the function or associated item `new` exists for struct `VChild<Unimplemented>`, but its trait bounds were not satisfied
2-
--> $DIR/component-unimplemented-fail.rs:6:14
8+
--> tests/html_macro/component-unimplemented-fail.rs:6:14
39
|
410
3 | struct Unimplemented;
511
| --------------------- doesn't satisfy `Unimplemented: yew::Component`
@@ -9,9 +15,3 @@ error[E0599]: the function or associated item `new` exists for struct `VChild<Un
915
|
1016
= note: the following trait bounds were not satisfied:
1117
`Unimplemented: yew::Component`
12-
13-
error[E0277]: the trait bound `Unimplemented: yew::Component` is not satisfied
14-
--> $DIR/component-unimplemented-fail.rs:6:14
15-
|
16-
6 | html! { <Unimplemented /> };
17-
| ^^^^^^^^^^^^^ the trait `yew::Component` is not implemented for `Unimplemented`

0 commit comments

Comments
 (0)