Skip to content

Commit 2a1de04

Browse files
committed
fix(formatter): correct printing comments for for statements
1 parent 8c13f0a commit 2a1de04

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ForStatement<'a>> {
659659

660660
impl<'a> FormatWrite<'a> for AstNode<'a, ForInStatement<'a>> {
661661
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
662-
let comments = f.context().comments().own_line_comments_before(self.body.span().start);
662+
let comments = f.context().comments().own_line_comments_before(self.right.span().start);
663663
write!(
664664
f,
665665
[
@@ -683,7 +683,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ForInStatement<'a>> {
683683

684684
impl<'a> FormatWrite<'a> for AstNode<'a, ForOfStatement<'a>> {
685685
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
686-
let comments = f.context().comments().own_line_comments_before(self.body.span().start);
686+
let comments = f.context().comments().own_line_comments_before(self.right.span().start);
687687

688688
let r#await = self.r#await();
689689
let left = self.left();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
for (let i in [
2+
// comment1
3+
1, 2, 3
4+
// comment2
5+
]);
6+
7+
for (let i of [
8+
// comment1
9+
1, 2, 3
10+
// comment2
11+
]);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
for (let i in [
6+
// comment1
7+
1, 2, 3
8+
// comment2
9+
]);
10+
11+
for (let i of [
12+
// comment1
13+
1, 2, 3
14+
// comment2
15+
]);
16+
==================== Output ====================
17+
for (let i in [
18+
// comment1
19+
1, 2,
20+
3,
21+
// comment2
22+
]);
23+
24+
for (let i of [
25+
// comment1
26+
1, 2,
27+
3,
28+
// comment2
29+
]);
30+
31+
===================== End =====================

0 commit comments

Comments
 (0)