diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index cc857fcf..dc48a8e3 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -626,6 +626,43 @@ return ( ); ``` +#### JavaScript: Fix inconsistent indentation in switch statement ([#6514] by [@sosukesuzuki]) + + +```js +// Input +switch ($veryLongAndVeryVerboseVariableName && $anotherVeryLongAndVeryVerboseVariableName) { +} + +switch ($longButSlightlyShorterVariableName && $anotherSlightlyShorterVariableName) { +} + +// Prettier (stable) +switch ( + $veryLongAndVeryVerboseVariableName && + $anotherVeryLongAndVeryVerboseVariableName +) { +} + +switch ( + $longButSlightlyShorterVariableName && $anotherSlightlyShorterVariableName +) { +} + +// Prettier (master) +switch ( + $veryLongAndVeryVerboseVariableName && + $anotherVeryLongAndVeryVerboseVariableName +) { +} + +switch ( + $longButSlightlyShorterVariableName && + $anotherSlightlyShorterVariableName +) { +} +``` + [#5910]: https://github.com/prettier/prettier/pull/5910 [#6186]: https://github.com/prettier/prettier/pull/6186 [#6206]: https://github.com/prettier/prettier/pull/6206 @@ -648,6 +685,7 @@ return ( [#6441]: https://github.com/prettier/prettier/pull/6441 [#6446]: https://github.com/prettier/prettier/pull/6446 [#6506]: https://github.com/prettier/prettier/pull/6506 +[#6514]: https://github.com/prettier/prettier/pull/6514 [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce [@sosukesuzuki]: https://github.com/sosukesuzuki diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 39aed235..93837e50 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -544,6 +544,7 @@ function printPathNoParens(path, options, print, args) { n !== parent.body && (parent.type === "IfStatement" || parent.type === "WhileStatement" || + parent.type === "SwitchStatement" || parent.type === "DoWhileStatement"); const parts = printBinaryishExpressions( diff --git a/tests/switch/__snapshots__/jsfmt.spec.js.snap b/tests/switch/__snapshots__/jsfmt.spec.js.snap index ec686651..27631557 100644 --- a/tests/switch/__snapshots__/jsfmt.spec.js.snap +++ b/tests/switch/__snapshots__/jsfmt.spec.js.snap @@ -288,6 +288,12 @@ switch (veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery alert( 'default' ); } +switch ($veryLongAndVeryVerboseVariableName && $anotherVeryLongAndVeryVerboseVariableName) { +} + +switch ($longButSlightlyShorterVariableName && $anotherSlightlyShorterVariableName) { +} + =====================================output===================================== switch (a) { case 3: @@ -315,7 +321,7 @@ switch ( switch ( veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong > - veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong + veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong ) { case 3: alert("3"); @@ -324,5 +330,17 @@ switch ( alert("default"); } +switch ( + $veryLongAndVeryVerboseVariableName && + $anotherVeryLongAndVeryVerboseVariableName +) { +} + +switch ( + $longButSlightlyShorterVariableName && + $anotherSlightlyShorterVariableName +) { +} + ================================================================================ `; diff --git a/tests/switch/switch.js b/tests/switch/switch.js index c4c7ac13..5ba69767 100644 --- a/tests/switch/switch.js +++ b/tests/switch/switch.js @@ -27,3 +27,9 @@ switch (veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery default: alert( 'default' ); } + +switch ($veryLongAndVeryVerboseVariableName && $anotherVeryLongAndVeryVerboseVariableName) { +} + +switch ($longButSlightlyShorterVariableName && $anotherSlightlyShorterVariableName) { +}