Răsfoiți Sursa

Merge pull request #1812 from ealmloff/duplicate-event-listener-rsx

check for duplicate event listeners in the rsx macro
Jonathan Kelley 1 an în urmă
părinte
comite
4f077ec664

+ 0 - 12
packages/autofmt/tests/samples/attributes.rsx

@@ -12,18 +12,6 @@ rsx! {
             let blah = 120;
             true
         },
-        onclick: move |_| {
-            let blah = 120;
-            true
-        },
-        onclick: move |_| {
-            let blah = 120;
-            true
-        },
-        onclick: move |_| {
-            let blah = 120;
-            true
-        },
         div {
             div { "hi" }
             h2 { class: "asd" }

+ 1 - 1
packages/autofmt/tests/samples/complex.rsx

@@ -6,7 +6,7 @@ rsx! {
             show_user_menu.set(!show_user_menu.get());
             evt.cancel_bubble();
         },
-        onclick: move |evt| show_user_menu.set(!show_user_menu.get()),
+        onmousedown: move |evt| show_user_menu.set(!show_user_menu.get()),
         span { class: "inline-block mr-4", icons::icon_14 {} }
         span { "Settings" }
     }

+ 0 - 7
packages/core/src/virtual_dom.rs

@@ -401,13 +401,6 @@ impl VirtualDom {
                                 }
                             }
                         });
-
-                        // Break if this is the exact target element.
-                        // This means we won't call two listeners with the same name on the same element. This should be
-                        // documented, or be rejected from the rsx! macro outright
-                        if target_path == this_path {
-                            break;
-                        }
                     }
                 }
 

+ 21 - 0
packages/rsx/src/element.rs

@@ -99,6 +99,27 @@ impl Parse for Element {
                 let span = content.span();
 
                 if name_str.starts_with("on") {
+                    // check for any duplicate event listeners
+                    if attributes.iter().any(|f| {
+                        if let AttributeType::Named(ElementAttrNamed {
+                            attr:
+                                ElementAttr {
+                                    name: ElementAttrName::BuiltIn(n),
+                                    value: ElementAttrValue::EventTokens(_),
+                                },
+                            ..
+                        }) = f
+                        {
+                            n == &name_str
+                        } else {
+                            false
+                        }
+                    }) {
+                        return Err(syn::Error::new(
+                            name.span(),
+                            format!("Duplicate event listener `{}`", name),
+                        ));
+                    }
                     attributes.push(attribute::AttributeType::Named(ElementAttrNamed {
                         el_name: el_name.clone(),
                         attr: ElementAttr {