Skip to content

Commit 0ca8154

Browse files
committed
fix(formatter): incorrect printing of trailing comments of callee when the call arguments are empty (#16232)
* close: #16125
1 parent f08411e commit 0ca8154

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> {
227227
MemberChain::from_call_expression(self, f).fmt(f);
228228
} else {
229229
let format_inner = format_with(|f| {
230-
if self.type_arguments.is_some() {
230+
// Preserve trailing comments of the callee in the following cases:
231+
// `call /**/()`
232+
// `call /**/<T>()`
233+
if self.type_arguments.is_some() || self.arguments.is_empty() {
231234
write!(f, [callee]);
232235
} else {
233236
write!(f, [FormatNodeWithoutTrailingComments(callee)]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
call/* C1 */()
2+
call/* C2 */?.()
3+
4+
call // C3
5+
()
6+
call // C4
7+
?.()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
call/* C1 */()
6+
call/* C2 */?.()
7+
8+
call // C3
9+
()
10+
call // C4
11+
?.()
12+
13+
==================== Output ====================
14+
------------------
15+
{ printWidth: 80 }
16+
------------------
17+
call /* C1 */();
18+
call /* C2 */?.();
19+
20+
call(); // C3
21+
call?.(); // C4
22+
23+
-------------------
24+
{ printWidth: 100 }
25+
-------------------
26+
call /* C1 */();
27+
call /* C2 */?.();
28+
29+
call(); // C3
30+
call?.(); // C4
31+
32+
===================== End =====================

0 commit comments

Comments
 (0)