Skip to content

Commit 29a11ae

Browse files
authored
Fix comment in empty type literal (#18364)
1 parent 21a91fc commit 29a11ae

11 files changed

Lines changed: 267 additions & 41 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#### Fix comment in empty type literal (#18364 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
export type XXX = {
7+
// tbd
8+
};
9+
10+
// Prettier stable
11+
export type XXX = { // tbd };
12+
13+
// Prettier main
14+
export type XXX = {
15+
// tbd
16+
};
17+
```

‎src/language-js/print/class-body.js‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function printClassBody(path, options, print) {
3232
const isFlowTypeAnnotation = node.type === "ObjectTypeAnnotation";
3333
const isObjectType = !isClassBody(path);
3434
const separator = isObjectType ? line : hardline;
35+
const hasDanglingComments = hasComment(node, CommentCheckFlags.Dangling);
3536

3637
const [openingBrace, closingBrace] =
3738
isFlowTypeAnnotation && node.exact ? ["{|", "|}"] : "{}";
@@ -68,7 +69,7 @@ function printClassBody(path, options, print) {
6869
}
6970
});
7071

71-
if (hasComment(node, CommentCheckFlags.Dangling)) {
72+
if (hasDanglingComments) {
7273
parts.push(printDanglingComments(path, options));
7374
}
7475

@@ -92,13 +93,14 @@ function printClassBody(path, options, print) {
9293

9394
if (isObjectType) {
9495
const shouldBreak =
95-
options.objectWrap === "preserve" &&
96-
firstMember &&
97-
hasNewlineInRange(
98-
options.originalText,
99-
locStart(node),
100-
locStart(firstMember),
101-
);
96+
hasDanglingComments ||
97+
(options.objectWrap === "preserve" &&
98+
firstMember &&
99+
hasNewlineInRange(
100+
options.originalText,
101+
locStart(node),
102+
locStart(firstMember),
103+
));
102104

103105
let content;
104106
if (parts.length === 0) {

‎tests/format/flow/object-inexact/__snapshots__/format.test.js.snap‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ type Foo = {
8181
...
8282
};
8383
84-
type Foo = { /* comment */ ... };
84+
type Foo = {
85+
/* comment */
86+
...
87+
};
8588
8689
type Foo = {
8790
/* comment */
@@ -216,7 +219,10 @@ type Foo = {
216219
...
217220
};
218221
219-
type Foo = { /* comment */ ... };
222+
type Foo = {
223+
/* comment */
224+
...
225+
};
220226
221227
type Foo = {
222228
/* comment */
@@ -351,7 +357,10 @@ type Foo = {
351357
...
352358
};
353359
354-
type Foo = { /* comment */ ... };
360+
type Foo = {
361+
/* comment */
362+
...
363+
};
355364
356365
type Foo = {
357366
/* comment */

‎tests/format/js/classes/__snapshots__/format.test.js.snap‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,31 +239,11 @@ parsers: ["babel", "flow", "typescript"]
239239
printWidth: 80
240240
| printWidth
241241
=====================================input======================================
242-
class A1 {
243-
// comment
244-
}
245-
246-
class A2 { // comment
247-
}
248-
249-
class A3 {
250-
}
251-
252242
class A4 {
253243
m() {}
254244
}
255245
256246
=====================================output=====================================
257-
class A1 {
258-
// comment
259-
}
260-
261-
class A2 {
262-
// comment
263-
}
264-
265-
class A3 {}
266-
267247
class A4 {
268248
m() {}
269249
}

‎tests/format/js/classes/empty.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
class A1 {
2-
// comment
3-
}
4-
5-
class A2 { // comment
6-
}
7-
8-
class A3 {
9-
}
10-
111
class A4 {
122
m() {}
133
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`empty.js format 1`] = `
4+
====================================options=====================================
5+
parsers: ["babel", "flow", "typescript"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
class C1 {}
10+
class C2 {//comment
11+
}
12+
class C3 {
13+
//comment
14+
}
15+
class C4 {/*comment*/}
16+
class C5 {/*comment*/
17+
}
18+
class C6 {
19+
/*comment*/}
20+
class C7 {
21+
/*comment*/
22+
}
23+
24+
[
25+
class C1 {},
26+
class C2 {//comment
27+
},
28+
class C3 {
29+
//comment
30+
},
31+
class C4 {/*comment*/},
32+
class C5 {/*comment*/
33+
},
34+
class C6 {
35+
/*comment*/},
36+
class C7 {
37+
/*comment*/
38+
},
39+
];
40+
41+
=====================================output=====================================
42+
class C1 {}
43+
class C2 {
44+
//comment
45+
}
46+
class C3 {
47+
//comment
48+
}
49+
class C4 {
50+
/*comment*/
51+
}
52+
class C5 {
53+
/*comment*/
54+
}
55+
class C6 {
56+
/*comment*/
57+
}
58+
class C7 {
59+
/*comment*/
60+
}
61+
62+
[
63+
class C1 {},
64+
class C2 {
65+
//comment
66+
},
67+
class C3 {
68+
//comment
69+
},
70+
class C4 {
71+
/*comment*/
72+
},
73+
class C5 {
74+
/*comment*/
75+
},
76+
class C6 {
77+
/*comment*/
78+
},
79+
class C7 {
80+
/*comment*/
81+
},
82+
];
83+
84+
================================================================================
85+
`;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class C1 {}
2+
class C2 {//comment
3+
}
4+
class C3 {
5+
//comment
6+
}
7+
class C4 {/*comment*/}
8+
class C5 {/*comment*/
9+
}
10+
class C6 {
11+
/*comment*/}
12+
class C7 {
13+
/*comment*/
14+
}
15+
16+
[
17+
class C1 {},
18+
class C2 {//comment
19+
},
20+
class C3 {
21+
//comment
22+
},
23+
class C4 {/*comment*/},
24+
class C5 {/*comment*/
25+
},
26+
class C6 {
27+
/*comment*/},
28+
class C7 {
29+
/*comment*/
30+
},
31+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runFormatTest(import.meta, ["babel", "flow", "typescript"]);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`empty.ts format 1`] = `
4+
====================================options=====================================
5+
parsers: ["typescript"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
interface I1 {}
10+
interface I2 {//comment
11+
}
12+
interface I3 {
13+
//comment
14+
}
15+
interface I4 {/*comment*/}
16+
interface I5 {/*comment*/
17+
}
18+
interface I6 {
19+
/*comment*/}
20+
interface I7 {
21+
/*comment*/
22+
}
23+
24+
type T1 = {}
25+
type T2 = {//comment
26+
}
27+
type T3 = {
28+
//comment
29+
}
30+
type T4 = {/*comment*/}
31+
type T5 = {/*comment*/
32+
}
33+
type T6 = {
34+
/*comment*/}
35+
type T7 = {
36+
/*comment*/
37+
}
38+
39+
=====================================output=====================================
40+
interface I1 {}
41+
interface I2 {
42+
//comment
43+
}
44+
interface I3 {
45+
//comment
46+
}
47+
interface I4 {
48+
/*comment*/
49+
}
50+
interface I5 {
51+
/*comment*/
52+
}
53+
interface I6 {
54+
/*comment*/
55+
}
56+
interface I7 {
57+
/*comment*/
58+
}
59+
60+
type T1 = {};
61+
type T2 = {
62+
//comment
63+
};
64+
type T3 = {
65+
//comment
66+
};
67+
type T4 = {
68+
/*comment*/
69+
};
70+
type T5 = {
71+
/*comment*/
72+
};
73+
type T6 = {
74+
/*comment*/
75+
};
76+
type T7 = {
77+
/*comment*/
78+
};
79+
80+
================================================================================
81+
`;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
interface I1 {}
2+
interface I2 {//comment
3+
}
4+
interface I3 {
5+
//comment
6+
}
7+
interface I4 {/*comment*/}
8+
interface I5 {/*comment*/
9+
}
10+
interface I6 {
11+
/*comment*/}
12+
interface I7 {
13+
/*comment*/
14+
}
15+
16+
type T1 = {}
17+
type T2 = {//comment
18+
}
19+
type T3 = {
20+
//comment
21+
}
22+
type T4 = {/*comment*/}
23+
type T5 = {/*comment*/
24+
}
25+
type T6 = {
26+
/*comment*/}
27+
type T7 = {
28+
/*comment*/
29+
}

0 commit comments

Comments
 (0)