feat: adding plugin ability for isBlockComment (#4347)

* adding plugin ability for isBlockComment

* refactor to remove private util def

* remove old fn def
master
Mike Grip 2018-04-19 16:31:08 -04:00 committed by Christopher Chedeau
parent 7cb6790ce0
commit 4c464133ac
5 changed files with 32 additions and 34 deletions

View File

@ -411,29 +411,6 @@ function getLeftMost(node) {
return node;
}
function hasBlockComments(node) {
return node.comments && node.comments.some(isBlockComment);
}
function isBlockComment(comment) {
return comment.type === "Block" || comment.type === "CommentBlock";
}
function hasClosureCompilerTypeCastComment(text, node, locEnd) {
// https://github.com/google/closure-compiler/wiki/Annotating-Types#type-casts
// Syntax example: var x = /** @type {string} */ (fruit);
return (
node.comments &&
node.comments.some(
comment =>
comment.leading &&
isBlockComment(comment) &&
comment.value.match(/^\*\s*@type\s*{[^}]+}\s*$/) &&
getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === "("
)
);
}
function getAlignmentSize(value, tabWidth, startIndex) {
startIndex = startIndex || 0;
@ -803,9 +780,6 @@ module.exports = {
setLocStart,
setLocEnd,
startsWithNoLookaheadToken,
hasBlockComments,
isBlockComment,
hasClosureCompilerTypeCastComment,
getAlignmentSize,
getIndentSize,
printString,

View File

@ -700,8 +700,13 @@ function handleVariableDeclaratorComments(
return false;
}
function isBlockComment(comment) {
return comment.type === "Block" || comment.type === "CommentBlock";
}
module.exports = {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment
handleRemainingComment,
isBlockComment
};

View File

@ -3,6 +3,22 @@
const assert = require("assert");
const util = require("../common/util");
const comments = require("./comments");
function hasClosureCompilerTypeCastComment(text, node, locEnd) {
// https://github.com/google/closure-compiler/wiki/Annotating-Types#type-casts
// Syntax example: var x = /** @type {string} */ (fruit);
return (
node.comments &&
node.comments.some(
comment =>
comment.leading &&
comments.isBlockComment(comment) &&
comment.value.match(/^\*\s*@type\s*{[^}]+}\s*$/) &&
util.getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === "("
)
);
}
function needsParens(path, options) {
const parent = path.getParentNode();
@ -28,7 +44,7 @@ function needsParens(path, options) {
// Closure compiler requires that type casted expressions to be surrounded by
// parentheses.
if (
util.hasClosureCompilerTypeCastComment(
hasClosureCompilerTypeCastComment(
options.originalText,
node,
options.locEnd

View File

@ -1471,7 +1471,7 @@ function printPathNoParens(path, options, print, args) {
(hasTrailingComment(n.consequent) &&
n.consequent.comments.some(
comment =>
comment.trailing && !privateUtil.isBlockComment(comment)
comment.trailing && !handleComments.isBlockComment(comment)
)) ||
needsHardlineAfterDanglingComment(n);
const elseOnSameLine =
@ -1930,7 +1930,7 @@ function printPathNoParens(path, options, print, args) {
case "TSJsxClosingFragment": {
const hasComment = n.comments && n.comments.length;
const hasOwnLineComment =
hasComment && !n.comments.every(privateUtil.isBlockComment);
hasComment && !n.comments.every(handleComments.isBlockComment);
const isOpeningFragment =
n.type === "JSXOpeningFragment" || n.type === "TSJsxOpeningFragment";
return concat([
@ -1954,7 +1954,7 @@ function printPathNoParens(path, options, print, args) {
throw new Error("JSXTest should be handled by JSXElement");
case "JSXEmptyExpression": {
const requiresHardline =
n.comments && !n.comments.every(privateUtil.isBlockComment);
n.comments && !n.comments.every(handleComments.isBlockComment);
return concat([
comments.printDanglingComments(
@ -5313,7 +5313,7 @@ function needsHardlineAfterDanglingComment(node) {
node.comments.filter(comment => !comment.leading && !comment.trailing)
);
return (
lastDanglingComment && !privateUtil.isBlockComment(lastDanglingComment)
lastDanglingComment && !handleComments.isBlockComment(lastDanglingComment)
);
}
@ -5539,6 +5539,7 @@ module.exports = {
willPrintOwnComments,
canAttachComment,
printComment,
isBlockComment: handleComments.isBlockComment,
handleComments: {
ownLine: handleComments.handleOwnLineComment,
endOfLine: handleComments.handleEndOfLineComment,

View File

@ -368,7 +368,8 @@ function printLeadingComment(commentPath, print, options) {
if (!contents) {
return "";
}
const isBlock = privateUtil.isBlockComment(comment);
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);
// Leading block comments should see if they need to stay on the
// same line or not.
@ -390,7 +391,8 @@ function printTrailingComment(commentPath, print, options) {
if (!contents) {
return "";
}
const isBlock = privateUtil.isBlockComment(comment);
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);
// We don't want the line to break
// when the parentParentNode is a ClassDeclaration/-Expression