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.
master
Joseph Frazier 2017-05-14 18:58:06 -04:00 committed by Christopher Chedeau
parent f048ad3094
commit 4e9cdc413c
3 changed files with 16 additions and 16 deletions

View File

@ -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), {

View File

@ -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

View File

@ -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
};