From 0e964679319ac4c51f093cac273375ecaf87944c Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Fri, 23 Dec 2022 00:54:25 +0200 Subject: [PATCH 1/2] Fix displacing of comments in default switch case --- src/language-js/comments.js | 4 +- .../switch/__snapshots__/jsfmt.spec.js.snap | 60 +++++++++++++++++++ tests/format/js/switch/comments2.js | 21 +++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/format/js/switch/comments2.js diff --git a/src/language-js/comments.js b/src/language-js/comments.js index cb32c25a13f0..61de3f993b48 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -884,7 +884,9 @@ function handleSwitchDefaultCaseComments({ if ( !enclosingNode || enclosingNode.type !== "SwitchCase" || - enclosingNode.test + enclosingNode.test || + !followingNode || + followingNode !== enclosingNode.consequent[0] ) { return false; } diff --git a/tests/format/js/switch/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/switch/__snapshots__/jsfmt.spec.js.snap index 9a4cf4dcab80..a2a13868661b 100644 --- a/tests/format/js/switch/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/switch/__snapshots__/jsfmt.spec.js.snap @@ -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"] diff --git a/tests/format/js/switch/comments2.js b/tests/format/js/switch/comments2.js new file mode 100644 index 000000000000..b588de99df51 --- /dev/null +++ b/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 +} From 132e17eeabf83fbaea00121c01755ebdfef10d27 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Fri, 23 Dec 2022 01:01:16 +0200 Subject: [PATCH 2/2] Add changelog --- changelog_unreleased/javascript/14047.md | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 changelog_unreleased/javascript/14047.md diff --git a/changelog_unreleased/javascript/14047.md b/changelog_unreleased/javascript/14047.md new file mode 100644 index 000000000000..9fbe380884cf --- /dev/null +++ b/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. + + +```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; +} +```