Skip to content

Commit

Permalink
Fix displacing of comments in default switch case (prettier#14047)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 authored and medikoo committed Jan 4, 2024
1 parent 5d9ae35 commit acf48a5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
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
}

0 comments on commit acf48a5

Please sign in to comment.