Skip to content

Commit b9ab7e2

Browse files
thorn0evilebottnawi
authored andcommitted
fix formatting of comments in flow enums (prettier#6860)
1 parent 54cbdb8 commit b9ab7e2

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

src/language-js/postprocess.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ function postprocess(ast, options) {
2727
});
2828
}
2929
break;
30+
case "EnumDeclaration":
31+
// A workaround for what looks like a bug in Flow.
32+
// Flow assigns the same range to enum nodes and enum body nodes.
33+
if (
34+
options.parser === "flow" &&
35+
node.body.range[0] === node.range[0] &&
36+
node.body.range[1] === node.range[1]
37+
) {
38+
node.body.range = [node.id.range[1], node.range[1] - 1];
39+
}
40+
// Babel does strange things as well. E.g. node.body.start > node.body.end can be true.
41+
if (options.parser === "babel-flow") {
42+
node.body.start = node.id.end;
43+
node.body.end = node.end - 1;
44+
}
45+
break;
3046
}
3147
});
3248

tests/flow_enums/__snapshots__/jsfmt.spec.js.snap

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,72 @@ enum E {
5050
================================================================================
5151
`;
5252

53+
exports[`enum-comments.js 1`] = `
54+
====================================options=====================================
55+
parsers: ["flow", "babel-flow"]
56+
printWidth: 80
57+
trailingComma: "all"
58+
| printWidth
59+
=====================================input======================================
60+
enum E1 {
61+
A = 0,
62+
// B = 1,
63+
C = 2
64+
}
65+
66+
enum E2 of number {
67+
// AA = -1,
68+
A = 0,
69+
// B = 1,
70+
C = 2
71+
// D = 100
72+
}
73+
74+
enum E3 {/*Q*/}
75+
76+
enum E4 of /*Q*/ string {
77+
Foo = "foo"
78+
}
79+
80+
enum E5 of string { // Q
81+
Bar = "bar"
82+
}
83+
84+
enum /*Q*/ E6 of string {}
85+
86+
=====================================output=====================================
87+
enum E1 {
88+
A = 0,
89+
// B = 1,
90+
C = 2,
91+
}
92+
93+
enum E2 of number {
94+
// AA = -1,
95+
A = 0,
96+
// B = 1,
97+
C = 2,
98+
// D = 100
99+
}
100+
101+
enum E3 {
102+
/*Q*/
103+
}
104+
105+
enum E4 of string {
106+
/*Q*/ Foo = "foo",
107+
}
108+
109+
enum E5 of string {
110+
// Q
111+
Bar = "bar",
112+
}
113+
114+
enum /*Q*/ E6 of string {}
115+
116+
================================================================================
117+
`;
118+
53119
exports[`enum-empty.js 1`] = `
54120
====================================options=====================================
55121
parsers: ["flow", "babel-flow"]

tests/flow_enums/enum-comments.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
enum E1 {
2+
A = 0,
3+
// B = 1,
4+
C = 2
5+
}
6+
7+
enum E2 of number {
8+
// AA = -1,
9+
A = 0,
10+
// B = 1,
11+
C = 2
12+
// D = 100
13+
}
14+
15+
enum E3 {/*Q*/}
16+
17+
enum E4 of /*Q*/ string {
18+
Foo = "foo"
19+
}
20+
21+
enum E5 of string { // Q
22+
Bar = "bar"
23+
}
24+
25+
enum /*Q*/ E6 of string {}

0 commit comments

Comments
 (0)