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 collapsing of multiline components and rsx!{} calls #2849

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion packages/autofmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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} ");
}

Expand Down
5 changes: 3 additions & 2 deletions packages/autofmt/src/prettier_please.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}

Expand Down Expand Up @@ -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(' ');
}

Expand Down
21 changes: 14 additions & 7 deletions packages/autofmt/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>(),
),
[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::<usize>(),
)
}

// 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
Expand Down
7 changes: 4 additions & 3 deletions packages/autofmt/tests/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ macro_rules! twoway {

// doc attrs
$( #[doc = $doc:expr] )*
$name:ident
),*
$name:ident,
)*
) => {
$(
$( #[doc = $doc] )*
Expand Down Expand Up @@ -59,5 +59,6 @@ twoway![
trailing_expr,
oneline,
prop_rsx,
asset
asset,
collapse,
];
21 changes: 21 additions & 0 deletions packages/autofmt/tests/samples/collapse.rsx
Original file line number Diff line number Diff line change
@@ -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! {}
Loading