Add a warning for keys after the first node which do nothing (#4978)

* add a warning for keys after the first node which do nothing

* warnings on stable

* better warnings on nightly
This commit is contained in:
Evan Almloff authored and GitHub committed 2025-11-30 15:29:50 -05:00
1 parent c58526a36b
commit 386153672e
5 files changed
+57 -5

No files matched your search

+21 -5
View File
@@ -109,6 +109,8 @@ impl ToTokens for TemplateBody {
None => quote! { None },
};
let key_warnings = self.check_for_duplicate_keys();
let roots = node.quote_roots();
// Print paths is easy - just print the paths
@@ -138,6 +140,8 @@ impl ToTokens for TemplateBody {
dioxus_core::Element::Ok({
#diagnostics
#key_warnings
// Components pull in the dynamic literal pool and template in debug mode, so they need to be defined before dynamic nodes
#[cfg(debug_assertions)]
fn __original_template() -> &'static dioxus_core::internal::HotReloadedTemplate {
@@ -273,11 +277,7 @@ impl TemplateBody {
}
pub fn implicit_key(&self) -> Option<&AttributeValue> {
match self.roots.first() {
Some(BodyNode::Element(el)) => el.key(),
Some(BodyNode::Component(comp)) => comp.get_key(),
_ => None,
}
self.roots.first().and_then(BodyNode::key)
}
/// Ensure only one key and that the key is not a static str
@@ -304,6 +304,22 @@ impl TemplateBody {
}
}
fn check_for_duplicate_keys(&self) -> TokenStream2 {
let mut warnings = TokenStream2::new();
// Make sure there are not multiple keys or keys on nodes other than the first in the block
for root in self.roots.iter().skip(1) {
if let Some(key) = root.key() {
warnings.extend(new_diagnostics::warning_diagnostic(
key.span(),
"Keys are only allowed on the first node in the block.",
));
}
}
warnings
}
pub fn get_dyn_node(&self, path: &[u8]) -> &BodyNode {
let mut node = self.roots.get(path[0] as usize).unwrap();
for idx in path.iter().skip(1) {