From 005d52468d00bdd2df3ab6ca6f1ae81b96e5c47d Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 20 Aug 2024 16:57:56 -0700 Subject: [PATCH] Fix: clippy, dont throw error on commas (#2869) * Fix: clippy, hotreload css blocks, raw blocks * fix test * okay nevermind --- packages/core/src/suspense/component.rs | 4 ++-- packages/rsx/src/ifmt.rs | 25 ++++++---------------- packages/rsx/src/literal.rs | 2 +- packages/rsx/src/rsx_block.rs | 28 ++++++++++++++++++------- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/packages/core/src/suspense/component.rs b/packages/core/src/suspense/component.rs index 1b6ea90c5..6af8f05bd 100644 --- a/packages/core/src/suspense/component.rs +++ b/packages/core/src/suspense/component.rs @@ -316,7 +316,7 @@ impl SuspenseBoundaryProps { // Store the (now mounted) children back into the scope state let scope_state = &mut dom.scopes[scope_id.0]; let props = Self::downcast_from_props(&mut *scope_state.props).unwrap(); - props.children = children.clone(); + props.children.clone_from(&children); let scope_state = &mut dom.scopes[scope_id.0]; let suspense_context = scope_state @@ -435,7 +435,7 @@ impl SuspenseBoundaryProps { // Store the (now mounted) children back into the scope state let scope_state = &mut dom.scopes[scope_id.0]; let props = Self::downcast_from_props(&mut *scope_state.props).unwrap(); - props.children = children.clone(); + props.children.clone_from(&children); scope_state.last_rendered_node = Some(children); }) } diff --git a/packages/rsx/src/ifmt.rs b/packages/rsx/src/ifmt.rs index 13cf32eb6..7f6822838 100644 --- a/packages/rsx/src/ifmt.rs +++ b/packages/rsx/src/ifmt.rs @@ -1,6 +1,6 @@ use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned, ToTokens, TokenStreamExt}; -use std::{collections::HashMap, str::FromStr}; +use std::collections::HashMap; use syn::{ parse::{Parse, ParseStream}, *, @@ -25,9 +25,9 @@ impl IfmtInput { } } - pub fn new_litstr(source: LitStr) -> Self { - let segments = Self::from_raw(&source.value()).unwrap(); - Self { segments, source } + pub fn new_litstr(source: LitStr) -> Result { + let segments = IfmtInput::from_raw(&source.value())?; + Ok(Self { segments, source }) } pub fn span(&self) -> Span { @@ -335,23 +335,10 @@ impl ToTokens for FormattedSegmentType { } } -impl FromStr for IfmtInput { - type Err = syn::Error; - - fn from_str(input: &str) -> Result { - let segments = IfmtInput::from_raw(input)?; - Ok(Self { - source: LitStr::new(input, Span::call_site()), - segments, - }) - } -} - impl Parse for IfmtInput { fn parse(input: ParseStream) -> Result { let source: LitStr = input.parse()?; - let segments = IfmtInput::from_raw(&source.value())?; - Ok(Self { source, segments }) + Self::new_litstr(source) } } @@ -370,7 +357,7 @@ mod tests { #[test] fn segments_parse() { - let input = "blah {abc} {def}".parse::().unwrap(); + let input: IfmtInput = parse_quote! { "blah {abc} {def}" }; assert_eq!( input.segments, vec![ diff --git a/packages/rsx/src/literal.rs b/packages/rsx/src/literal.rs index 3ff64e5e8..e59440013 100644 --- a/packages/rsx/src/literal.rs +++ b/packages/rsx/src/literal.rs @@ -65,7 +65,7 @@ impl Parse for HotLiteral { Lit::Int(a) => HotLiteral::Int(a), Lit::Bool(a) => HotLiteral::Bool(a), Lit::Float(a) => HotLiteral::Float(a), - Lit::Str(a) => HotLiteral::Fmted(IfmtInput::new_litstr(a).into()), + Lit::Str(a) => HotLiteral::Fmted(IfmtInput::new_litstr(a)?.into()), _ => { return Err(syn::Error::new( raw.span(), diff --git a/packages/rsx/src/rsx_block.rs b/packages/rsx/src/rsx_block.rs index 0f4b1f39b..6a593900f 100644 --- a/packages/rsx/src/rsx_block.rs +++ b/packages/rsx/src/rsx_block.rs @@ -224,16 +224,30 @@ impl RsxBlock { // Parse a body node with diagnostics for unnecessary trailing commas fn parse_body_node_with_comma_diagnostics( content: &ParseBuffer, - diagnostics: &mut Diagnostics, + _diagnostics: &mut Diagnostics, ) -> syn::Result { let body_node = content.parse::()?; if !content.is_empty() && content.peek(Token![,]) { - let comma = content.parse::()?; - diagnostics.push( - comma - .span() - .warning("Elements and text nodes do not need to be separated by commas."), - ); + let _comma = content.parse::()?; + + // todo: we would've pushed a warning here but proc-macro-2 emits them as errors, which we + // dont' want. There's no built-in cfg way for checking if we're on nightly, and adding + // that would require a build script, so for the interest of compile times, we won't throw + // any warning at all. + // + // Whenever the user formats their code with `dx fmt`, the comma will be removed, so active + // projects will implicitly be fixed. + // + // Whenever the issue is resolved or diagnostics are added, we can re-add this warning. + // + // - https://github.com/SergioBenitez/proc-macro2-diagnostics/issues/9 + // - https://github.com/DioxusLabs/dioxus/issues/2807 + // + // diagnostics.push( + // comma + // .span() + // .warning("Elements and text nodes do not need to be separated by commas."), + // ); } Ok(body_node) }