diff --git a/packages/core-macro/Cargo.toml b/packages/core-macro/Cargo.toml index 2f9eef4d4..0b238bbc4 100644 --- a/packages/core-macro/Cargo.toml +++ b/packages/core-macro/Cargo.toml @@ -22,6 +22,7 @@ dioxus-rsx = { workspace = true } # testing [dev-dependencies] +dioxus = { workspace = true } rustversion = "1.0" trybuild = "1.0" diff --git a/packages/core-macro/tests/rsx.rs b/packages/core-macro/tests/rsx.rs new file mode 100644 index 000000000..f4037f928 --- /dev/null +++ b/packages/core-macro/tests/rsx.rs @@ -0,0 +1,5 @@ +#[test] +fn rsx() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/rsx/trailing-comma-0.rs"); +} diff --git a/packages/core-macro/tests/rsx/trailing-comma-0.rs b/packages/core-macro/tests/rsx/trailing-comma-0.rs new file mode 100644 index 000000000..e4735907e --- /dev/null +++ b/packages/core-macro/tests/rsx/trailing-comma-0.rs @@ -0,0 +1,13 @@ +// Given an `rsx!` invocation with a missing trailing comma, +// ensure the stderr output has an informative span. + +use dioxus::prelude::*; + +fn main() { + rsx! { + p { + class: "foo bar" + "Hello world" + } + }; +} diff --git a/packages/core-macro/tests/rsx/trailing-comma-0.stderr b/packages/core-macro/tests/rsx/trailing-comma-0.stderr new file mode 100644 index 000000000..168466572 --- /dev/null +++ b/packages/core-macro/tests/rsx/trailing-comma-0.stderr @@ -0,0 +1,5 @@ +error: missing trailing comma + --> tests/rsx/trailing-comma-0.rs:9:20 + | +9 | class: "foo bar" + | ^^^^^^^^^ diff --git a/packages/rsx/src/element.rs b/packages/rsx/src/element.rs index 466200eca..0fe6b93ee 100644 --- a/packages/rsx/src/element.rs +++ b/packages/rsx/src/element.rs @@ -76,11 +76,14 @@ impl Parse for Element { if content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]) { let name = content.parse::()?; - let ident = name.clone(); let name_str = name.to_string(); content.parse::()?; + // The span of the content to be parsed, + // for example the `hi` part of `class: "hi"`. + let span = content.span(); + if name_str.starts_with("on") { attributes.push(ElementAttrNamed { el_name: el_name.clone(), @@ -127,7 +130,7 @@ impl Parse for Element { // todo: add a message saying you need to include commas between fields if content.parse::().is_err() { - missing_trailing_comma!(ident.span()); + missing_trailing_comma!(span); } continue; }