From 3a60140dff06ecf6d55c21c9da5f00f5f5f4ff7e Mon Sep 17 00:00:00 2001 From: hideselfview Date: Fri, 16 Jan 2026 18:47:47 -0500 Subject: [PATCH] fix: prevent dx fmt from adding trailing whitespace to blank lines (#5224) This fixes a conflict between dx fmt and cargo fmt (rustfmt). Previously, dx fmt would add indentation whitespace to blank lines inside RSX blocks, but cargo fmt removes trailing whitespace from all lines. This made it impossible for code to satisfy both formatters. The fix skips adding indentation to empty lines in two places: 1. When indenting formatted RSX blocks inside expressions 2. When reassembling the final formatted output Fixes #5223 Co-authored-by: Dmitry Minkovsky --- packages/autofmt/src/prettier_please.rs | 13 ++++++++++--- packages/autofmt/tests/samples.rs | 2 ++ packages/autofmt/tests/samples/blank_lines.rsx | 16 ++++++++++++++++ .../tests/samples/blank_lines_preserved.rsx | 9 +++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 packages/autofmt/tests/samples/blank_lines.rsx create mode 100644 packages/autofmt/tests/samples/blank_lines_preserved.rsx 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" } + } +}