Skip to content

Commit 31595c3

Browse files
committed
fix(formatter): correct printing comments for assignment pattern (#14469)
1 parent bb43dc5 commit 31595c3

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,17 +857,14 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BindingPattern<'a>> {
857857

858858
impl<'a> FormatWrite<'a> for AstNode<'a, AssignmentPattern<'a>> {
859859
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
860+
let mut left = self.left().memoized();
861+
// Format `left` early before writing leading comments, so that comments
862+
// inside `left` are not treated as leading comments of `= right`
863+
left.inspect(f)?;
860864
let comments = f.context().comments().own_line_comments_before(self.right.span().start);
861865
write!(
862866
f,
863-
[
864-
FormatLeadingComments::Comments(comments),
865-
self.left(),
866-
space(),
867-
"=",
868-
space(),
869-
self.right(),
870-
]
867+
[FormatLeadingComments::Comments(comments), left, space(), "=", space(), self.right()]
871868
)
872869
}
873870
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
({
2+
// comment1 inside the left side of the assignment pattern
3+
random = Math.random,
4+
// comment2 inside the left side of the assignment pattern
5+
sqrt = Math.sqrt,
6+
} = {}) => {};
7+
8+
([
9+
// comment1 inside the left side of the assignment pattern
10+
random = Math.random,
11+
// comment2 inside the left side of the assignment pattern
12+
sqrt = Math.sqrt,
13+
] = []) => {};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
({
6+
// comment1 inside the left side of the assignment pattern
7+
random = Math.random,
8+
// comment2 inside the left side of the assignment pattern
9+
sqrt = Math.sqrt,
10+
} = {}) => {};
11+
12+
([
13+
// comment1 inside the left side of the assignment pattern
14+
random = Math.random,
15+
// comment2 inside the left side of the assignment pattern
16+
sqrt = Math.sqrt,
17+
] = []) => {};
18+
19+
==================== Output ====================
20+
({
21+
// comment1 inside the left side of the assignment pattern
22+
random = Math.random,
23+
// comment2 inside the left side of the assignment pattern
24+
sqrt = Math.sqrt,
25+
} = {}) => {};
26+
27+
([
28+
// comment1 inside the left side of the assignment pattern
29+
random = Math.random,
30+
// comment2 inside the left side of the assignment pattern
31+
sqrt = Math.sqrt,
32+
] = []) => {};
33+
34+
===================== End =====================

crates/oxc_formatter/tests/fixtures/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ fn generate_snapshot(path: &Path, source_text: &str) -> String {
198198
}
199199

200200
/// Helper function to run a test for a single file
201-
#[expect(unused)]
202201
fn test_file(path: &Path) {
203202
let source_text = fs::read_to_string(path).unwrap();
204203
let snapshot = generate_snapshot(path, &source_text);

0 commit comments

Comments
 (0)