diff --git a/src/comments.js b/src/comments.js index aea9ca83..7b1673a3 100644 --- a/src/comments.js +++ b/src/comments.js @@ -728,8 +728,7 @@ function handleImportDeclarationComments( precedingNode && enclosingNode && enclosingNode.type === "ImportDeclaration" && - comment.type !== "CommentBlock" && - comment.type !== "Block" + !util.isBlockComment(comment) ) { addTrailingComment(precedingNode, comment); return true; @@ -821,7 +820,7 @@ function getQuasiRange(expr) { function printLeadingComment(commentPath, print, options) { const comment = commentPath.getValue(); const contents = printComment(commentPath); - const isBlock = comment.type === "Block" || comment.type === "CommentBlock"; + const isBlock = util.isBlockComment(comment); // Leading block comments should see if they need to stay on the // same line or not. @@ -838,7 +837,7 @@ function printLeadingComment(commentPath, print, options) { function printTrailingComment(commentPath, print, options) { const comment = commentPath.getValue(); const contents = printComment(commentPath); - const isBlock = comment.type === "Block" || comment.type === "CommentBlock"; + const isBlock = util.isBlockComment(comment); if ( util.hasNewline(options.originalText, locStart(comment), { diff --git a/src/printer.js b/src/printer.js index 4a7caab7..1be95b9b 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1542,9 +1542,7 @@ function genericPrintNoParens(path, options, print, args) { case "JSXText": throw new Error("JSXTest should be handled by JSXElement"); case "JSXEmptyExpression": - const requiresHardline = n.comments && n.comments.some( - comment => comment.type === "Line" || comment.type === "CommentLine" - ); + const requiresHardline = n.comments && !n.comments.every(util.isBlockComment); return concat([ comments.printDanglingComments( @@ -2856,7 +2854,7 @@ function canPrintParamsWithoutParens(node) { !node.rest && node.params[0].type === "Identifier" && !node.params[0].typeAnnotation && - !hasBlockComments(node.params[0]) && + !util.hasBlockComments(node.params[0]) && !node.params[0].optional && !node.predicate && !node.returnType @@ -4231,13 +4229,6 @@ function hasDanglingComments(node) { node.comments.some(comment => !comment.leading && !comment.trailing); } -function hasBlockComments(node) { - return node.comments && - node.comments.some(comment => - comment.type === "Block" || comment.type === "CommentBlock" - ); -} - function removeLines(doc) { // Force this doc into flat mode by statically converting all // lines into spaces (or soft lines into nothing). Hard lines diff --git a/src/util.js b/src/util.js index fbb1776c..6106c109 100644 --- a/src/util.js +++ b/src/util.js @@ -312,6 +312,14 @@ function getLeftMost(node) { } } +function hasBlockComments(node) { + return node.comments && node.comments.some(isBlockComment); +} + +function isBlockComment(comment) { + return comment.type === "Block" || comment.type === "CommentBlock"; +} + module.exports = { getPrecedence, isExportDeclaration, @@ -331,5 +339,7 @@ module.exports = { locEnd, setLocStart, setLocEnd, - startsWithNoLookaheadToken + startsWithNoLookaheadToken, + hasBlockComments, + isBlockComment };