Skip to content

Commit 8c67314

Browse files
committed
fix(formatter): improve handling of dangling comments in if statements
1 parent 6039089 commit 8c67314

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,14 +836,17 @@ impl<'a> FormatWrite<'a> for AstNode<'a, IfStatement<'a>> {
836836
}
837837

838838
if has_dangling_comments {
839-
write!(
840-
f,
841-
FormatDanglingComments::Comments { comments, indent: DanglingIndentMode::None }
842-
)?;
843-
844839
if has_line_comment {
840+
write!(f, FormatTrailingComments::Comments(comments))?;
845841
write!(f, hard_line_break())?;
846842
} else {
843+
write!(
844+
f,
845+
FormatDanglingComments::Comments {
846+
comments,
847+
indent: DanglingIndentMode::None
848+
}
849+
)?;
847850
write!(f, space())?;
848851
}
849852
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (true) {}
2+
3+
// comment1
4+
else if (false) {}
5+
6+
// comment2
7+
8+
else {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
if (true) {}
6+
7+
// comment1
8+
else if (false) {}
9+
10+
// comment2
11+
12+
else {}
13+
==================== Output ====================
14+
if (true) {
15+
}
16+
17+
// comment1
18+
else if (false) {
19+
}
20+
21+
// comment2
22+
else {
23+
}
24+
25+
===================== End =====================

0 commit comments

Comments
 (0)