From 1f065acb4c4e4fbae152d2fffc603ab6434dc4ee Mon Sep 17 00:00:00 2001 From: Ilya Maximov Date: Sat, 19 Mar 2022 20:37:25 +0100 Subject: [PATCH] refactor: simplify and organize the errors --- packages/core-macro/src/rsx/component.rs | 3 +- packages/core-macro/src/rsx/element.rs | 37 +++--------------------- packages/core-macro/src/rsx/errors.rs | 15 ++++++++++ packages/core-macro/src/rsx/mod.rs | 3 ++ 4 files changed, 23 insertions(+), 35 deletions(-) create mode 100644 packages/core-macro/src/rsx/errors.rs diff --git a/packages/core-macro/src/rsx/component.rs b/packages/core-macro/src/rsx/component.rs index 064e2dad6..ce906e76f 100644 --- a/packages/core-macro/src/rsx/component.rs +++ b/packages/core-macro/src/rsx/component.rs @@ -199,8 +199,7 @@ impl Parse for ComponentField { } if input.peek(LitStr) && input.peek2(LitStr) { - let item = input.parse::().unwrap(); - proc_macro_error::emit_error!(item, "this attribute is missing a trailing comma") + attr_after_element!(input.span()); } let content = ContentField::ManExpr(input.parse()?); diff --git a/packages/core-macro/src/rsx/element.rs b/packages/core-macro/src/rsx/element.rs index 92aa949ce..96c7a2efc 100644 --- a/packages/core-macro/src/rsx/element.rs +++ b/packages/core-macro/src/rsx/element.rs @@ -63,12 +63,8 @@ impl Parse for Element { break; } - // todo: add a message saying you need to include commas between fields if content.parse::().is_err() { - proc_macro_error::emit_error!( - ident, - "this attribute is missing a trailing comma" - ) + missing_trailing_comma!(ident); } continue; } @@ -126,10 +122,7 @@ impl Parse for Element { // todo: add a message saying you need to include commas between fields if content.parse::().is_err() { - proc_macro_error::emit_error!( - ident, - "this attribute is missing a trailing comma" - ) + missing_trailing_comma!(ident); } continue; } @@ -139,33 +132,11 @@ impl Parse for Element { while !content.is_empty() { if (content.peek(LitStr) && content.peek2(Token![:])) && !content.peek3(Token![:]) { - let ident = content.parse::().unwrap(); - let name = ident.value(); - proc_macro_error::emit_error!( - ident, "this attribute `{}` is in the wrong place", name; - help = -"all attribute fields must be placed above children elements - - div { - attr: \"...\", <---- attribute is above children - div { } <---- children are below attributes - }"; - ) + attr_after_element!(content.span()); } if (content.peek(Ident) && content.peek2(Token![:])) && !content.peek3(Token![:]) { - let ident = content.parse::().unwrap(); - let name = ident.to_string(); - proc_macro_error::emit_error!( - ident, "this attribute `{}` is in the wrong place", name; - help = -"all attribute fields must be placed above children elements - - div { - attr: \"...\", <---- attribute is above children - div { } <---- children are below attributes - }"; - ) + attr_after_element!(content.span()); } children.push(content.parse::()?); diff --git a/packages/core-macro/src/rsx/errors.rs b/packages/core-macro/src/rsx/errors.rs new file mode 100644 index 000000000..27e2c7f98 --- /dev/null +++ b/packages/core-macro/src/rsx/errors.rs @@ -0,0 +1,15 @@ +macro_rules! missing_trailing_comma { + ($span:expr) => { + proc_macro_error::emit_error!($span, "missing trailing comma") + }; +} + +macro_rules! attr_after_element { + ($span:expr) => { + proc_macro_error::emit_error!( + $span, + "expected element"; + help = "move the attribute above all the children and text elements" + ) + }; +} diff --git a/packages/core-macro/src/rsx/mod.rs b/packages/core-macro/src/rsx/mod.rs index 858f07c5c..6b9dfc3dc 100644 --- a/packages/core-macro/src/rsx/mod.rs +++ b/packages/core-macro/src/rsx/mod.rs @@ -11,6 +11,9 @@ //! //! Any errors in using rsx! will likely occur when people start using it, so the first errors must be really helpful. +#[macro_use] +mod errors; + mod component; mod element; mod node;