Skip to content

Commit

Permalink
If consequent is a block statement, treat as trailing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
duailibe committed Apr 5, 2018
1 parent 88489a9 commit fa089f5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/language-js/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 12 additions & 7 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions tests/flow/refinements/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit fa089f5

Please sign in to comment.