Fix displacing of comments in default switch case (#14047)

This commit is contained in:
Georgii Dolzhykov authored and GitHub committed 2022-12-29 10:13:02 +08:00
1 parent 32d46d9fa1
commit 201e02e14e
4 files changed
+111 -1

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
#### Fix displacing of comments in default switch case (#14047 by @thorn0)
It was a regression in Prettier 2.6.0.
<!-- prettier-ignore -->
```jsx
// Input
switch (state) {
default:
result = state; // no change
break;
}
// Prettier stable
switch (state) {
default: // no change
result = state;
break;
}
// Prettier main
switch (state) {
default:
result = state; // no change
break;
}
```
+3 -1
View File
@@ -884,7 +884,9 @@ function handleSwitchDefaultCaseComments({
if (
!enclosingNode ||
enclosingNode.type !== "SwitchCase" ||
enclosingNode.test
enclosingNode.test ||
!followingNode ||
followingNode !== enclosingNode.consequent[0]
) {
return false;
}
@@ -136,6 +136,66 @@ switch (x) {
================================================================================
`;
exports[`comments2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
switch(1){default: // comment1
}
switch(2){default: // comment2
//comment2a
}
switch(3){default: // comment3
break;// comment3a
}
switch(4){default: // comment4
// comment4a
break;// comment4b
}
switch(5){default: // comment5
// comment5a
foo();bar();//comment5b
break;// comment5c
}
=====================================output=====================================
switch (1) {
default: // comment1
}
switch (2) {
default: // comment2
//comment2a
}
switch (3) {
default: // comment3
break; // comment3a
}
switch (4) {
default: // comment4
// comment4a
break; // comment4b
}
switch (5) {
default: // comment5
// comment5a
foo();
bar(); //comment5b
break; // comment5c
}
================================================================================
`;
exports[`empty_lines.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
+21
View File
@@ -0,0 +1,21 @@
switch(1){default: // comment1
}
switch(2){default: // comment2
//comment2a
}
switch(3){default: // comment3
break;// comment3a
}
switch(4){default: // comment4
// comment4a
break;// comment4b
}
switch(5){default: // comment5
// comment5a
foo();bar();//comment5b
break;// comment5c
}