diff --git a/crates/oxc_formatter/src/write/mod.rs b/crates/oxc_formatter/src/write/mod.rs index 623d939558dc6..de5eb5cdae67f 100644 --- a/crates/oxc_formatter/src/write/mod.rs +++ b/crates/oxc_formatter/src/write/mod.rs @@ -227,7 +227,10 @@ impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> { MemberChain::from_call_expression(self, f).fmt(f); } else { let format_inner = format_with(|f| { - if self.type_arguments.is_some() { + // Preserve trailing comments of the callee in the following cases: + // `call /**/()` + // `call /**/()` + if self.type_arguments.is_some() || self.arguments.is_empty() { write!(f, [callee]); } else { write!(f, [FormatNodeWithoutTrailingComments(callee)]); diff --git a/crates/oxc_formatter/tests/fixtures/js/calls/issue-16125.js b/crates/oxc_formatter/tests/fixtures/js/calls/issue-16125.js new file mode 100644 index 0000000000000..15f2e381bcf44 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/calls/issue-16125.js @@ -0,0 +1,7 @@ +call/* C1 */() +call/* C2 */?.() + +call // C3 +() +call // C4 +?.() diff --git a/crates/oxc_formatter/tests/fixtures/js/calls/issue-16125.js.snap b/crates/oxc_formatter/tests/fixtures/js/calls/issue-16125.js.snap new file mode 100644 index 0000000000000..7697ae28ef6e7 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/calls/issue-16125.js.snap @@ -0,0 +1,32 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +call/* C1 */() +call/* C2 */?.() + +call // C3 +() +call // C4 +?.() + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +call /* C1 */(); +call /* C2 */?.(); + +call(); // C3 +call?.(); // C4 + +------------------- +{ printWidth: 100 } +------------------- +call /* C1 */(); +call /* C2 */?.(); + +call(); // C3 +call?.(); // C4 + +===================== End =====================