Keep comments of default cases in the same line of the case (#12177)
This commit is contained in:
1 parent
a407f9b5af
commit
f4d9816bc1
5 files changed
+181
No files matched your search
@@ -0,0 +1,40 @@
|
||||
#### Keep comments after `default` cases in the same line (#12177 by @duailibe)
|
||||
|
||||
Keep comments right after `default` cases (in switch statements) in the same line, when possible.
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```jsx
|
||||
// Input
|
||||
function read_statement() {
|
||||
switch (peek_keyword()) {
|
||||
case Keyword.impl: // impl<T> Growling<T> for Wolf {}
|
||||
return ImplDeclaration.read();
|
||||
|
||||
default: // expression;
|
||||
return ExpressionStatement.read();
|
||||
}
|
||||
}
|
||||
|
||||
// Prettier stable
|
||||
function read_statement() {
|
||||
switch (peek_keyword()) {
|
||||
case Keyword.impl: // impl<T> Growling<T> for Wolf {}
|
||||
return ImplDeclaration.read();
|
||||
|
||||
default:
|
||||
// expression;
|
||||
return ExpressionStatement.read();
|
||||
}
|
||||
}
|
||||
|
||||
// Prettier main
|
||||
function read_statement() {
|
||||
switch (peek_keyword()) {
|
||||
case Keyword.impl: // impl<T> Growling<T> for Wolf {}
|
||||
return ImplDeclaration.read();
|
||||
|
||||
default: // expression;
|
||||
return ExpressionStatement.read();
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -24,6 +24,7 @@ const {
|
||||
isCallExpression,
|
||||
isMemberExpression,
|
||||
isObjectProperty,
|
||||
isLineComment,
|
||||
getComments,
|
||||
CommentCheckFlags,
|
||||
markerForIfWithoutBlockAndSameLineComment,
|
||||
@@ -93,6 +94,7 @@ function handleEndOfLineComment(context) {
|
||||
handleTypeAliasComments,
|
||||
handleVariableDeclaratorComments,
|
||||
handleBreakAndContinueStatementComments,
|
||||
handleSwitchDefaultCaseComments,
|
||||
].some((fn) => fn(context));
|
||||
}
|
||||
|
||||
@@ -897,6 +899,28 @@ function handleTSMappedTypeComments({
|
||||
return false;
|
||||
}
|
||||
|
||||
function handleSwitchDefaultCaseComments({
|
||||
comment,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
}) {
|
||||
if (
|
||||
!enclosingNode ||
|
||||
enclosingNode.type !== "SwitchCase" ||
|
||||
enclosingNode.test
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (followingNode.type === "BlockStatement" && isLineComment(comment)) {
|
||||
addBlockStatementFirstComment(followingNode, comment);
|
||||
} else {
|
||||
addDanglingComment(enclosingNode, comment);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
|
||||
@@ -696,6 +696,10 @@ function printPathNoParens(path, options, print, args) {
|
||||
parts.push("default:");
|
||||
}
|
||||
|
||||
if (hasComment(node, CommentCheckFlags.Dangling)) {
|
||||
parts.push(" ", printDanglingComments(path, options, true));
|
||||
}
|
||||
|
||||
const consequent = node.consequent.filter(
|
||||
(node) => node.type !== "EmptyStatement"
|
||||
);
|
||||
|
||||
@@ -30,6 +30,41 @@ switch(x) {
|
||||
}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: // comment
|
||||
break;
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: // comment
|
||||
{break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: {// comment
|
||||
break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: /* comment */
|
||||
break;
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: /* comment */
|
||||
{break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: {/* comment */
|
||||
break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: /* comment */ {
|
||||
break;}
|
||||
}
|
||||
|
||||
=====================================output=====================================
|
||||
switch (true) {
|
||||
case true:
|
||||
@@ -55,6 +90,49 @@ switch (x) {
|
||||
}
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: // comment
|
||||
break;
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: {
|
||||
// comment
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: {
|
||||
// comment
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: /* comment */
|
||||
break;
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: /* comment */ {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: {
|
||||
/* comment */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: /* comment */ {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
|
||||
@@ -21,3 +21,38 @@ switch(x) {
|
||||
case y: {
|
||||
}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: // comment
|
||||
break;
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: // comment
|
||||
{break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: {// comment
|
||||
break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: /* comment */
|
||||
break;
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: /* comment */
|
||||
{break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: {/* comment */
|
||||
break;}
|
||||
}
|
||||
|
||||
switch(x) {
|
||||
default: /* comment */ {
|
||||
break;}
|
||||
}
|
||||
Reference in new issue
Block a user