If consequent is a block statement, treat as trailing comment

master
Lucas Duailibe 2018-04-05 17:22:10 -03:00
parent 88489a9afe
commit fa089f5bb6
3 changed files with 18 additions and 10 deletions

View File

@ -211,7 +211,11 @@ function handleIfStatementComments(
precedingNode === enclosingNode.consequent &&
followingNode === enclosingNode.alternate
) {
addDanglingComment(enclosingNode, comment);
if (precedingNode.type === "BlockStatement") {
addTrailingComment(precedingNode, comment);
} else {
addDanglingComment(enclosingNode, comment);
}
return true;
}

View File

@ -1456,13 +1456,18 @@ function printPathNoParens(path, options, print, args) {
if (n.alternate) {
const commentOnOwnLine =
hasDanglingComments(n) &&
n.comments.some(
comment =>
!comment.leading &&
!comment.trailing &&
!privateUtil.isBlockComment(comment)
);
(hasTrailingComment(n.consequent) &&
n.consequent.comments.some(
comment =>
comment.trailing && !privateUtil.isBlockComment(comment)
)) ||
(hasDanglingComments(n) &&
n.comments.some(
comment =>
!comment.leading &&
!comment.trailing &&
!privateUtil.isBlockComment(comment)
));
const elseOnSameLine =
n.consequent.type === "BlockStatement" && !commentOnOwnLine;
parts.push(elseOnSameLine ? " " : hardline);

View File

@ -2571,8 +2571,7 @@ type Breakfast = Apple | Orange | Broccoli | Carrot;
function bar(x: Breakfast) {
if (x.kind === "Fruit") {
(x.taste: "Good");
}
// error, Apple.taste = Bad
} // error, Apple.taste = Bad
else (x.raw: "No"); // error, Carrot.raw = Maybe
}