diff --git a/packages/yew-macro/src/html_tree/html_component.rs b/packages/yew-macro/src/html_tree/html_component.rs index e3ef15954e9..4f557134c36 100644 --- a/packages/yew-macro/src/html_tree/html_component.rs +++ b/packages/yew-macro/src/html_tree/html_component.rs @@ -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.: )", + 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::()?; - let div = Some(fork.parse::()?); - let ty = fork.parse::()?; - 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::]>()?; - let close = HtmlComponentClose { - tag: TagTokens { lt, div, gt }, - ty, - }; - input.advance_to(&fork); - break close; - } + })?; } children.parse_child(input)?; }; diff --git a/packages/yew-macro/tests/html_macro/component-fail.stderr b/packages/yew-macro/tests/html_macro/component-fail.stderr index 88716431599..15229044d0e 100644 --- a/packages/yew-macro/tests/html_macro/component-fail.stderr +++ b/packages/yew-macro/tests/html_macro/component-fail.stderr @@ -392,13 +392,13 @@ error: mismatched closing tags: expected `A`, found `B` 177 | let _ = html! { }; | ^^^^^ -error: expected a valid closing tag (e.g.: ) - --> tests/html_macro/component-fail.rs:178:13 +error: expected a valid closing tag for component + note: found opening tag `` + help: try `` + --> tests/html_macro/component-fail.rs:178:24 | 178 | let _ = html! { }; - | ^^^^^^^^^^^^^^^^ - | - = 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 diff --git a/packages/yew-macro/tests/html_macro/generic-component-fail.stderr b/packages/yew-macro/tests/html_macro/generic-component-fail.stderr index aa7dbd2b589..a0071a87b44 100644 --- a/packages/yew-macro/tests/html_macro/generic-component-fail.stderr +++ b/packages/yew-macro/tests/html_macro/generic-component-fail.stderr @@ -22,10 +22,10 @@ error: mismatched closing tags: expected `Generic`, found `Generic 50 | html! { >> }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: expected a valid closing tag (e.g.: ) - --> tests/html_macro/generic-component-fail.rs:51:5 +error: expected a valid closing tag for component + note: found opening tag `>` + help: try `>` + --> tests/html_macro/generic-component-fail.rs:51:30 | 51 | html! { > }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info) + | ^^^