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