Browse Source

feat: cleanuup

Jonathan Kelley 3 năm trước cách đây
mục cha
commit
84fd0c6162

+ 4 - 3
packages/core-macro/src/htm.rs

@@ -175,7 +175,7 @@ impl ToTokens for ToToksCtx<&Element> {
                 if let Some(child) = children.next() {
                     let mut inner_toks = TokenStream2::new();
                     self.recurse(child).to_tokens(&mut inner_toks);
-                    while let Some(child) = children.next() {
+                    for child in children {
                         quote!(,).to_tokens(&mut inner_toks);
                         self.recurse(child).to_tokens(&mut inner_toks);
                     }
@@ -234,7 +234,7 @@ impl Parse for Element {
         s.parse::<Token![<]>()?;
         s.parse::<Token![/]>()?;
         let close = Ident::parse_any(s)?;
-        if close.to_string() != name.to_string() {
+        if close != name {
             return Err(Error::new_spanned(
                 close,
                 "closing element does not match opening",
@@ -271,7 +271,7 @@ impl Parse for Attr {
         // If so, parse into literal tokens
         let ty = if name_str.starts_with("on") {
             // remove the "on" bit
-            name = Ident::new(&name_str.trim_start_matches("on"), name.span());
+            name = Ident::new(name_str.trim_start_matches("on"), name.span());
             let content;
             syn::braced!(content in s);
             // AttrType::Value(content.parse()?)
@@ -359,6 +359,7 @@ impl ToTokens for ToToksCtx<&TextNode> {
     }
 }
 
+#[allow(clippy::large_enum_variant)]
 enum MaybeExpr<T> {
     Literal(T),
     Expr(Expr),

+ 6 - 12
packages/core-macro/src/ifmt.rs

@@ -7,12 +7,6 @@ use ::syn::{
 };
 use proc_macro2::TokenStream;
 
-macro_rules! debug_output {
-    ($expr:expr) => {
-        $expr
-    };
-}
-
 pub fn format_args_f_impl(input: IfmtInput) -> Result<TokenStream> {
     let IfmtInput {
         mut format_literal,
@@ -21,7 +15,7 @@ pub fn format_args_f_impl(input: IfmtInput) -> Result<TokenStream> {
     } = input;
 
     let s = format_literal.value();
-    let ref mut out_format_literal = String::with_capacity(s.len());
+    let out_format_literal = &mut String::with_capacity(s.len());
 
     let mut iterator = s.char_indices().peekable();
     while let Some((i, c)) = iterator.next() {
@@ -135,13 +129,13 @@ pub fn format_args_f_impl(input: IfmtInput) -> Result<TokenStream> {
     });
     format_literal = LitStr::new(out_format_literal, format_literal.span());
 
-    Ok(TokenStream::from(debug_output!(quote! {
+    Ok(quote! {
         format_args!(
             #format_literal
             #(, #positional_args)*
             #(, #named_args)*
         )
-    })))
+    })
 }
 
 #[allow(dead_code)] // dumb compiler does not see the struct being used...
@@ -199,7 +193,7 @@ where
     where
         Drop: FnOnce(T),
     {
-        fn drop(self: &'_ mut Self) {
+        fn drop(&'_ mut self) {
             use ::core::ptr;
             unsafe {
                 // # Safety
@@ -217,7 +211,7 @@ where
     {
         type Target = T;
         #[inline]
-        fn deref(self: &'_ Self) -> &'_ Self::Target {
+        fn deref(&'_ self) -> &'_ Self::Target {
             &self.0
         }
     }
@@ -226,7 +220,7 @@ where
         Drop: FnOnce(T),
     {
         #[inline]
-        fn deref_mut(self: &'_ mut Self) -> &'_ mut Self::Target {
+        fn deref_mut(&'_ mut self) -> &'_ mut Self::Target {
             &mut self.0
         }
     }

+ 1 - 52
packages/core-macro/src/rsx/ambiguous.rs

@@ -15,6 +15,7 @@ use syn::{
     Error, Ident, Result, Token,
 };
 
+#[allow(clippy::large_enum_variant)]
 pub enum AmbiguousElement<const AS: HtmlOrRsx> {
     Element(Element<AS>),
     Component(Component<AS>),
@@ -30,7 +31,6 @@ impl Parse for AmbiguousElement<AS_RSX> {
         }
 
         // If not an absolute path, then parse the ident and check if it's a valid tag
-
         if let Ok(pat) = input.fork().parse::<syn::Path>() {
             if pat.segments.len() > 1 {
                 return input
@@ -80,57 +80,6 @@ impl Parse for AmbiguousElement<AS_HTML> {
         } else {
             Err(Error::new(input.span(), "Not a valid Html tag"))
         }
-
-        // input.parse::<Token![>]>()?;
-
-        // let mut children = Vec::new();
-        // while !input.peek(Token![<]) {
-        //     children.push(input.parse::<BodyNode<AS_HTML>>()?);
-        // }
-
-        // Ok(AmbiguousElement::Element(Element {
-        //     name,
-        //     key: todo!(),
-        //     attributes: todo!(),
-        //     listeners: todo!(),
-        //     children,
-        //     _is_static: todo!(),
-        // }))
-
-        // // Try to parse as an absolute path and immediately defer to the componetn
-        // if input.peek(Token![::]) {
-        //     return input
-        //         .parse::<Component<AS_HTML>>()
-        //         .map(AmbiguousElement::Component);
-        // }
-
-        // // If not an absolute path, then parse the ident and check if it's a valid tag
-
-        // if let Ok(pat) = input.fork().parse::<syn::Path>() {
-        //     if pat.segments.len() > 1 {
-        //         return input
-        //             .parse::<Component<AS_HTML>>()
-        //             .map(AmbiguousElement::Component);
-        //     }
-        // }
-
-        // use syn::ext::IdentExt;
-        // if let Ok(name) = input.fork().call(Ident::parse_any) {
-        //     let name_str = name.to_string();
-
-        //     let first_char = name_str.chars().next().unwrap();
-        //     if first_char.is_ascii_uppercase() {
-        //         input
-        //             .parse::<Component<AS_HTML>>()
-        //             .map(AmbiguousElement::Component)
-        //     } else {
-        //         input
-        //             .parse::<Element<AS_HTML>>()
-        //             .map(AmbiguousElement::Element)
-        //     }
-        // } else {
-        //     Err(Error::new(input.span(), "Not a valid Html tag"))
-        // }
     }
 }
 

+ 3 - 15
packages/core-macro/src/rsx/component.rs

@@ -91,7 +91,7 @@ impl Parse for Component<AS_HTML> {
         stream.parse::<Token![>]>()?;
 
         'parsing: loop {
-            if stream.peek(Token![<]) {
+            if stream.peek(Token![<]) && stream.peek2(Token![/]) {
                 break 'parsing;
             }
 
@@ -115,18 +115,6 @@ impl Parse for Component<AS_HTML> {
         }
         stream.parse::<Token![>]>()?;
 
-        // // parse the guts
-        // let content: ParseBuffer;
-        // syn::braced!(content in stream);
-
-        // let cfg: BodyConfig<AS_HTML> = BodyConfig {
-        //     allow_children: true,
-        //     allow_fields: true,
-        //     allow_manual_props: true,
-        // };
-
-        // let (body, children, manual_props) = cfg.parse_component_body(&content)?;
-
         Ok(Self {
             name,
             body,
@@ -281,7 +269,7 @@ impl<const AS: HtmlOrRsx> ToTokens for Component<AS> {
                     let mut __manual_props = #manual_props;
                 };
                 for field in &self.body {
-                    if field.name.to_string() == "key" {
+                    if field.name == "key" {
                         has_key = Some(field);
                     } else {
                         let name = &field.name;
@@ -301,7 +289,7 @@ impl<const AS: HtmlOrRsx> ToTokens for Component<AS> {
             None => {
                 let mut toks = quote! { fc_to_builder(#name) };
                 for field in &self.body {
-                    if field.name.to_string() == "key" {
+                    if field.name == "key" {
                         has_key = Some(field);
                     } else {
                         toks.append_all(quote! {#field})

+ 5 - 5
packages/core-macro/src/rsx/element.rs

@@ -137,10 +137,11 @@ impl Parse for Element<AS_HTML> {
                 }
             };
         }
+
         stream.parse::<Token![>]>()?;
 
         'parsing: loop {
-            if stream.peek(Token![<]) {
+            if stream.peek(Token![<]) && stream.peek2(Token![/]) {
                 break 'parsing;
             }
 
@@ -155,6 +156,7 @@ impl Parse for Element<AS_HTML> {
         // closing element
         stream.parse::<Token![<]>()?;
         stream.parse::<Token![/]>()?;
+
         let close = Ident::parse_any(stream)?;
         if close != el_name {
             return Err(Error::new_spanned(
@@ -233,8 +235,6 @@ fn parse_rsx_element_field(
     // Return early if the field is a listener
     if name_str.starts_with("on") {
         // remove the "on" bit
-        // name = Ident::new(&name_str.trim_start_matches("on"), name.span());
-
         let ty = if stream.peek(token::Brace) {
             let content;
             syn::braced!(content in stream);
@@ -293,7 +293,7 @@ fn parse_rsx_element_field(
             return Ok(());
         }
         "classes" => {
-            todo!("custom class lsit not supported")
+            todo!("custom class list not supported")
         }
         "namespace" => {
             todo!("custom namespace not supported")
@@ -346,11 +346,11 @@ impl<const AS: HtmlOrRsx> ToTokens for ElementAttr<AS> {
             AttrType::BumpText(value) => tokens.append_all(quote! {
                 dioxus_elements::#el_name.#nameident(__cx, format_args_f!(#value))
             }),
+
             AttrType::FieldTokens(exp) => tokens.append_all(quote! {
                 dioxus_elements::#el_name.#nameident(__cx, #exp)
             }),
 
-            // todo: move event handlers on to the elements or onto the nodefactory
             AttrType::Event(event) => tokens.append_all(quote! {
                 dioxus::events::on::#nameident(__cx, #event)
             }),

+ 0 - 1
packages/core-macro/src/rsx/mod.rs

@@ -17,7 +17,6 @@ mod component;
 mod element;
 mod fragment;
 mod node;
-mod parse_rsx;
 
 // Re-export the namespaces into each other
 pub use ambiguous::*;

+ 0 - 2
packages/core-macro/src/rsx/parse_rsx.rs

@@ -1,2 +0,0 @@
-struct CallBody {}
-pub fn parse_callbody() {}

+ 18 - 10
packages/core/examples/syntax.rs

@@ -11,20 +11,28 @@ fn html_usage() {
     let r = html! {
         <div>
             "hello world"
+            <div>
+            </div>
+            <div />
             "hello world"
-            "hello world"
-            "hello world"
+            <Fragment>
+            </Fragment>
         </div>
     };
+    let r = rsx! {
+        div {
+            "hello world"
+        }
+    };
 }
 
 fn rsx_uage() {
-    let r = html! {
-        <Fragment>
-            "hello world"
-            "hello world"
-            "hello world"
-            "hello world"
-        </Fragment>
-    };
+    // let r = html! {
+    //     <Fragment>
+    //         "hello world"
+    //         "hello world"
+    //         "hello world"
+    //         "hello world"
+    //     </Fragment>
+    // };
 }