From 4e9cdc413c923d873ae0d2a033e0159646ccd88c Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Sun, 14 May 2017 18:58:06 -0400 Subject: [PATCH] Extract util.isBlockComment() helper (#1602) * Add trailing space when changing template literal line comment to block This was originally part of https://github.com/prettier/prettier/pull/1580, but I decided not to include it there, since it was unrelated. * Extract util.isBlockComment() helper * printer: Simplify genericPrintNoParens() JSXEmptyExpression logic * Extract makeBlockComment() helper * Move hasBlockComments() from printer to util Addresses https://github.com/prettier/prettier/pull/1602#issuecomment-301344310 * Revert "Extract makeBlockComment() helper" This reverts commit 7e940f43b62fb34d71fc3015ed14f9c59cf61d1a. * Revert "Add trailing space when changing template literal line comment to block" This reverts commit b3cf5b24c576ed062ca86975a09138e3f4ac9597. --- src/comments.js | 7 +++---- src/printer.js | 13 ++----------- src/util.js | 12 +++++++++++- 3 files changed, 16 insertions(+), 16 deletions(-) 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 };