Fixed unstable while comment (#5251)
* Fixed unstable while comment * Aligned while loop formatting more closely with if statment formattingmaster
parent
253716dd49
commit
2cc32da5af
|
@ -28,6 +28,14 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) {
|
|||
comment,
|
||||
options
|
||||
) ||
|
||||
handleWhileComments(
|
||||
text,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment,
|
||||
options
|
||||
) ||
|
||||
handleTryStatementComments(
|
||||
enclosingNode,
|
||||
precedingNode,
|
||||
|
@ -93,6 +101,14 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
|
|||
comment,
|
||||
options
|
||||
) ||
|
||||
handleWhileComments(
|
||||
text,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment,
|
||||
options
|
||||
) ||
|
||||
handleTryStatementComments(
|
||||
enclosingNode,
|
||||
precedingNode,
|
||||
|
@ -124,6 +140,14 @@ function handleRemainingComment(comment, text, options, ast, isLastComment) {
|
|||
comment,
|
||||
options
|
||||
) ||
|
||||
handleWhileComments(
|
||||
text,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment,
|
||||
options
|
||||
) ||
|
||||
handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) ||
|
||||
handleCommentInEmptyParens(text, enclosingNode, comment, options) ||
|
||||
handleMethodNameComments(
|
||||
|
@ -246,11 +270,11 @@ function handleIfStatementComments(
|
|||
}
|
||||
|
||||
// For comments positioned after the condition parenthesis in an if statement
|
||||
// before the consequent with or without brackets on, such as
|
||||
// if (a) /* comment */ {} or if (a) /* comment */ true,
|
||||
// we look at the next character to see if it is a { or if the following node
|
||||
// before the consequent without brackets on, such as
|
||||
// if (a) /* comment */ true,
|
||||
// we look at the next character to see if the following node
|
||||
// is the consequent for the if statement
|
||||
if (nextCharacter === "{" || enclosingNode.consequent === followingNode) {
|
||||
if (enclosingNode.consequent === followingNode) {
|
||||
addLeadingComment(followingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
@ -258,6 +282,45 @@ function handleIfStatementComments(
|
|||
return false;
|
||||
}
|
||||
|
||||
function handleWhileComments(
|
||||
text,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment,
|
||||
options
|
||||
) {
|
||||
if (
|
||||
!enclosingNode ||
|
||||
enclosingNode.type !== "WhileStatement" ||
|
||||
!followingNode
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// We unfortunately have no way using the AST or location of nodes to know
|
||||
// if the comment is positioned before the condition parenthesis:
|
||||
// while (a /* comment */) {}
|
||||
// The only workaround I found is to look at the next character to see if
|
||||
// it is a ).
|
||||
const nextCharacter = privateUtil.getNextNonSpaceNonCommentCharacter(
|
||||
text,
|
||||
comment,
|
||||
options.locEnd
|
||||
);
|
||||
if (nextCharacter === ")") {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (followingNode.type === "BlockStatement") {
|
||||
addBlockStatementFirstComment(followingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Same as IfStatement but for TryStatement
|
||||
function handleTryStatementComments(
|
||||
enclosingNode,
|
||||
|
|
|
@ -2062,3 +2062,51 @@ let // Comment
|
|||
bar = "val";
|
||||
|
||||
`;
|
||||
|
||||
exports[`while.js - flow-verify 1`] = `
|
||||
while(
|
||||
true
|
||||
// Comment
|
||||
) {}
|
||||
|
||||
while(true)// Comment
|
||||
{}
|
||||
|
||||
while(true){}// Comment
|
||||
|
||||
while(true)/*Comment*/{}
|
||||
|
||||
while(
|
||||
true // Comment
|
||||
&& true // Comment
|
||||
){}
|
||||
|
||||
while(true) {} // comment
|
||||
|
||||
while(true) /* comment */ ++x;
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
while (
|
||||
true
|
||||
// Comment
|
||||
) {}
|
||||
|
||||
while (true) {
|
||||
// Comment
|
||||
}
|
||||
|
||||
while (true) {} // Comment
|
||||
|
||||
while (true) {
|
||||
/*Comment*/
|
||||
}
|
||||
|
||||
while (
|
||||
true && // Comment
|
||||
true // Comment
|
||||
) {}
|
||||
|
||||
while (true) {} // comment
|
||||
|
||||
while (true) /* comment */ ++x;
|
||||
|
||||
`;
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
while(
|
||||
true
|
||||
// Comment
|
||||
) {}
|
||||
|
||||
while(true)// Comment
|
||||
{}
|
||||
|
||||
while(true){}// Comment
|
||||
|
||||
while(true)/*Comment*/{}
|
||||
|
||||
while(
|
||||
true // Comment
|
||||
&& true // Comment
|
||||
){}
|
||||
|
||||
while(true) {} // comment
|
||||
|
||||
while(true) /* comment */ ++x;
|
Loading…
Reference in New Issue