Skip to content

Commit 201e02e

Browse files
authored
Fix displacing of comments in default switch case (#14047)
1 parent 32d46d9 commit 201e02e

4 files changed

Lines changed: 111 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#### Fix displacing of comments in default switch case (#14047 by @thorn0)
2+
3+
It was a regression in Prettier 2.6.0.
4+
5+
<!-- prettier-ignore -->
6+
```jsx
7+
// Input
8+
switch (state) {
9+
default:
10+
result = state; // no change
11+
break;
12+
}
13+
14+
// Prettier stable
15+
switch (state) {
16+
default: // no change
17+
result = state;
18+
break;
19+
}
20+
21+
// Prettier main
22+
switch (state) {
23+
default:
24+
result = state; // no change
25+
break;
26+
}
27+
```

src/language-js/comments.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,9 @@ function handleSwitchDefaultCaseComments({
884884
if (
885885
!enclosingNode ||
886886
enclosingNode.type !== "SwitchCase" ||
887-
enclosingNode.test
887+
enclosingNode.test ||
888+
!followingNode ||
889+
followingNode !== enclosingNode.consequent[0]
888890
) {
889891
return false;
890892
}

tests/format/js/switch/__snapshots__/jsfmt.spec.js.snap

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,66 @@ switch (x) {
136136
================================================================================
137137
`;
138138

139+
exports[`comments2.js format 1`] = `
140+
====================================options=====================================
141+
parsers: ["babel", "flow", "typescript"]
142+
printWidth: 80
143+
| printWidth
144+
=====================================input======================================
145+
switch(1){default: // comment1
146+
}
147+
148+
switch(2){default: // comment2
149+
//comment2a
150+
}
151+
152+
switch(3){default: // comment3
153+
break;// comment3a
154+
}
155+
156+
switch(4){default: // comment4
157+
// comment4a
158+
break;// comment4b
159+
}
160+
161+
switch(5){default: // comment5
162+
// comment5a
163+
foo();bar();//comment5b
164+
break;// comment5c
165+
}
166+
167+
=====================================output=====================================
168+
switch (1) {
169+
default: // comment1
170+
}
171+
172+
switch (2) {
173+
default: // comment2
174+
//comment2a
175+
}
176+
177+
switch (3) {
178+
default: // comment3
179+
break; // comment3a
180+
}
181+
182+
switch (4) {
183+
default: // comment4
184+
// comment4a
185+
break; // comment4b
186+
}
187+
188+
switch (5) {
189+
default: // comment5
190+
// comment5a
191+
foo();
192+
bar(); //comment5b
193+
break; // comment5c
194+
}
195+
196+
================================================================================
197+
`;
198+
139199
exports[`empty_lines.js format 1`] = `
140200
====================================options=====================================
141201
parsers: ["babel", "flow", "typescript"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
switch(1){default: // comment1
2+
}
3+
4+
switch(2){default: // comment2
5+
//comment2a
6+
}
7+
8+
switch(3){default: // comment3
9+
break;// comment3a
10+
}
11+
12+
switch(4){default: // comment4
13+
// comment4a
14+
break;// comment4b
15+
}
16+
17+
switch(5){default: // comment5
18+
// comment5a
19+
foo();bar();//comment5b
20+
break;// comment5c
21+
}

0 commit comments

Comments
 (0)