From c21b44a4e6d3ab4dc275e91a2d950ce3dd76f9af Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Thu, 15 Aug 2024 17:03:58 -0700 Subject: [PATCH] fix collapsing of multiline components and rsx!{} calls (#2849) --- packages/autofmt/src/lib.rs | 7 ++++++- packages/autofmt/src/prettier_please.rs | 5 +++-- packages/autofmt/src/writer.rs | 21 ++++++++++++++------- packages/autofmt/tests/samples.rs | 7 ++++--- packages/autofmt/tests/samples/collapse.rsx | 21 +++++++++++++++++++++ 5 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 packages/autofmt/tests/samples/collapse.rsx diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index f4fa85d338..2f5c0cba8e 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -118,7 +118,12 @@ pub fn try_fmt_file( let body_is_solo_expr = body.body.roots.len() == 1 && matches!(body.body.roots[0], BodyNode::RawExpr(_) | BodyNode::Text(_)); - if formatted.len() <= 80 && !formatted.contains('\n') && !body_is_solo_expr { + // If it's short, and it's not a single expression, and it's not empty, then we can collapse it + if formatted.len() <= 80 + && !formatted.contains('\n') + && !body_is_solo_expr + && !formatted.trim().is_empty() + { formatted = format!(" {formatted} "); } diff --git a/packages/autofmt/src/prettier_please.rs b/packages/autofmt/src/prettier_please.rs index 8dcc1b0582..cca0f0e5ca 100644 --- a/packages/autofmt/src/prettier_please.rs +++ b/packages/autofmt/src/prettier_please.rs @@ -85,11 +85,12 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String { // now we can replace the macros with the formatted blocks for fmted in replacer.formatted_stack.drain(..) { let is_multiline = fmted.contains('{'); + let is_empty = fmted.trim().is_empty(); let mut out_fmt = String::from("rsx! {"); if is_multiline { out_fmt.push('\n'); - } else { + } else if !is_empty { out_fmt.push(' '); } @@ -122,7 +123,7 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String { if is_multiline { out_fmt.push('\n'); out_fmt.push_str(&cfg.indent_str().repeat(whitespace)); - } else { + } else if !is_empty { out_fmt.push(' '); } diff --git a/packages/autofmt/src/writer.rs b/packages/autofmt/src/writer.rs index e60dfeb891..33b859d70a 100644 --- a/packages/autofmt/src/writer.rs +++ b/packages/autofmt/src/writer.rs @@ -836,13 +836,20 @@ impl<'a> Writer<'a> { } // TODO: let rawexprs to be inlined - [BodyNode::Component(ref comp)] if comp.fields.is_empty() => Some( - comp.name - .segments - .iter() - .map(|s| s.ident.to_string().len() + 2) - .sum::(), - ), + [BodyNode::Component(ref comp)] + // basically if the component is completely empty, we can inline it + if comp.fields.is_empty() + && comp.children.is_empty() + && comp.spreads.is_empty() => + { + Some( + comp.name + .segments + .iter() + .map(|s| s.ident.to_string().len() + 2) + .sum::(), + ) + } // Feedback on discord indicates folks don't like combining multiple children on the same line // We used to do a lot of math to figure out if we should expand out the line, but folks just diff --git a/packages/autofmt/tests/samples.rs b/packages/autofmt/tests/samples.rs index c386656145..2e7306fb97 100644 --- a/packages/autofmt/tests/samples.rs +++ b/packages/autofmt/tests/samples.rs @@ -6,8 +6,8 @@ macro_rules! twoway { // doc attrs $( #[doc = $doc:expr] )* - $name:ident - ),* + $name:ident, + )* ) => { $( $( #[doc = $doc] )* @@ -59,5 +59,6 @@ twoway![ trailing_expr, oneline, prop_rsx, - asset + asset, + collapse, ]; diff --git a/packages/autofmt/tests/samples/collapse.rsx b/packages/autofmt/tests/samples/collapse.rsx new file mode 100644 index 0000000000..7441930dbc --- /dev/null +++ b/packages/autofmt/tests/samples/collapse.rsx @@ -0,0 +1,21 @@ +// nesting pushes out +rsx! { + Fragment { + Fragment { + Fragment { + Fragment { + Fragment { + div { "Finally have a real node!" } + } + } + } + } + } +} + +// we don't make extra spaces +rsx! { + Component { blah: rsx! {} } +} + +rsx! {}