Skip to content

Commit f3ffebe

Browse files
committed
fix(formatter): should indent variable declarator if there is a trailing comment (#16243)
* close: #16193
1 parent 31d3186 commit f3ffebe

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

crates/oxc_formatter/src/formatter/separated.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::Deref;
2+
13
use oxc_span::{GetSpan, Span};
24

35
use crate::{
@@ -21,6 +23,14 @@ pub struct FormatSeparatedElement<E: GetSpan> {
2123
options: FormatSeparatedOptions,
2224
}
2325

26+
impl<T: GetSpan> Deref for FormatSeparatedElement<T> {
27+
type Target = T;
28+
29+
fn deref(&self) -> &Self::Target {
30+
&self.element
31+
}
32+
}
33+
2434
impl<T: GetSpan> GetSpan for FormatSeparatedElement<T> {
2535
fn span(&self) -> Span {
2636
self.element.span()

crates/oxc_formatter/src/write/variable_declaration.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, VariableDeclarator<'a>>> {
6969
let first_declarator = declarators.next().unwrap();
7070

7171
if length == 1 && !f.comments().has_comment_before(first_declarator.span().start) {
72-
return write!(f, first_declarator);
72+
return if first_declarator.init.is_none()
73+
&& f.comments()
74+
.has_comment_in_range(first_declarator.span.end, self.parent.span().end)
75+
{
76+
write!(f, indent(&first_declarator));
77+
} else {
78+
write!(f, &first_declarator);
79+
};
7380
}
7481

7582
write!(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const PAGE_PATH: string
2+
//<- THIS spaces
3+
;(()=>{})()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
declare const PAGE_PATH: string
6+
//<- THIS spaces
7+
;(()=>{})()
8+
9+
==================== Output ====================
10+
------------------
11+
{ printWidth: 80 }
12+
------------------
13+
declare const PAGE_PATH: string;
14+
//<- THIS spaces
15+
(() => {})();
16+
17+
-------------------
18+
{ printWidth: 100 }
19+
-------------------
20+
declare const PAGE_PATH: string;
21+
//<- THIS spaces
22+
(() => {})();
23+
24+
===================== End =====================

0 commit comments

Comments
 (0)