Fix: #2604, Fix: #2240, Fix: #2341, Fix #1355 - Better error handling and and spaces handling in autofmt (#2736)

* add new autofmt sample
* Feat: implement rustfmt::skip support for rsx
* generally improve error handling with better expect messages
* wip: nested rsx formatting and expression formatting
* nested rsx formatting works
* collapse autofmt crate
* cast indent through macros
* use proper whitespace
* no more eating comments!
* Use proper error handling
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-07-30 18:36:13 -07:00
1 parent ef0202f999
commit 828cc502f1
47 files changed
+1621 -1055

No files matched your search

+3 -3
View File
@@ -34,7 +34,7 @@ pub struct Component {
pub fields: Vec<Attribute>,
pub component_literal_dyn_idx: Vec<DynIdx>,
pub spreads: Vec<Spread>,
pub brace: token::Brace,
pub brace: Option<token::Brace>,
pub children: TemplateBody,
pub dyn_idx: DynIdx,
pub diagnostics: Diagnostics,
@@ -69,8 +69,8 @@ impl Parse for Component {
name,
generics,
fields,
brace: Some(brace),
component_literal_dyn_idx,
brace,
spreads,
diagnostics,
};
@@ -308,7 +308,7 @@ impl Component {
Component {
name,
generics,
brace: token::Brace::default(),
brace: None,
fields: vec![],
spreads: vec![],
children: TemplateBody::new(vec![]),
+4 -2
View File
@@ -1,7 +1,7 @@
use super::*;
use location::DynIdx;
use proc_macro2::TokenStream as TokenStream2;
use syn::{braced, Expr, Pat};
use syn::{braced, token::Brace, Expr, Pat};
#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug)]
@@ -10,6 +10,7 @@ pub struct ForLoop {
pub pat: Pat,
pub in_token: Token![in],
pub expr: Box<Expr>,
pub brace: Brace,
pub body: TemplateBody,
pub dyn_idx: DynIdx,
}
@@ -24,13 +25,14 @@ impl Parse for ForLoop {
let expr = input.call(Expr::parse_without_eager_brace)?;
let content;
let _brace = braced!(content in input);
let brace = braced!(content in input);
let body = content.parse()?;
Ok(Self {
for_token,
pat,
in_token,
brace,
expr: Box::new(expr),
body,
dyn_idx: DynIdx::default(),
+8 -2
View File
@@ -4,6 +4,7 @@ use quote::quote;
use quote::{ToTokens, TokenStreamExt};
use syn::{
parse::{Parse, ParseStream},
token::Brace,
Expr, Result, Token,
};
@@ -14,8 +15,10 @@ use crate::TemplateBody;
pub struct IfChain {
pub if_token: Token![if],
pub cond: Box<Expr>,
pub then_brace: Brace,
pub then_branch: TemplateBody,
pub else_if_branch: Option<Box<IfChain>>,
pub else_brace: Option<Brace>,
pub else_branch: Option<TemplateBody>,
pub dyn_idx: DynIdx,
}
@@ -42,10 +45,11 @@ impl Parse for IfChain {
let cond = Box::new(input.call(Expr::parse_without_eager_brace)?);
let content;
syn::braced!(content in input);
let then_brace = syn::braced!(content in input);
let then_branch = content.parse()?;
let mut else_brace = None;
let mut else_branch = None;
let mut else_if_branch = None;
@@ -56,7 +60,7 @@ impl Parse for IfChain {
else_if_branch = Some(Box::new(input.parse::<IfChain>()?));
} else {
let content;
syn::braced!(content in input);
else_brace = Some(syn::braced!(content in input));
else_branch = Some(content.parse()?);
}
}
@@ -67,6 +71,8 @@ impl Parse for IfChain {
then_branch,
else_if_branch,
else_branch,
then_brace,
else_brace,
dyn_idx: DynIdx::default(),
})
}
+1 -7
View File
@@ -17,12 +17,6 @@ pub struct IfmtInput {
pub segments: Vec<Segment>,
}
impl Default for IfmtInput {
fn default() -> Self {
Self::new(Span::call_site())
}
}
impl IfmtInput {
pub fn new(span: Span) -> Self {
Self {
@@ -213,7 +207,7 @@ impl ToTokens for IfmtInput {
fn to_tokens(&self, tokens: &mut TokenStream) {
// If the input is a string literal, we can just return it
if let Some(static_str) = self.to_static() {
return static_str.to_tokens(tokens);
return quote_spanned! { self.span() => #static_str }.to_tokens(tokens);
}
// Try to turn it into a single _.to_string() call
+8
View File
@@ -151,6 +151,14 @@ pub struct HotReloadFormattedSegment {
pub dynamic_node_indexes: Vec<DynIdx>,
}
impl HotReloadFormattedSegment {
/// This method is very important!
/// Deref + Spanned + .span() methods leads to name collisions
pub fn span(&self) -> Span {
self.formatted_input.span()
}
}
impl Deref for HotReloadFormattedSegment {
type Target = IfmtInput;