feat: adding plugin ability for isBlockComment (#4347)
* adding plugin ability for isBlockComment * refactor to remove private util def * remove old fn defmaster
parent
7cb6790ce0
commit
4c464133ac
|
@ -411,29 +411,6 @@ function getLeftMost(node) {
|
||||||
return 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) {
|
function getAlignmentSize(value, tabWidth, startIndex) {
|
||||||
startIndex = startIndex || 0;
|
startIndex = startIndex || 0;
|
||||||
|
|
||||||
|
@ -803,9 +780,6 @@ module.exports = {
|
||||||
setLocStart,
|
setLocStart,
|
||||||
setLocEnd,
|
setLocEnd,
|
||||||
startsWithNoLookaheadToken,
|
startsWithNoLookaheadToken,
|
||||||
hasBlockComments,
|
|
||||||
isBlockComment,
|
|
||||||
hasClosureCompilerTypeCastComment,
|
|
||||||
getAlignmentSize,
|
getAlignmentSize,
|
||||||
getIndentSize,
|
getIndentSize,
|
||||||
printString,
|
printString,
|
||||||
|
|
|
@ -700,8 +700,13 @@ function handleVariableDeclaratorComments(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isBlockComment(comment) {
|
||||||
|
return comment.type === "Block" || comment.type === "CommentBlock";
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
handleOwnLineComment,
|
handleOwnLineComment,
|
||||||
handleEndOfLineComment,
|
handleEndOfLineComment,
|
||||||
handleRemainingComment
|
handleRemainingComment,
|
||||||
|
isBlockComment
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,6 +3,22 @@
|
||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
|
|
||||||
const util = require("../common/util");
|
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) {
|
function needsParens(path, options) {
|
||||||
const parent = path.getParentNode();
|
const parent = path.getParentNode();
|
||||||
|
@ -28,7 +44,7 @@ function needsParens(path, options) {
|
||||||
// Closure compiler requires that type casted expressions to be surrounded by
|
// Closure compiler requires that type casted expressions to be surrounded by
|
||||||
// parentheses.
|
// parentheses.
|
||||||
if (
|
if (
|
||||||
util.hasClosureCompilerTypeCastComment(
|
hasClosureCompilerTypeCastComment(
|
||||||
options.originalText,
|
options.originalText,
|
||||||
node,
|
node,
|
||||||
options.locEnd
|
options.locEnd
|
||||||
|
|
|
@ -1471,7 +1471,7 @@ function printPathNoParens(path, options, print, args) {
|
||||||
(hasTrailingComment(n.consequent) &&
|
(hasTrailingComment(n.consequent) &&
|
||||||
n.consequent.comments.some(
|
n.consequent.comments.some(
|
||||||
comment =>
|
comment =>
|
||||||
comment.trailing && !privateUtil.isBlockComment(comment)
|
comment.trailing && !handleComments.isBlockComment(comment)
|
||||||
)) ||
|
)) ||
|
||||||
needsHardlineAfterDanglingComment(n);
|
needsHardlineAfterDanglingComment(n);
|
||||||
const elseOnSameLine =
|
const elseOnSameLine =
|
||||||
|
@ -1930,7 +1930,7 @@ function printPathNoParens(path, options, print, args) {
|
||||||
case "TSJsxClosingFragment": {
|
case "TSJsxClosingFragment": {
|
||||||
const hasComment = n.comments && n.comments.length;
|
const hasComment = n.comments && n.comments.length;
|
||||||
const hasOwnLineComment =
|
const hasOwnLineComment =
|
||||||
hasComment && !n.comments.every(privateUtil.isBlockComment);
|
hasComment && !n.comments.every(handleComments.isBlockComment);
|
||||||
const isOpeningFragment =
|
const isOpeningFragment =
|
||||||
n.type === "JSXOpeningFragment" || n.type === "TSJsxOpeningFragment";
|
n.type === "JSXOpeningFragment" || n.type === "TSJsxOpeningFragment";
|
||||||
return concat([
|
return concat([
|
||||||
|
@ -1954,7 +1954,7 @@ function printPathNoParens(path, options, print, args) {
|
||||||
throw new Error("JSXTest should be handled by JSXElement");
|
throw new Error("JSXTest should be handled by JSXElement");
|
||||||
case "JSXEmptyExpression": {
|
case "JSXEmptyExpression": {
|
||||||
const requiresHardline =
|
const requiresHardline =
|
||||||
n.comments && !n.comments.every(privateUtil.isBlockComment);
|
n.comments && !n.comments.every(handleComments.isBlockComment);
|
||||||
|
|
||||||
return concat([
|
return concat([
|
||||||
comments.printDanglingComments(
|
comments.printDanglingComments(
|
||||||
|
@ -5313,7 +5313,7 @@ function needsHardlineAfterDanglingComment(node) {
|
||||||
node.comments.filter(comment => !comment.leading && !comment.trailing)
|
node.comments.filter(comment => !comment.leading && !comment.trailing)
|
||||||
);
|
);
|
||||||
return (
|
return (
|
||||||
lastDanglingComment && !privateUtil.isBlockComment(lastDanglingComment)
|
lastDanglingComment && !handleComments.isBlockComment(lastDanglingComment)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5539,6 +5539,7 @@ module.exports = {
|
||||||
willPrintOwnComments,
|
willPrintOwnComments,
|
||||||
canAttachComment,
|
canAttachComment,
|
||||||
printComment,
|
printComment,
|
||||||
|
isBlockComment: handleComments.isBlockComment,
|
||||||
handleComments: {
|
handleComments: {
|
||||||
ownLine: handleComments.handleOwnLineComment,
|
ownLine: handleComments.handleOwnLineComment,
|
||||||
endOfLine: handleComments.handleEndOfLineComment,
|
endOfLine: handleComments.handleEndOfLineComment,
|
||||||
|
|
|
@ -368,7 +368,8 @@ function printLeadingComment(commentPath, print, options) {
|
||||||
if (!contents) {
|
if (!contents) {
|
||||||
return "";
|
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
|
// Leading block comments should see if they need to stay on the
|
||||||
// same line or not.
|
// same line or not.
|
||||||
|
@ -390,7 +391,8 @@ function printTrailingComment(commentPath, print, options) {
|
||||||
if (!contents) {
|
if (!contents) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
const isBlock = privateUtil.isBlockComment(comment);
|
const isBlock =
|
||||||
|
options.printer.isBlockComment && options.printer.isBlockComment(comment);
|
||||||
|
|
||||||
// We don't want the line to break
|
// We don't want the line to break
|
||||||
// when the parentParentNode is a ClassDeclaration/-Expression
|
// when the parentParentNode is a ClassDeclaration/-Expression
|
||||||
|
|
Loading…
Reference in New Issue