Skip to content

Commit 64b8226

Browse files
committed
fix(formatter): corrct printing leading own line comments before method body (#14886)
There is a failed case, probably due to the Prettier bug
1 parent 6ce1162 commit 64b8226

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

crates/oxc_formatter/src/write/class.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
121121
)?;
122122

123123
if let Some(body) = &value.body() {
124-
// Handle block comments between method signature and body
125-
// Example: method() /* comment */ {}
126-
let comments = f.context().comments().comments_before(body.span.start);
127-
if !comments.is_empty() {
128-
write!(f, [space(), FormatTrailingComments::Comments(comments)])?;
129-
}
130-
write!(f, [space(), body])?;
124+
write!(f, body)?;
131125
}
132126
if self.r#type().is_abstract()
133127
|| matches!(value.r#type, FunctionType::TSEmptyBodyFunctionExpression)
@@ -641,7 +635,7 @@ pub fn format_grouped_parameters_with_return_type<'a>(
641635
group(&format_once(|f| {
642636
let mut format_type_parameters = type_parameters.memoized();
643637
let mut format_parameters = params.memoized();
644-
let mut format_return_type = return_type.memoized();
638+
let mut format_return_type = return_type.map(FormatNodeWithoutTrailingComments).memoized();
645639

646640
// Inspect early, in case the `return_type` is formatted before `parameters`
647641
// in `should_group_function_parameters`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class A {
2+
m1(element: Element, key: string, undefined: undefined) /* block comment */ {
3+
// method body
4+
}
5+
m2(element: Element, key: string, undefined: undefined): void /* block comment */ {
6+
// method body
7+
}
8+
m3(tagName: string, rect: number[]): void // line comment
9+
{
10+
// method body
11+
}
12+
m4(tagName: string, rect: number[]) // line comment
13+
{
14+
// method body
15+
}
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
class A {
6+
m1(element: Element, key: string, undefined: undefined) /* block comment */ {
7+
// method body
8+
}
9+
m2(element: Element, key: string, undefined: undefined): void /* block comment */ {
10+
// method body
11+
}
12+
m3(tagName: string, rect: number[]): void // line comment
13+
{
14+
// method body
15+
}
16+
m4(tagName: string, rect: number[]) // line comment
17+
{
18+
// method body
19+
}
20+
}
21+
22+
==================== Output ====================
23+
class A {
24+
m1(element: Element, key: string, undefined: undefined) /* block comment */ {
25+
// method body
26+
}
27+
m2(
28+
element: Element,
29+
key: string,
30+
undefined: undefined,
31+
): void /* block comment */ {
32+
// method body
33+
}
34+
m3(tagName: string, rect: number[]): void {
35+
// line comment
36+
// method body
37+
}
38+
m4(tagName: string, rect: number[]) {
39+
// line comment
40+
// method body
41+
}
42+
}
43+
44+
===================== End =====================

tasks/prettier_conformance/snapshots/prettier.js.snap.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
js compatibility: 662/699 (94.71%)
1+
js compatibility: 661/699 (94.56%)
22

33
# Failed
44

55
| Spec path | Failed or Passed | Match ratio |
66
| :-------- | :--------------: | :---------: |
7+
| js/class-comment/misc.js | 💥 | 72.73% |
78
| js/comments/15661.js | 💥💥 | 55.17% |
89
| js/comments/empty-statements.js | 💥💥 | 90.91% |
910
| js/comments/function-declaration.js | 💥💥 | 92.80% |

0 commit comments

Comments
 (0)