diff --git a/packages/autofmt/src/prettier_please.rs b/packages/autofmt/src/prettier_please.rs index 0e0228325..2b24c9dbf 100644 --- a/packages/autofmt/src/prettier_please.rs +++ b/packages/autofmt/src/prettier_please.rs @@ -55,7 +55,14 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String { if multiline || formatted.contains('\n') { formatted = formatted .lines() - .map(|line| format!("{}{line}", self.cfg.indent_str())) + .map(|line| { + // Don't add indentation to blank lines (avoid trailing whitespace) + if line.is_empty() { + String::new() + } else { + format!("{}{line}", self.cfg.indent_str()) + } + }) .collect::>() .join("\n"); } @@ -106,8 +113,8 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String { let mut lines = fmted.lines().enumerate().peekable(); while let Some((_idx, fmt_line)) = lines.next() { - // Push the indentation - if is_multiline { + // Push the indentation (but not for blank lines - avoid trailing whitespace) + if is_multiline && !fmt_line.is_empty() { out_fmt.push_str(&cfg.indent_str().repeat(whitespace)); } diff --git a/packages/autofmt/tests/samples.rs b/packages/autofmt/tests/samples.rs index 9e05c1aa8..b2ec30a75 100644 --- a/packages/autofmt/tests/samples.rs +++ b/packages/autofmt/tests/samples.rs @@ -62,4 +62,6 @@ twoway![ asset, collapse, expr_on_conditional, + blank_lines, + blank_lines_preserved, ]; diff --git a/packages/autofmt/tests/samples/blank_lines.rsx b/packages/autofmt/tests/samples/blank_lines.rsx new file mode 100644 index 000000000..816e8a859 --- /dev/null +++ b/packages/autofmt/tests/samples/blank_lines.rsx @@ -0,0 +1,16 @@ +rsx! { + div { + match true { + true => rsx! { + if true { + span { "a" } + } + if true { + span { "b" } + } + span { "c" } + }, + false => rsx! {}, + } + } +} diff --git a/packages/autofmt/tests/samples/blank_lines_preserved.rsx b/packages/autofmt/tests/samples/blank_lines_preserved.rsx new file mode 100644 index 000000000..c35e64bb6 --- /dev/null +++ b/packages/autofmt/tests/samples/blank_lines_preserved.rsx @@ -0,0 +1,9 @@ +rsx! { + div { + span { "a" } + + span { "b" } + + span { "c" } + } +}