Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: clippy, dont throw error on commas #2869

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/suspense/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
jkelleyrtp marked this conversation as resolved.
Show resolved Hide resolved
scope_state.last_rendered_node = Some(children);
})
}
Expand Down
42 changes: 21 additions & 21 deletions packages/rsx/src/ifmt.rs
Original file line number Diff line number Diff line change
@@ -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},
*,
Expand All @@ -25,9 +25,13 @@ 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<Self> {
let segments = match source.to_token_stream().to_string().starts_with("r#") {
true => vec![Segment::Literal(source.value())],
false => IfmtInput::from_raw_escaped(&source.value())?,
};

Ok(Self { segments, source })
}

pub fn span(&self) -> Span {
Expand Down Expand Up @@ -135,10 +139,11 @@ impl IfmtInput {
}

/// Parse the source into segments
fn from_raw(input: &str) -> Result<Vec<Segment>> {
fn from_raw_escaped(input: &str) -> Result<Vec<Segment>> {
let mut chars = input.chars().peekable();
let mut segments = Vec::new();
let mut current_literal = String::new();

while let Some(c) = chars.next() {
if c == '{' {
if let Some(c) = chars.next_if(|c| *c == '{') {
Expand Down Expand Up @@ -335,23 +340,10 @@ impl ToTokens for FormattedSegmentType {
}
}

impl FromStr for IfmtInput {
type Err = syn::Error;

fn from_str(input: &str) -> Result<Self> {
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<Self> {
let source: LitStr = input.parse()?;
let segments = IfmtInput::from_raw(&source.value())?;
Ok(Self { source, segments })
Self::new_litstr(source)
}
}

Expand All @@ -370,7 +362,7 @@ mod tests {

#[test]
fn segments_parse() {
let input = "blah {abc} {def}".parse::<IfmtInput>().unwrap();
let input: IfmtInput = parse_quote! { "blah {abc} {def}" };
assert_eq!(
input.segments,
vec![
Expand Down Expand Up @@ -399,10 +391,11 @@ mod tests {
let input = syn::parse2::<IfmtInput>(quote! { "hello {world} {world} {world()}" }).unwrap();
println!("{}", input.to_string_with_quotes());

// raw expressions are always static!
let input =
syn::parse2::<IfmtInput>(quote! { r#"hello {world} {world} {world()}"# }).unwrap();
println!("{}", input.to_string_with_quotes());
assert!(!input.is_static());
assert!(input.is_static());
ealmloff marked this conversation as resolved.
Show resolved Hide resolved

let input = syn::parse2::<IfmtInput>(quote! { r#"hello"# }).unwrap();
println!("{}", input.to_string_with_quotes());
Expand All @@ -417,4 +410,11 @@ mod tests {
Some("body { background: red; }".to_string())
);
}

#[test]
fn raw_blocks_not_formatted() {
let input = syn::parse2::<IfmtInput>(quote! { r#"hello {}"# }).unwrap();
println!("{}", input.to_string_with_quotes());
assert_eq!(input.to_string_with_quotes(), "r#\"hello {}\"#".to_string());
}
}
2 changes: 1 addition & 1 deletion packages/rsx/src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
28 changes: 21 additions & 7 deletions packages/rsx/src/rsx_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BodyNode> {
let body_node = content.parse::<BodyNode>()?;
if !content.is_empty() && content.peek(Token![,]) {
let comma = content.parse::<Token![,]>()?;
diagnostics.push(
comma
.span()
.warning("Elements and text nodes do not need to be separated by commas."),
);
let _comma = content.parse::<Token![,]>()?;

// 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)
}
Expand Down
Loading