Skip to content

Commit

Permalink
Fix own line last if comment (#1219)
Browse files Browse the repository at this point in the history
Fixes #1205
  • Loading branch information
vjeux committed Apr 13, 2017
1 parent ee7cfa9 commit 5d8ffd0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/comments.js
Expand Up @@ -161,6 +161,7 @@ function attach(comments, ast, text, options) {
handleMemberExpressionComments(enclosingNode, followingNode, comment) ||
handleIfStatementComments(
text,
precedingNode,
enclosingNode,
followingNode,
comment
Expand Down Expand Up @@ -209,6 +210,7 @@ function attach(comments, ast, text, options) {
handleTemplateLiteralComments(enclosingNode, comment) ||
handleIfStatementComments(
text,
precedingNode,
enclosingNode,
followingNode,
comment
Expand Down Expand Up @@ -240,6 +242,7 @@ function attach(comments, ast, text, options) {
if (
handleIfStatementComments(
text,
precedingNode,
enclosingNode,
followingNode,
comment
Expand Down Expand Up @@ -391,6 +394,7 @@ function addBlockOrNotComment(node, comment) {
// }
function handleIfStatementComments(
text,
precedingNode,
enclosingNode,
followingNode,
comment
Expand All @@ -408,7 +412,8 @@ function handleIfStatementComments(
// The only workaround I found is to look at the next character to see if
// it is a ).
if (getNextNonSpaceNonCommentCharacter(text, comment) === ")") {
return false;
addTrailingComment(precedingNode, comment);
return true;
}

if (followingNode.type === "BlockStatement") {
Expand Down
14 changes: 14 additions & 0 deletions tests/if/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -122,6 +122,13 @@ if (code === 92 /* '\\' */ /* '\\' */) {}
if (code === 92) /* '\\' */ {}
if (code === 92) { /* '\\' */ }
if (
1
// Comment
) {
a;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (code === 92 /* '\\' */) {
}
Expand All @@ -135,4 +142,11 @@ if (code === 92) {
/* '\\' */
}
if (
1
// Comment
) {
a;
}
`;
7 changes: 7 additions & 0 deletions tests/if/trailing_comment.js
Expand Up @@ -3,3 +3,10 @@ if (code === 92 /* '\' */ /* '\' */) {}

if (code === 92) /* '\' */ {}
if (code === 92) { /* '\' */ }

if (
1
// Comment
) {
a;
}

0 comments on commit 5d8ffd0

Please sign in to comment.