Skip to content

Commit

Permalink
better diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza1311 committed Oct 8, 2022
1 parent ff923bd commit 3dae63a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
67 changes: 32 additions & 35 deletions packages/yew-macro/src/html_tree/html_component.rs
Expand Up @@ -55,46 +55,43 @@ impl Parse for HtmlComponent {
}

if trying_to_close() {
let cursor = input.cursor();
let _ = cursor
.punct()
.and_then(|(_, cursor)| cursor.punct())
.and_then(|(_, cursor)| cursor.ident())
.ok_or_else(|| {
fn format_token_stream(ts: impl ToTokens) -> String {
let string = ts.to_token_stream().to_string();
// remove unnecessary spaces
string.replace(' ', "")
}

let fork = input.fork();
break TagTokens::parse_end_content(&fork, |i_fork, tag| {
let ty = i_fork.parse().map_err(|e| {
syn::Error::new(
Span::call_site(),
"expected a valid closing tag (e.g.: </Component>)",
e.span(),
format!(
"expected a valid closing tag for component\nnote: found opening \
tag `{lt}{0}{gt}`\nhelp: try `{lt}/{0}{gt}`",
format_token_stream(&open.ty),
lt = open.tag.lt.to_token_stream(),
gt = open.tag.gt.to_token_stream(),
),
)
})?;

let fork = input.fork();
let lt = fork.parse::<Token![<]>()?;
let div = Some(fork.parse::<Token![/]>()?);
let ty = fork.parse::<Type>()?;
if ty != open.ty {
fn format_token_stream(ts: impl ToTokens) -> String {
let string = ts.to_token_stream().to_string();
// remove unnecessary spaces
string.replace(' ', "")
if ty != open.ty {
let open_ty = &open.ty;
Err(syn::Error::new_spanned(
quote!(#open_ty #ty),
format!(
"mismatched closing tags: expected `{}`, found `{}`",
format_token_stream(open_ty),
format_token_stream(ty)
),
))
} else {
let close = HtmlComponentClose { tag, ty };
input.advance_to(&fork);
Ok(close)
}
let open_ty = open.ty;
return Err(syn::Error::new_spanned(
quote!(#open_ty #ty),
format!(
"mismatched closing tags: expected `{}`, found `{}`",
format_token_stream(open_ty),
format_token_stream(ty)
),
));
} else {
let gt = fork.parse::<Token![>]>()?;
let close = HtmlComponentClose {
tag: TagTokens { lt, div, gt },
ty,
};
input.advance_to(&fork);
break close;
}
})?;
}
children.parse_child(input)?;
};
Expand Down
10 changes: 5 additions & 5 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Expand Up @@ -392,13 +392,13 @@ error: mismatched closing tags: expected `A`, found `B`
177 | let _ = html! { <A></B> };
| ^^^^^

error: expected a valid closing tag (e.g.: </Component>)
--> tests/html_macro/component-fail.rs:178:13
error: expected a valid closing tag for component
note: found opening tag `<A>`
help: try `</A>`
--> tests/html_macro/component-fail.rs:178:24
|
178 | let _ = html! { <A></> };
| ^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info)
| ^^^

error[E0425]: cannot find value `blah` in this scope
--> tests/html_macro/component-fail.rs:82:22
Expand Down
Expand Up @@ -22,10 +22,10 @@ error: mismatched closing tags: expected `Generic<String>`, found `Generic<Path>
50 | html! { <Generic<String>></Generic<Path>> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected a valid closing tag (e.g.: </Component>)
--> tests/html_macro/generic-component-fail.rs:51:5
error: expected a valid closing tag for component
note: found opening tag `<Generic<String>>`
help: try `</Generic<String>>`
--> tests/html_macro/generic-component-fail.rs:51:30
|
51 | html! { <Generic<String>></> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info)
| ^^^

0 comments on commit 3dae63a

Please sign in to comment.