Skip to content

Commit

Permalink
[yew-macro] Improve error message (#1219)
Browse files Browse the repository at this point in the history
* [X] missing property value as in `<input autofocus />`
* [X] missing identifier after `with` as in `<Button with>`
  • Loading branch information
kaoet authored and mergify[bot] committed May 14, 2020
1 parent e9c24ef commit aa5faeb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 3 additions & 1 deletion yew-macro/src/html_tree/html_component.rs
Expand Up @@ -398,7 +398,9 @@ impl Parse for Props {

input.parse::<Ident>()?;
props = Props::With(Box::new(WithProps {
props: input.parse::<Ident>()?,
props: input.parse::<Ident>().map_err(|_| {
syn::Error::new_spanned(&token, "`with` must be followed by an identifier")
})?,
node_ref: None,
key: None,
}));
Expand Down
9 changes: 4 additions & 5 deletions yew-macro/src/html_tree/html_prop.rs
@@ -1,6 +1,5 @@
use crate::html_tree::HtmlDashedName as HtmlPropLabel;
use crate::{Peek, PeekValue};
use boolinator::Boolinator;
use proc_macro::TokenStream;
use proc_macro2::TokenTree;
use syn::buffer::Cursor;
Expand All @@ -14,16 +13,16 @@ pub struct HtmlProp {

impl PeekValue<()> for HtmlProp {
fn peek(cursor: Cursor) -> Option<()> {
let (_, cursor) = HtmlPropLabel::peek(cursor)?;
let (punct, _) = cursor.punct()?;
(punct.as_char() == '=').as_option()
HtmlPropLabel::peek(cursor).map(|_| ())
}
}

impl Parse for HtmlProp {
fn parse(input: ParseStream) -> ParseResult<Self> {
let label = input.parse::<HtmlPropLabel>()?;
input.parse::<Token![=]>()?;
input
.parse::<Token![=]>()
.map_err(|_| syn::Error::new_spanned(&label, "this prop doesn't have a value"))?;
let value = input.parse::<Expr>()?;
// backwards compat
let _ = input.parse::<Token![,]>();
Expand Down
8 changes: 4 additions & 4 deletions yew-macro/tests/macro/html-component-fail.stderr
Expand Up @@ -14,15 +14,15 @@ error: expected identifier
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: unexpected end of input, expected identifier
--> $DIR/html-component-fail.rs:81:5
error: `with` must be followed by an identifier
--> $DIR/html-component-fail.rs:81:20
|
81 | html! { <Child with /> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: unexpected token
error: this prop doesn't have a value
--> $DIR/html-component-fail.rs:82:20
|
82 | html! { <Child props /> };
Expand Down

0 comments on commit aa5faeb

Please sign in to comment.