From 182882b9cb806e3e0c71de08f6853827b76027bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 2 Jun 2026 09:36:38 +0200 Subject: [PATCH] fix: stable formatting for conditional expression as if/else body A single-statement if/else body that is a conditional (ternary) expression could format differently on each run: when the inline form exceeds the line width, braces are added and the body moves to its own indented line, where the ternary then fits on one line, but it was being wrapped based on the pre-brace column. This left the formatter producing different output on the next run ("Formatting not stable"). Wrap the single non-block statement body in a new_line_group so its line-break decisions are re-measured at the body's final indentation, matching how binary and call expressions already behave (which were already stable). --- src/generation/generate.rs | 4 ++-- ...Deno_ConditionalExpressionStatementBody.txt | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/specs/deno/Deno_ConditionalExpressionStatementBody.txt diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 7fb3a332f..0cc27b50c 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -8854,13 +8854,13 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont items })); } else { - items.extend(ir_helpers::with_indent({ + items.extend(ir_helpers::with_indent(ir_helpers::new_line_group({ let mut items = PrintItems::new(); let body_node_range = opts.body_node.range(); items.extend(gen_node(opts.body_node, context)); items.extend(gen_trailing_comments(&body_node_range, context)); items - })); + }))); } items.push_line_and_column(end_statements_lc); diff --git a/tests/specs/deno/Deno_ConditionalExpressionStatementBody.txt b/tests/specs/deno/Deno_ConditionalExpressionStatementBody.txt new file mode 100644 index 000000000..cb9a27eb0 --- /dev/null +++ b/tests/specs/deno/Deno_ConditionalExpressionStatementBody.txt @@ -0,0 +1,18 @@ +~~ deno: true ~~ +== should keep a conditional expression body stable when braces are added (not re-wrap the ternary) == +function w(n) { + if ("play" === n) { + var w = 1; + play({ ids: [b] }); + } else if ("queue" === n) playing() ? queue({ ids: [b] }) : queue({ ids: [b] }); +} + +[expect] +function w(n) { + if ("play" === n) { + var w = 1; + play({ ids: [b] }); + } else if ("queue" === n) { + playing() ? queue({ ids: [b] }) : queue({ ids: [b] }); + } +}