diff --git a/changelog_unreleased/javascript/19581.md b/changelog_unreleased/javascript/19581.md new file mode 100644 index 000000000..09cb42081 --- /dev/null +++ b/changelog_unreleased/javascript/19581.md @@ -0,0 +1,21 @@ +#### Handle dangling comments in `SwitchStatement` (#19581 by @fisker) + + +```jsx +// Input +switch (foo) { + // comment +} + +// Prettier stable +switch ( + foo + // comment +) { +} + +// Prettier main +switch (foo) { + // comment +} +``` diff --git a/src/language-js/comments/attach/handle-switch-statement-comments.js b/src/language-js/comments/attach/handle-switch-statement-comments.js new file mode 100644 index 000000000..0336042dc --- /dev/null +++ b/src/language-js/comments/attach/handle-switch-statement-comments.js @@ -0,0 +1,41 @@ +import { addDanglingComment } from "../../../main/comments/utilities.js"; +import getNextNonSpaceNonCommentCharacter from "../../../utilities/get-next-non-space-non-comment-character.js"; +import { locEnd } from "../../location/index.js"; + +/** +@import {CommentContext} from "../handle-comments.js" +*/ + +/** +@param {CommentContext} param0 +*/ +function handleSwitchStatementComments({ + comment, + precedingNode, + enclosingNode, + followingNode, + text, +}) { + if (!( + enclosingNode?.type === "SwitchStatement" && + enclosingNode.cases.length === 0 && + !followingNode && + precedingNode === enclosingNode.discriminant + )) { + return false; + } + + const nextCharacter = getNextNonSpaceNonCommentCharacter( + text, + locEnd(comment), + ); + + if (nextCharacter === "}") { + addDanglingComment(enclosingNode, comment); + return true; + } + + return false; +} + +export { handleSwitchStatementComments }; diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 797e43af0..5014e3d24 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -32,6 +32,7 @@ import { import { stripComments } from "../utilities/strip-comments.js"; import { handleForXStatementComments } from "./attach/handle-for-x-statement-comments.js"; import { handleIfStatementComments } from "./attach/handle-if-statement-comments.js"; +import { handleSwitchStatementComments } from "./attach/handle-switch-statement-comments.js"; import { handleWhileLikeComments } from "./attach/handle-while-like-comments.js"; import { addLeadingCommentToPossibleUnionType, @@ -74,6 +75,7 @@ function handleOwnLineComment(context) { handleMemberExpressionComments, handleIfStatementComments, handleWhileLikeComments, + handleSwitchStatementComments, handleTryStatementComments, handleClassComments, handleForXStatementComments, @@ -105,6 +107,7 @@ function handleEndOfLineComment(context) { handleModuleSpecifiersComments, handleIfStatementComments, handleWhileLikeComments, + handleSwitchStatementComments, handleTryStatementComments, handleClassComments, handleForXStatementComments, @@ -134,6 +137,7 @@ function handleRemainingComment(context) { handleIgnoreComments, handleIfStatementComments, handleWhileLikeComments, + handleSwitchStatementComments, handleForXStatementComments, handleMethodNameComments, handleOnlyComments, diff --git a/src/language-js/print/switch-statement.js b/src/language-js/print/switch-statement.js index 9c3284d40..915a68fae 100644 --- a/src/language-js/print/switch-statement.js +++ b/src/language-js/print/switch-statement.js @@ -33,7 +33,7 @@ function printSwitchStatement(path, options, print) { ), ), ]) - : "", + : printDanglingComments(path, options, { indent: true }), hardline, "}", ]; diff --git a/tests/format/js/comments/while-like/__snapshots__/format.test.js.snap b/tests/format/js/comments/while-like/__snapshots__/format.test.js.snap index 07872a770..f5dae055f 100644 --- a/tests/format/js/comments/while-like/__snapshots__/format.test.js.snap +++ b/tests/format/js/comments/while-like/__snapshots__/format.test.js.snap @@ -13,6 +13,10 @@ if( if(true)// 2 {} +if(true) +// 22 +{} + if(true){}// 3 if(true)/*4*/{} @@ -29,6 +33,17 @@ if(true) /* 7 */ ++x; if(1) // 8 foo(); +if (0) {// comment +} + +if (0) { + // comment +} + +if (0) {/* comment */} + +if ((0, 0/* comment */)) {} + =====================================output===================================== if ( true @@ -40,6 +55,11 @@ if (true) // 2 { } +if (true) +// 22 +{ +} + if (true) { } // 3 @@ -61,6 +81,111 @@ if (1) // 8 foo(); +if (0) { + // comment +} + +if (0) { + // comment +} + +if (0) { + /* comment */ +} + +if ((0, 0) /* comment */) { +} + +================================================================================ +`; + +exports[`switch.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +switch( + true + // 1 + ) {} + +switch(true)// 2 +{} + +switch(true) +// 22 +{} + +switch(true){}// 3 + +switch(true)/*4*/{} + +switch( + true // 5 + && true // 52 + ){} + +switch(true) {} // 6 + +switch (0) {// comment +} + +switch (0) { + // comment +} + +switch (0) {/* comment */} + +switch ((0, 0/* comment */)) {} + +=====================================output===================================== +switch ( + true + // 1 +) { +} + +switch ( + true // 2 +) { +} + +switch ( + true + // 22 +) { +} + +switch (true) { +} // 3 + +switch (true /*4*/) { +} + +switch ( + true && // 5 + true // 52 +) { +} + +switch (true) { +} // 6 + +switch (0) { + // comment +} + +switch (0) { + // comment +} + +switch (0) { + /* comment */ +} + +switch ((0, 0) /* comment */) { +} + ================================================================================ `; @@ -77,6 +202,10 @@ while( while(true)// 2 {} +while(true) +// 22 +{} + while(true){}// 3 while(true)/*4*/{} @@ -93,6 +222,17 @@ while(true) /* 7 */ ++x; while(1) // 8 foo(); +while (0) {// comment +} + +while (0) { + // comment +} + +while (0) {/* comment */} + +while ((0, 0/* comment */)) {} + =====================================output===================================== while ( true @@ -102,6 +242,10 @@ while ( while (true) // 2 {} +while (true) +// 22 +{} + while (true) {} // 3 while (true) /*4*/ {} @@ -119,6 +263,20 @@ while (1) // 8 foo(); +while (0) { + // comment +} + +while (0) { + // comment +} + +while (0) { + /* comment */ +} + +while ((0, 0) /* comment */) {} + ================================================================================ `; @@ -135,6 +293,10 @@ with( with(foo)// 2 {} +with(foo) +// 22 +{} + with(foo){}// 3 with(foo)/*4*/{} @@ -151,6 +313,17 @@ with(foo) /* 7 */ ++x; with(1) // 8 foo(); +with (0) {// comment +} + +with (0) { + // comment +} + +with (0) {/* comment */} + +with ((0, 0/* comment */)) {} + =====================================output===================================== with ( foo @@ -162,6 +335,11 @@ with (foo) // 2 { } +with (foo) +// 22 +{ +} + with (foo) { } // 3 @@ -183,5 +361,20 @@ with (1) // 8 foo(); +with (0) { + // comment +} + +with (0) { + // comment +} + +with (0) { + /* comment */ +} + +with ((0, 0) /* comment */) { +} + ================================================================================ `; diff --git a/tests/format/js/comments/while-like/if.js b/tests/format/js/comments/while-like/if.js index 2f2334677..d717b8c76 100644 --- a/tests/format/js/comments/while-like/if.js +++ b/tests/format/js/comments/while-like/if.js @@ -6,6 +6,10 @@ if( if(true)// 2 {} +if(true) +// 22 +{} + if(true){}// 3 if(true)/*4*/{} @@ -21,3 +25,14 @@ if(true) /* 7 */ ++x; if(1) // 8 foo(); + +if (0) {// comment +} + +if (0) { + // comment +} + +if (0) {/* comment */} + +if ((0, 0/* comment */)) {} diff --git a/tests/format/js/comments/while-like/switch.js b/tests/format/js/comments/while-like/switch.js new file mode 100644 index 000000000..9c572805c --- /dev/null +++ b/tests/format/js/comments/while-like/switch.js @@ -0,0 +1,33 @@ +switch( + true + // 1 + ) {} + +switch(true)// 2 +{} + +switch(true) +// 22 +{} + +switch(true){}// 3 + +switch(true)/*4*/{} + +switch( + true // 5 + && true // 52 + ){} + +switch(true) {} // 6 + +switch (0) {// comment +} + +switch (0) { + // comment +} + +switch (0) {/* comment */} + +switch ((0, 0/* comment */)) {} diff --git a/tests/format/js/comments/while-like/while.js b/tests/format/js/comments/while-like/while.js index 666bf0e57..c5e9fe560 100644 --- a/tests/format/js/comments/while-like/while.js +++ b/tests/format/js/comments/while-like/while.js @@ -6,6 +6,10 @@ while( while(true)// 2 {} +while(true) +// 22 +{} + while(true){}// 3 while(true)/*4*/{} @@ -21,3 +25,14 @@ while(true) /* 7 */ ++x; while(1) // 8 foo(); + +while (0) {// comment +} + +while (0) { + // comment +} + +while (0) {/* comment */} + +while ((0, 0/* comment */)) {} diff --git a/tests/format/js/comments/while-like/with.js b/tests/format/js/comments/while-like/with.js index 08fa325a7..5c2231bac 100644 --- a/tests/format/js/comments/while-like/with.js +++ b/tests/format/js/comments/while-like/with.js @@ -6,6 +6,10 @@ with( with(foo)// 2 {} +with(foo) +// 22 +{} + with(foo){}// 3 with(foo)/*4*/{} @@ -21,3 +25,14 @@ with(foo) /* 7 */ ++x; with(1) // 8 foo(); + +with (0) {// comment +} + +with (0) { + // comment +} + +with (0) {/* comment */} + +with ((0, 0/* comment */)) {}