diff --git a/changelog_unreleased/javascript/pr-6984.md b/changelog_unreleased/javascript/pr-6984.md new file mode 100644 index 000000000..854baa2d8 --- /dev/null +++ b/changelog_unreleased/javascript/pr-6984.md @@ -0,0 +1,23 @@ +#### Fix formatting of labeled statements with comments ([#6994](https://github.com/prettier/prettier/pull/6984) by [@clement26695](https://github.com/clement26695)) + +Formatting of comments in labeled statements was not stable + + +```jsx +// Input +loop1: +//test +const i = 3; + +// Prettier stable (first output) +loop1: //test +const i = 3; + +// Prettier stable (second output) +//test +loop1: const i = 3; + +// Prettier master (first output) +//test +loop1: const i = 3; +``` diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 5447b7f7a..f5cefb375 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -66,7 +66,8 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) { precedingNode, comment, options - ) + ) || + handleLabeledStatementComments(enclosingNode, comment) ) { return true; } diff --git a/tests/label/__snapshots__/jsfmt.spec.js.snap b/tests/label/__snapshots__/jsfmt.spec.js.snap index 556522ec8..e83d3fee3 100644 --- a/tests/label/__snapshots__/jsfmt.spec.js.snap +++ b/tests/label/__snapshots__/jsfmt.spec.js.snap @@ -10,12 +10,21 @@ printWidth: 80 inf_leave: // goto emulation for (;;) {} } +{ + inf_leave: + // goto emulation + for (; ;) { } +} =====================================output===================================== { // goto emulation inf_leave: for (;;) {} } +{ + // goto emulation + inf_leave: for (;;) {} +} ================================================================================ `; diff --git a/tests/label/comment.js b/tests/label/comment.js index 45e9363f1..2659e3978 100644 --- a/tests/label/comment.js +++ b/tests/label/comment.js @@ -2,3 +2,8 @@ inf_leave: // goto emulation for (;;) {} } +{ + inf_leave: + // goto emulation + for (; ;) { } +}