diff --git a/packages/yew-macro/src/use_prepared_state.rs b/packages/yew-macro/src/use_prepared_state.rs index 771d765699c..c94240aab1c 100644 --- a/packages/yew-macro/src/use_prepared_state.rs +++ b/packages/yew-macro/src/use_prepared_state.rs @@ -13,11 +13,19 @@ pub struct PreparedState { impl Parse for PreparedState { fn parse(input: ParseStream) -> syn::Result { // Reads a closure. - let closure: ExprClosure = input.parse()?; + let expr: Expr = input.parse()?; - input - .parse::() - .map_err(|e| syn::Error::new(e.span(), "expected a second argument as dependency"))?; + let closure = match expr { + Expr::Closure(m) => m, + other => return Err(syn::Error::new_spanned(other, "expected closure")), + }; + + input.parse::().map_err(|e| { + syn::Error::new( + e.span(), + "this hook takes 2 arguments but 1 argument was supplied", + ) + })?; let return_type = match &closure.output { ReturnType::Default => { diff --git a/packages/yew-macro/src/use_transitive_state.rs b/packages/yew-macro/src/use_transitive_state.rs index b3a6dbc2047..08bf9fc8265 100644 --- a/packages/yew-macro/src/use_transitive_state.rs +++ b/packages/yew-macro/src/use_transitive_state.rs @@ -13,11 +13,19 @@ pub struct TransitiveState { impl Parse for TransitiveState { fn parse(input: ParseStream) -> syn::Result { // Reads a closure. - let closure: ExprClosure = input.parse()?; + let expr: Expr = input.parse()?; - input - .parse::() - .map_err(|e| syn::Error::new(e.span(), "expected a second argument as dependency"))?; + let closure = match expr { + Expr::Closure(m) => m, + other => return Err(syn::Error::new_spanned(other, "expected closure")), + }; + + input.parse::().map_err(|e| { + syn::Error::new( + e.span(), + "this hook takes 2 arguments but 1 argument was supplied", + ) + })?; let return_type = match &closure.output { ReturnType::Default => { diff --git a/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr b/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr index 37206a3dcef..62ba0033a56 100644 --- a/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr +++ b/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr @@ -1,4 +1,4 @@ -error: expected `|` +error: expected closure --> tests/hook_macro/use_prepared_state-fail.rs:6:38 | 6 | use_prepared_state_with_closure!(123)?; @@ -10,7 +10,7 @@ error: You must specify a return type for this closure. This is used when the cl 8 | use_prepared_state_with_closure!(|_| { todo!() }, 123)?; | ^^^^^^^^^^^^^^^ -error: expected a second argument as dependency +error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_prepared_state-fail.rs:10:5 | 10 | use_prepared_state_with_closure!(|_| -> u32 { todo!() })?; @@ -18,7 +18,7 @@ error: expected a second argument as dependency | = note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: expected a second argument as dependency +error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_prepared_state-fail.rs:12:5 | 12 | use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?; @@ -26,7 +26,7 @@ error: expected a second argument as dependency | = note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: expected `|` +error: expected closure --> tests/hook_macro/use_prepared_state-fail.rs:19:41 | 19 | use_prepared_state_without_closure!(123)?; @@ -38,7 +38,7 @@ error: You must specify a return type for this closure. This is used when the cl 21 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?; | ^^^^^^^^^^^^^^^ -error: expected a second argument as dependency +error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_prepared_state-fail.rs:23:5 | 23 | use_prepared_state_without_closure!(|_| -> u32 { todo!() })?; @@ -46,7 +46,7 @@ error: expected a second argument as dependency | = note: this error originates in the macro `use_prepared_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: expected a second argument as dependency +error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_prepared_state-fail.rs:25:5 | 25 | use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?; diff --git a/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr b/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr index f824f06abb5..94712261373 100644 --- a/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr +++ b/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr @@ -1,4 +1,4 @@ -error: expected `|` +error: expected closure --> tests/hook_macro/use_transitive_state-fail.rs:6:40 | 6 | use_transitive_state_with_closure!(123)?; @@ -10,7 +10,7 @@ error: You must specify a return type for this closure. This is used when the cl 8 | use_transitive_state_with_closure!(|_| { todo!() }, 123)?; | ^^^^^^^^^^^^^^^ -error: expected a second argument as dependency +error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_transitive_state-fail.rs:10:5 | 10 | use_transitive_state_with_closure!(|_| -> u32 { todo!() })?; @@ -18,7 +18,7 @@ error: expected a second argument as dependency | = note: this error originates in the macro `use_transitive_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: expected `|` +error: expected closure --> tests/hook_macro/use_transitive_state-fail.rs:17:43 | 17 | use_transitive_state_without_closure!(123)?; @@ -30,7 +30,7 @@ error: You must specify a return type for this closure. This is used when the cl 19 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?; | ^^^^^^^^^^^^^^^ -error: expected a second argument as dependency +error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_transitive_state-fail.rs:21:5 | 21 | use_transitive_state_without_closure!(|_| -> u32 { todo!() })?;