Skip to content

Commit bd9da7d

Browse files
committed
fix(formatter): print a line break incorrectly for directives
1 parent c1a7e3d commit bd9da7d

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

crates/oxc_formatter/src/write/program.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, Directive<'a>>> {
113113
//```
114114
// so we should keep an extra empty line after JsDirectiveList
115115

116-
let need_extra_empty_line = f.source_text().lines_after(last_directive.span.end) > 1;
116+
let end = if let Some(last_printed_comment) = f.comments().printed_comments().last()
117+
&& last_printed_comment.span.end > last_directive.span.end
118+
{
119+
last_printed_comment.span.end
120+
} else {
121+
last_directive.span.end
122+
};
123+
124+
let need_extra_empty_line = f.source_text().lines_after(end) > 1;
117125
write!(f, if need_extra_empty_line { empty_line() } else { hard_line_break() })
118126
}
119127
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
3+
// comment
4+
(() => {});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
"use strict";
6+
7+
// comment
8+
(() => {});
9+
==================== Output ====================
10+
"use strict";
11+
12+
// comment
13+
() => {};
14+
15+
===================== End =====================

0 commit comments

Comments
 (0)