If consequent is a block statement, treat as trailing comment

This commit is contained in:
Lucas Duailibe committed 2018-04-05 17:22:10 -03:00
1 parent 88489a9afe
commit fa089f5bb6
3 files changed
+18 -10

No files matched your search

+5 -1
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;
}
+12 -7
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);
@@ -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
}