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

Remove extra braces in html_nested macro #2169

Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion packages/yew-macro/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
clear = true
toolchain = "1.51"
command = "cargo"
args = ["test"]
# test target can be optionally specified like `cargo make test html_macro`,
args = ["test", "${@}"]
Comment on lines +5 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this useful in development


[tasks.test-overwrite]
extend = "test"
Expand Down
4 changes: 2 additions & 2 deletions packages/yew-macro/src/html_tree/html_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ impl ToTokens for HtmlComponent {

tokens.extend(quote_spanned! {ty.span()=>
{
#[allow(clippy::unit_arg)]
Copy link
Contributor Author

@Madoshakalaka Madoshakalaka Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the now __yew_props was inlined in the new call. When there are no properties (e.g. html_nested!{<MyComponent/>}), #build_props would return MyComponent::Properties::builder().build() which is the unit type, triggering the unit arg warning.
Extracting a variable solves that.

::yew::virtual_dom::VChild::<#ty>::new(#build_props, #node_ref, #key)
let __yew_props = #build_props;
::yew::virtual_dom::VChild::<#ty>::new(__yew_props, #node_ref, #key)
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions packages/yew-macro/src/html_tree/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ impl ToTokens for HtmlElement {
// this way we get a nice error message (with the correct span) when the expression
// doesn't return a valid value
quote_spanned! {expr.span()=> {
#[allow(unused_braces)]
// e.g. html!{<@{"div"}/>} will set `#expr` to `{"div"}`
// (note the extra braces). Hence the need for the `allow`.
// Anyways to remove the braces?
let mut #vtag_name = ::std::convert::Into::<
::std::borrow::Cow::<'static, ::std::primitive::str>
>::into(#expr);
Comment on lines +364 to 370
Copy link
Contributor Author

@Madoshakalaka Madoshakalaka Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the misplaced #[allow] really belongs. Maybe the allow here is also liftable? I'm however too inexperienced with proc macro to figure it out. So I just left some comments instead

Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/html_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl ToTokens for HtmlRootVNode {
fn to_tokens(&self, tokens: &mut TokenStream) {
let new_tokens = self.0.to_token_stream();
tokens.extend(quote! {{
#[allow(clippy::useless_conversion, unused_braces)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part is for the html! macro, it's no longer needed.

#[allow(clippy::useless_conversion)]
<::yew::virtual_dom::VNode as ::std::convert::From<_>>::from(#new_tokens)
}});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0412]: cannot find type `INVALID` in this scope
--> $DIR/generic-props-fail.rs:25:19
--> tests/function_component_attr/generic-props-fail.rs:25:19
|
20 | fn compile_fail() {
| - help: you might be missing a type parameter: `<INVALID>`
Expand All @@ -8,16 +8,24 @@ error[E0412]: cannot find type `INVALID` in this scope
| ^^^^^^^ not found in this scope

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

error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
--> tests/function_component_attr/generic-props-fail.rs:27:14
|
27 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
|
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`

error[E0599]: the function or associated item `new` exists for struct `VChild<FunctionComponent<comp<MissingTypeBounds>>>`, but its trait bounds were not satisfied
--> $DIR/generic-props-fail.rs:27:14
--> tests/function_component_attr/generic-props-fail.rs:27:14
|
27 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ function or associated item cannot be called on `VChild<FunctionComponent<comp<MissingTypeBounds>>>` due to unsatisfied trait bounds
Expand All @@ -30,22 +38,14 @@ error[E0599]: the function or associated item `new` exists for struct `VChild<Fu
= note: the following trait bounds were not satisfied:
`FunctionComponent<comp<MissingTypeBounds>>: yew::Component`

error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
--> $DIR/generic-props-fail.rs:27:14
|
27 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
|
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`

error[E0107]: missing generics for type alias `Comp`
--> $DIR/generic-props-fail.rs:30:14
--> tests/function_component_attr/generic-props-fail.rs:30:14
|
30 | html! { <Comp /> };
| ^^^^ expected 1 type argument
|
note: type alias defined here, with 1 type parameter: `P`
--> $DIR/generic-props-fail.rs:8:22
--> tests/function_component_attr/generic-props-fail.rs:8:22
|
8 | #[function_component(Comp)]
| ^^^^
Expand Down
63 changes: 63 additions & 0 deletions packages/yew-macro/tests/html_macro/as-return-value-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

pub struct MyComponent;
impl ::yew::Component for MyComponent {
type Message = ();
type Properties = ();
fn create(_ctx: &::yew::Context<Self>) -> Self {
::std::unimplemented!()
}
fn view(&self, _ctx: &::yew::Context<Self>) -> ::yew::Html {
::std::unimplemented!()
}
}

// can test "unused braces" warning inside the macro
// /~https://github.com/yewstack/yew/issues/2157
fn make_my_component()-> ::yew::virtual_dom::VChild<MyComponent>{
::yew::html_nested!{<MyComponent/>}
}

// can test "unused braces" warning inside the macro
// /~https://github.com/yewstack/yew/issues/2157
fn make_my_component_html()-> ::yew::Html{
::yew::html!{<MyComponent/>}
}
Comment on lines +51 to +61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test for both html and html_nested


fn main(){}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
error[E0277]: the trait bound `Unimplemented: yew::Component` is not satisfied
--> tests/html_macro/component-unimplemented-fail.rs:6:14
|
6 | html! { <Unimplemented /> };
| ^^^^^^^^^^^^^ the trait `yew::Component` is not implemented for `Unimplemented`

error[E0599]: the function or associated item `new` exists for struct `VChild<Unimplemented>`, but its trait bounds were not satisfied
--> $DIR/component-unimplemented-fail.rs:6:14
--> tests/html_macro/component-unimplemented-fail.rs:6:14
|
3 | struct Unimplemented;
| --------------------- doesn't satisfy `Unimplemented: yew::Component`
Expand All @@ -9,9 +15,3 @@ error[E0599]: the function or associated item `new` exists for struct `VChild<Un
|
= note: the following trait bounds were not satisfied:
`Unimplemented: yew::Component`

error[E0277]: the trait bound `Unimplemented: yew::Component` is not satisfied
--> $DIR/component-unimplemented-fail.rs:6:14
|
6 | html! { <Unimplemented /> };
| ^^^^^^^^^^^^^ the trait `yew::Component` is not implemented for `Unimplemented`