Handle dangling comments in SwitchStatement (#19581)
This commit is contained in:
1 parent
943a475c35
commit
908503e9ee
9 files changed
+338
-1
No files matched your search
@@ -0,0 +1,21 @@
|
||||
#### Handle dangling comments in `SwitchStatement` (#19581 by @fisker)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```jsx
|
||||
// Input
|
||||
switch (foo) {
|
||||
// comment
|
||||
}
|
||||
|
||||
// Prettier stable
|
||||
switch (
|
||||
foo
|
||||
// comment
|
||||
) {
|
||||
}
|
||||
|
||||
// Prettier main
|
||||
switch (foo) {
|
||||
// comment
|
||||
}
|
||||
```
|
||||
@@ -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 };
|
||||
@@ -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,
|
||||
|
||||
@@ -33,7 +33,7 @@ function printSwitchStatement(path, options, print) {
|
||||
),
|
||||
),
|
||||
])
|
||||
: "",
|
||||
: printDanglingComments(path, options, { indent: true }),
|
||||
hardline,
|
||||
"}",
|
||||
];
|
||||
|
||||
@@ -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 */) {
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -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 */)) {}
|
||||
@@ -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 */)) {}
|
||||
@@ -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 */)) {}
|
||||
@@ -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 */)) {}
|
||||
Reference in new issue
Block a user