From c83f90c81aee5d49e6ae44854dade43d04443947 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Mon, 22 Dec 2025 09:04:34 +0000 Subject: [PATCH] fix(formatter): should expand call arguments group when the argument will break (#17254) Fix an incorrect output that was found in https://github.com/oxc-project/oxc/pull/17252#issue-3752523578 Biome has the same incorrect output and ended up fixing it by aligning the [Prettier implementation](https://github.com/prettier/prettier/blob/0273e33fc691e28e4ab3f3c8ee86918b65cf823d/src/language-js/print/call-arguments.js#L193-L195). --- .../oxc_formatter/src/write/call_arguments.rs | 25 ++++++++++++++++++- .../fixtures/ts/union/issue-16902.ts.snap | 12 ++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/crates/oxc_formatter/src/write/call_arguments.rs b/crates/oxc_formatter/src/write/call_arguments.rs index db7a760d13dae..e015133930765 100644 --- a/crates/oxc_formatter/src/write/call_arguments.rs +++ b/crates/oxc_formatter/src/write/call_arguments.rs @@ -139,7 +139,30 @@ impl<'a> Format<'a> for AstNode<'a, ArenaVec<'a, Argument<'a>>> { ] ); } else { - format_all_args_broken_out(self, false, f); + let interned = f.intern(&format_args!( + "(", + soft_block_indent(&format_with(move |f| { + let separator = format_with(|f| write!(f, [",", soft_line_break_or_space()])); + f.join_with(&separator).entries(self.iter()); + write!( + f, + [(!matches!(self.parent, AstNodes::ImportExpression(_))) + .then_some(FormatTrailingCommas::All)] + ); + })), + ")", + )); + + if let Some(element) = interned { + let should_expand = element.will_break(); + write!( + f, + group(&format_once(|f| { + f.write_element(element); + })) + .should_expand(should_expand) + ); + } } } } diff --git a/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap index 313d9e0b3d03f..cc2e40f2443f7 100644 --- a/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap +++ b/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap @@ -59,10 +59,12 @@ Math.random< string | number | undefined >0; -console.log(< +console.log( + < // comment string | number | undefined - >0); + >0, +); ------------------- { printWidth: 100 } @@ -89,9 +91,11 @@ Math.random< string | number | undefined >0; -console.log(< +console.log( + < // comment string | number | undefined - >0); + >0, +); ===================== End =====================