Fix empty for loop, if statements and children (#2275)

* fix empty for loop, if statements and children

* fix clippy
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-04-08 19:23:55 -07:00
1 parent 40b576894f
commit af524d979b
8 files changed
+27 -29

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter};
use super::*; use super::*;
use proc_macro2::{Span, TokenStream as TokenStream2}; use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt}; use quote::quote;
use syn::{parse_quote, spanned::Spanned, Expr, ExprIf, Ident, LitStr}; use syn::{parse_quote, spanned::Spanned, Expr, ExprIf, Ident, LitStr};
#[derive(PartialEq, Eq, Clone, Debug, Hash)] #[derive(PartialEq, Eq, Clone, Debug, Hash)]
+5 -8
View File
@@ -11,18 +11,15 @@
//! - [ ] Keys //! - [ ] Keys
//! - [ ] Properties spreading with with `..` syntax //! - [ ] Properties spreading with with `..` syntax
use self::{location::CallerLocation, renderer::TemplateRenderer}; use self::location::CallerLocation;
use super::*; use super::*;
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt}; use quote::quote;
use syn::{ use syn::{
ext::IdentExt, ext::IdentExt, parse::ParseBuffer, spanned::Spanned, token::Brace,
parse::{Parse, ParseBuffer, ParseStream}, AngleBracketedGenericArguments, Error, Expr, Ident, LitStr, PathArguments, Token,
spanned::Spanned,
token::Brace,
AngleBracketedGenericArguments, Error, Expr, Ident, LitStr, PathArguments, Result, Token,
}; };
#[derive(PartialEq, Eq, Clone, Debug, Hash)] #[derive(PartialEq, Eq, Clone, Debug, Hash)]
@@ -184,7 +181,7 @@ impl Component {
} }
if !self.children.is_empty() { if !self.children.is_empty() {
let renderer = TemplateRenderer::as_tokens(&self.children, None); let renderer = TemplateRenderer::as_tokens(&self.children, None);
toks.append_all(quote! { .children( Some({ #renderer }) ) }); toks.append_all(quote! { .children( { #renderer } ) });
} }
toks.append_all(quote! { .build() }); toks.append_all(quote! { .build() });
toks toks
+2 -2
View File
@@ -261,7 +261,7 @@ impl<'a> DynamicContext<'a> {
} }
// And then set the path of this node to the current path (which we're hitting during traversal) // And then set the path of this node to the current path (which we're hitting during traversal)
self.node_paths[idx] = self.current_path.clone(); self.node_paths[idx].clone_from(&self.current_path);
Some(match root { Some(match root {
BodyNode::Text(_) => TemplateNode::DynamicText { id: idx }, BodyNode::Text(_) => TemplateNode::DynamicText { id: idx },
@@ -322,7 +322,7 @@ impl<'a> DynamicContext<'a> {
self.attr_paths.resize_with(idx + 1, Vec::new); self.attr_paths.resize_with(idx + 1, Vec::new);
} }
self.attr_paths[idx] = self.current_path.clone(); self.attr_paths[idx].clone_from(&self.current_path);
Some(idx) Some(idx)
} }
+3 -6
View File
@@ -3,13 +3,10 @@ use std::fmt::{Display, Formatter};
use super::*; use super::*;
use proc_macro2::{Span, TokenStream as TokenStream2}; use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt}; use quote::quote;
use syn::{ use syn::{
parse::{Parse, ParseBuffer, ParseStream}, parse::ParseBuffer, punctuated::Punctuated, spanned::Spanned, token::Brace, Expr, Ident,
punctuated::Punctuated, LitStr, Token,
spanned::Spanned,
token::Brace,
Expr, Ident, LitStr, Result, Token,
}; };
// ======================================= // =======================================
+2 -2
View File
@@ -77,7 +77,7 @@ impl CallBody {
let body = TemplateRenderer::as_tokens(&self.roots, Some(location)); let body = TemplateRenderer::as_tokens(&self.roots, Some(location));
quote! { Some({ #body }) } quote! { { #body } }
} }
/// This will try to create a new template from the current body and the previous body. This will return None if the /// This will try to create a new template from the current body and the previous body. This will return None if the
@@ -172,7 +172,7 @@ impl ToTokens for CallBody {
true => out_tokens.append_all(quote! { None }), true => out_tokens.append_all(quote! { None }),
false => { false => {
let body = TemplateRenderer::as_tokens(&self.roots, None); let body = TemplateRenderer::as_tokens(&self.roots, None);
out_tokens.append_all(quote! { Some({ #body }) }) out_tokens.append_all(quote! { { #body } })
} }
} }
} }
+4 -5
View File
@@ -3,13 +3,12 @@ use self::location::CallerLocation;
use super::*; use super::*;
use proc_macro2::{Span, TokenStream as TokenStream2}; use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt}; use quote::quote;
use syn::{ use syn::{
braced, braced,
parse::{Parse, ParseStream},
spanned::Spanned, spanned::Spanned,
token::{self, Brace}, token::{self, Brace},
Expr, ExprIf, LitStr, Pat, Result, Expr, ExprIf, LitStr, Pat,
}; };
/* /*
@@ -298,14 +297,14 @@ impl ToTokens for IfChain {
let renderer = TemplateRenderer::as_tokens(then_branch, None); let renderer = TemplateRenderer::as_tokens(then_branch, None);
body.append_all(quote! { #if_token #cond { Some({#renderer}) } }); body.append_all(quote! { #if_token #cond { {#renderer} } });
if let Some(next) = else_if_branch { if let Some(next) = else_if_branch {
body.append_all(quote! { else }); body.append_all(quote! { else });
elif = Some(next); elif = Some(next);
} else if let Some(else_branch) = else_branch { } else if let Some(else_branch) = else_branch {
let renderer = TemplateRenderer::as_tokens(else_branch, None); let renderer = TemplateRenderer::as_tokens(else_branch, None);
body.append_all(quote! { else { Some({#renderer}) } }); body.append_all(quote! { else { {#renderer} } });
terminated = true; terminated = true;
break; break;
} else { } else {
+9 -2
View File
@@ -16,6 +16,11 @@ impl<'a> TemplateRenderer<'a> {
} }
fn render(mut self) -> TokenStream2 { fn render(mut self) -> TokenStream2 {
// If there are no roots, this is an empty template, so just return None
if self.roots.is_empty() {
return quote! { Option::<dioxus_core::VNode>::None };
}
// Create a new dynamic context that tracks the state of all the dynamic nodes // Create a new dynamic context that tracks the state of all the dynamic nodes
// We have no old template, to seed it with, so it'll just be used for rendering // We have no old template, to seed it with, so it'll just be used for rendering
let mut context = DynamicContext::default(); let mut context = DynamicContext::default();
@@ -43,7 +48,7 @@ impl<'a> TemplateRenderer<'a> {
let node_paths = context.node_paths.iter().map(|it| quote!(&[#(#it),*])); let node_paths = context.node_paths.iter().map(|it| quote!(&[#(#it),*]));
let attr_paths = context.attr_paths.iter().map(|it| quote!(&[#(#it),*])); let attr_paths = context.attr_paths.iter().map(|it| quote!(&[#(#it),*]));
quote! { let vnode = quote! {
static TEMPLATE: dioxus_core::Template = dioxus_core::Template { static TEMPLATE: dioxus_core::Template = dioxus_core::Template {
name: #name, name: #name,
roots: #roots, roots: #roots,
@@ -61,7 +66,9 @@ impl<'a> TemplateRenderer<'a> {
); );
__vnodes __vnodes
} }
} };
quote! { Some({ #vnode }) }
} }
fn get_template_id_tokens(&self) -> TokenStream2 { fn get_template_id_tokens(&self) -> TokenStream2 {
+1 -3
View File
@@ -3,7 +3,7 @@
use futures_channel::mpsc::UnboundedReceiver; use futures_channel::mpsc::UnboundedReceiver;
use dioxus_core::Template; use dioxus_core::Template;
use web_sys::{console, Element}; use web_sys::{Element};
pub(crate) fn init() -> UnboundedReceiver<Template> { pub(crate) fn init() -> UnboundedReceiver<Template> {
use wasm_bindgen::closure::Closure; use wasm_bindgen::closure::Closure;
@@ -30,8 +30,6 @@ pub(crate) fn init() -> UnboundedReceiver<Template> {
// change the rsx when new data is received // change the rsx when new data is received
let cl = Closure::wrap(Box::new(move |e: MessageEvent| { let cl = Closure::wrap(Box::new(move |e: MessageEvent| {
console::log_1(&e.clone().into());
if let Ok(text) = e.data().dyn_into::<js_sys::JsString>() { if let Ok(text) = e.data().dyn_into::<js_sys::JsString>() {
let string: String = text.into(); let string: String = text.into();