diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 80d2e320a..5f29dbc07 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -198,25 +198,39 @@ function handleIfStatementComments({ precedingNode === enclosingNode.consequent && followingNode === enclosingNode.alternate ) { - if (precedingNode.type === "BlockStatement") { - addTrailingComment(precedingNode, comment); - } else { - const isSingleLineComment = - isLineComment(comment) || - comment.loc.start.line === comment.loc.end.line; - const isSameLineComment = - comment.loc.start.line === precedingNode.loc.start.line; - if (isSingleLineComment && isSameLineComment) { - // example: - // if (cond1) expr1; // comment A - // else if (cond2) expr2; // comment A - // else expr3; + const maybeElseTokenIndex = getNextNonSpaceNonCommentCharacterIndex( + text, + locEnd(enclosingNode.consequent), + ); + // With the above conditions alone, this code would also match. This is a false positive. + // So, ignore cases where the token "else" appears immediately after the consequent: + // + // if (cond) a; + // else /* foo */ b; + if ( + locStart(comment) < maybeElseTokenIndex || + enclosingNode.alternate.type === "BlockStatement" + ) { + if (precedingNode.type === "BlockStatement") { addTrailingComment(precedingNode, comment); } else { - addDanglingComment(enclosingNode, comment); + const isSingleLineComment = + isLineComment(comment) || + comment.loc.start.line === comment.loc.end.line; + const isSameLineComment = + comment.loc.start.line === precedingNode.loc.start.line; + if (isSingleLineComment && isSameLineComment) { + // example: + // if (cond1) expr1; // comment A + // else if (cond2) expr2; // comment A + // else expr3; + addTrailingComment(precedingNode, comment); + } else { + addDanglingComment(enclosingNode, comment); + } } + return true; } - return true; } if (followingNode.type === "BlockStatement") { diff --git a/tests/format/js/comments/16398.js b/tests/format/js/comments/16398.js new file mode 100644 index 000000000..38588282a --- /dev/null +++ b/tests/format/js/comments/16398.js @@ -0,0 +1,5 @@ +if (foo) a = b; +/* foo */ else foo.split; + +if (foo) a = b; +else /* foo */ foo.split; diff --git a/tests/format/js/comments/__snapshots__/format.test.js.snap b/tests/format/js/comments/__snapshots__/format.test.js.snap index 1232832c9..9fcdf2ba9 100644 --- a/tests/format/js/comments/__snapshots__/format.test.js.snap +++ b/tests/format/js/comments/__snapshots__/format.test.js.snap @@ -321,6 +321,51 @@ void ( ================================================================================ `; +exports[`16398.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +if (foo) a = b; +/* foo */ else foo.split; + +if (foo) a = b; +else /* foo */ foo.split; + +=====================================output===================================== +if (foo) a = b +/* foo */ else foo.split + +if (foo) a = b +else /* foo */ foo.split + +================================================================================ +`; + +exports[`16398.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +if (foo) a = b; +/* foo */ else foo.split; + +if (foo) a = b; +else /* foo */ foo.split; + +=====================================output===================================== +if (foo) a = b; +/* foo */ else foo.split; + +if (foo) a = b; +else /* foo */ foo.split; + +================================================================================ +`; + exports[`arrow.js - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"]