Skip to content

Commit 908503e

Browse files
authored
Handle dangling comments in SwitchStatement (#19581)
1 parent 943a475 commit 908503e

9 files changed

Lines changed: 338 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#### Handle dangling comments in `SwitchStatement` (#19581 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
switch (foo) {
7+
// comment
8+
}
9+
10+
// Prettier stable
11+
switch (
12+
foo
13+
// comment
14+
) {
15+
}
16+
17+
// Prettier main
18+
switch (foo) {
19+
// comment
20+
}
21+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { addDanglingComment } from "../../../main/comments/utilities.js";
2+
import getNextNonSpaceNonCommentCharacter from "../../../utilities/get-next-non-space-non-comment-character.js";
3+
import { locEnd } from "../../location/index.js";
4+
5+
/**
6+
@import {CommentContext} from "../handle-comments.js"
7+
*/
8+
9+
/**
10+
@param {CommentContext} param0
11+
*/
12+
function handleSwitchStatementComments({
13+
comment,
14+
precedingNode,
15+
enclosingNode,
16+
followingNode,
17+
text,
18+
}) {
19+
if (!(
20+
enclosingNode?.type === "SwitchStatement" &&
21+
enclosingNode.cases.length === 0 &&
22+
!followingNode &&
23+
precedingNode === enclosingNode.discriminant
24+
)) {
25+
return false;
26+
}
27+
28+
const nextCharacter = getNextNonSpaceNonCommentCharacter(
29+
text,
30+
locEnd(comment),
31+
);
32+
33+
if (nextCharacter === "}") {
34+
addDanglingComment(enclosingNode, comment);
35+
return true;
36+
}
37+
38+
return false;
39+
}
40+
41+
export { handleSwitchStatementComments };

‎src/language-js/comments/handle-comments.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { stripComments } from "../utilities/strip-comments.js";
3333
import { handleForXStatementComments } from "./attach/handle-for-x-statement-comments.js";
3434
import { handleIfStatementComments } from "./attach/handle-if-statement-comments.js";
35+
import { handleSwitchStatementComments } from "./attach/handle-switch-statement-comments.js";
3536
import { handleWhileLikeComments } from "./attach/handle-while-like-comments.js";
3637
import {
3738
addLeadingCommentToPossibleUnionType,
@@ -74,6 +75,7 @@ function handleOwnLineComment(context) {
7475
handleMemberExpressionComments,
7576
handleIfStatementComments,
7677
handleWhileLikeComments,
78+
handleSwitchStatementComments,
7779
handleTryStatementComments,
7880
handleClassComments,
7981
handleForXStatementComments,
@@ -105,6 +107,7 @@ function handleEndOfLineComment(context) {
105107
handleModuleSpecifiersComments,
106108
handleIfStatementComments,
107109
handleWhileLikeComments,
110+
handleSwitchStatementComments,
108111
handleTryStatementComments,
109112
handleClassComments,
110113
handleForXStatementComments,
@@ -134,6 +137,7 @@ function handleRemainingComment(context) {
134137
handleIgnoreComments,
135138
handleIfStatementComments,
136139
handleWhileLikeComments,
140+
handleSwitchStatementComments,
137141
handleForXStatementComments,
138142
handleMethodNameComments,
139143
handleOnlyComments,

‎src/language-js/print/switch-statement.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function printSwitchStatement(path, options, print) {
3333
),
3434
),
3535
])
36-
: "",
36+
: printDanglingComments(path, options, { indent: true }),
3737
hardline,
3838
"}",
3939
];

‎tests/format/js/comments/while-like/__snapshots__/format.test.js.snap‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ if(
1313
if(true)// 2
1414
{}
1515
16+
if(true)
17+
// 22
18+
{}
19+
1620
if(true){}// 3
1721
1822
if(true)/*4*/{}
@@ -29,6 +33,17 @@ if(true) /* 7 */ ++x;
2933
if(1) // 8
3034
foo();
3135
36+
if (0) {// comment
37+
}
38+
39+
if (0) {
40+
// comment
41+
}
42+
43+
if (0) {/* comment */}
44+
45+
if ((0, 0/* comment */)) {}
46+
3247
=====================================output=====================================
3348
if (
3449
true
@@ -40,6 +55,11 @@ if (true) // 2
4055
{
4156
}
4257
58+
if (true)
59+
// 22
60+
{
61+
}
62+
4363
if (true) {
4464
} // 3
4565
@@ -61,6 +81,111 @@ if (1)
6181
// 8
6282
foo();
6383
84+
if (0) {
85+
// comment
86+
}
87+
88+
if (0) {
89+
// comment
90+
}
91+
92+
if (0) {
93+
/* comment */
94+
}
95+
96+
if ((0, 0) /* comment */) {
97+
}
98+
99+
================================================================================
100+
`;
101+
102+
exports[`switch.js format 1`] = `
103+
====================================options=====================================
104+
parsers: ["babel", "flow", "typescript"]
105+
printWidth: 80 (default) |
106+
=====================================input======================================
107+
switch(
108+
true
109+
// 1
110+
) {}
111+
112+
switch(true)// 2
113+
{}
114+
115+
switch(true)
116+
// 22
117+
{}
118+
119+
switch(true){}// 3
120+
121+
switch(true)/*4*/{}
122+
123+
switch(
124+
true // 5
125+
&& true // 52
126+
){}
127+
128+
switch(true) {} // 6
129+
130+
switch (0) {// comment
131+
}
132+
133+
switch (0) {
134+
// comment
135+
}
136+
137+
switch (0) {/* comment */}
138+
139+
switch ((0, 0/* comment */)) {}
140+
141+
=====================================output=====================================
142+
switch (
143+
true
144+
// 1
145+
) {
146+
}
147+
148+
switch (
149+
true // 2
150+
) {
151+
}
152+
153+
switch (
154+
true
155+
// 22
156+
) {
157+
}
158+
159+
switch (true) {
160+
} // 3
161+
162+
switch (true /*4*/) {
163+
}
164+
165+
switch (
166+
true && // 5
167+
true // 52
168+
) {
169+
}
170+
171+
switch (true) {
172+
} // 6
173+
174+
switch (0) {
175+
// comment
176+
}
177+
178+
switch (0) {
179+
// comment
180+
}
181+
182+
switch (0) {
183+
/* comment */
184+
}
185+
186+
switch ((0, 0) /* comment */) {
187+
}
188+
64189
================================================================================
65190
`;
66191

@@ -77,6 +202,10 @@ while(
77202
while(true)// 2
78203
{}
79204
205+
while(true)
206+
// 22
207+
{}
208+
80209
while(true){}// 3
81210
82211
while(true)/*4*/{}
@@ -93,6 +222,17 @@ while(true) /* 7 */ ++x;
93222
while(1) // 8
94223
foo();
95224
225+
while (0) {// comment
226+
}
227+
228+
while (0) {
229+
// comment
230+
}
231+
232+
while (0) {/* comment */}
233+
234+
while ((0, 0/* comment */)) {}
235+
96236
=====================================output=====================================
97237
while (
98238
true
@@ -102,6 +242,10 @@ while (
102242
while (true) // 2
103243
{}
104244
245+
while (true)
246+
// 22
247+
{}
248+
105249
while (true) {} // 3
106250
107251
while (true) /*4*/ {}
@@ -119,6 +263,20 @@ while (1)
119263
// 8
120264
foo();
121265
266+
while (0) {
267+
// comment
268+
}
269+
270+
while (0) {
271+
// comment
272+
}
273+
274+
while (0) {
275+
/* comment */
276+
}
277+
278+
while ((0, 0) /* comment */) {}
279+
122280
================================================================================
123281
`;
124282

@@ -135,6 +293,10 @@ with(
135293
with(foo)// 2
136294
{}
137295
296+
with(foo)
297+
// 22
298+
{}
299+
138300
with(foo){}// 3
139301
140302
with(foo)/*4*/{}
@@ -151,6 +313,17 @@ with(foo) /* 7 */ ++x;
151313
with(1) // 8
152314
foo();
153315
316+
with (0) {// comment
317+
}
318+
319+
with (0) {
320+
// comment
321+
}
322+
323+
with (0) {/* comment */}
324+
325+
with ((0, 0/* comment */)) {}
326+
154327
=====================================output=====================================
155328
with (
156329
foo
@@ -162,6 +335,11 @@ with (foo) // 2
162335
{
163336
}
164337
338+
with (foo)
339+
// 22
340+
{
341+
}
342+
165343
with (foo) {
166344
} // 3
167345
@@ -183,5 +361,20 @@ with (1)
183361
// 8
184362
foo();
185363
364+
with (0) {
365+
// comment
366+
}
367+
368+
with (0) {
369+
// comment
370+
}
371+
372+
with (0) {
373+
/* comment */
374+
}
375+
376+
with ((0, 0) /* comment */) {
377+
}
378+
186379
================================================================================
187380
`;

0 commit comments

Comments
 (0)