Selaa lähdekoodia

Merge pull request #877 from Demonthos/fix-component-attribute-formatting

Don't accept raw attributes in components without trailing commas
Jon Kelley 2 vuotta sitten
vanhempi
commit
6f38c765e5
2 muutettua tiedostoa jossa 19 lisäystä ja 21 poistoa
  1. 1 1
      packages/router/tests/web_router.rs
  2. 18 20
      packages/rsx/src/component.rs

+ 1 - 1
packages/router/tests/web_router.rs

@@ -22,7 +22,7 @@ fn simple_test() {
                 onchange: move |router: RouterContext| log::trace!("route changed to {:?}", router.current_location()),
                 active_class: "is-active",
                 Route { to: "/", Home {} }
-                Route { to: "blog"
+                Route { to: "blog",
                     Route { to: "/", BlogList {} }
                     Route { to: ":id", BlogPost {} }
                 }

+ 18 - 20
packages/rsx/src/component.rs

@@ -230,30 +230,28 @@ impl Parse for ComponentField {
         let name = Ident::parse_any(input)?;
         input.parse::<Token![:]>()?;
 
-        if name.to_string().starts_with("on") {
-            let content = ContentField::OnHandlerRaw(input.parse()?);
-            return Ok(Self { name, content });
-        }
-
-        if name == "key" {
-            let content = ContentField::Formatted(input.parse()?);
-            return Ok(Self { name, content });
-        }
-        if input.peek(LitStr) {
-            let forked = input.fork();
-            let t: LitStr = forked.parse()?;
-            // the string literal must either be the end of the input or a followed by a comma
-            if (forked.is_empty() || forked.peek(Token![,])) && is_literal_foramtted(&t) {
+        let content = {
+            if name.to_string().starts_with("on") {
+                ContentField::OnHandlerRaw(input.parse()?)
+            } else if name == "key" {
                 let content = ContentField::Formatted(input.parse()?);
                 return Ok(Self { name, content });
+            } else if input.peek(LitStr) {
+                let forked = input.fork();
+                let t: LitStr = forked.parse()?;
+                // the string literal must either be the end of the input or a followed by a comma
+                if (forked.is_empty() || forked.peek(Token![,])) && is_literal_foramtted(&t) {
+                    ContentField::Formatted(input.parse()?)
+                } else {
+                    ContentField::ManExpr(input.parse()?)
+                }
+            } else {
+                ContentField::ManExpr(input.parse()?)
             }
+        };
+        if input.peek(LitStr) || input.peek(Ident) {
+            missing_trailing_comma!(content.span());
         }
-
-        if input.peek(LitStr) && input.peek2(LitStr) {
-            missing_trailing_comma!(input.span());
-        }
-
-        let content = ContentField::ManExpr(input.parse()?);
         Ok(Self { name, content })
     }
 }