Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix displacing of comments in default switch case #14047

Merged
merged 2 commits into from Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions changelog_unreleased/javascript/14047.md
@@ -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;
}
```
4 changes: 3 additions & 1 deletion src/language-js/comments.js
Expand Up @@ -884,7 +884,9 @@ function handleSwitchDefaultCaseComments({
if (
!enclosingNode ||
enclosingNode.type !== "SwitchCase" ||
enclosingNode.test
enclosingNode.test ||
!followingNode ||
followingNode !== enclosingNode.consequent[0]
) {
return false;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/format/js/switch/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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"]
Expand Down
21 changes: 21 additions & 0 deletions tests/format/js/switch/comments2.js
@@ -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
}