From 262f27bc6177b22cf78dd0292072ef0a9d71bef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Dessoude?= Date: Mon, 18 Nov 2019 06:41:44 +0100 Subject: [PATCH] Fix handling comments in labeled statements (#6984) --- changelog_unreleased/javascript/pr-6984.md | 23 ++++++++++++++++++++ src/language-js/comments.js | 3 ++- tests/label/__snapshots__/jsfmt.spec.js.snap | 9 ++++++++ tests/label/comment.js | 5 +++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/javascript/pr-6984.md 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 (; ;) { } +}