JS: Use isBlockComment/isLineComment (#9575)
This commit is contained in:
1 parent
743101f778
commit
1e673a6fed
6 files changed
+68
-59
No files matched your search
@@ -1,5 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const { isBlockComment } = require("./utils");
|
||||
|
||||
const ignoredProperties = new Set([
|
||||
"range",
|
||||
"raw",
|
||||
@@ -183,7 +185,7 @@ function clean(ast, newObj, parent) {
|
||||
ast.leadingComments &&
|
||||
ast.leadingComments.some(
|
||||
(comment) =>
|
||||
comment.type === "CommentBlock" &&
|
||||
isBlockComment(comment) &&
|
||||
["GraphQL", "HTML"].some(
|
||||
(languageName) => comment.value === ` ${languageName} `
|
||||
)
|
||||
|
||||
@@ -963,7 +963,6 @@ module.exports = {
|
||||
handleEndOfLineComment,
|
||||
handleRemainingComment,
|
||||
hasLeadingComment,
|
||||
isBlockComment,
|
||||
isTypeCastComment,
|
||||
getGapRegex,
|
||||
getCommentChildNodes,
|
||||
|
||||
@@ -15,7 +15,8 @@ const {
|
||||
},
|
||||
utils: { mapDoc, replaceNewlinesWithLiterallines },
|
||||
} = require("../document");
|
||||
const { isBlockComment, hasLeadingComment } = require("./comments");
|
||||
const { isBlockComment } = require("./utils");
|
||||
const { hasLeadingComment } = require("./comments");
|
||||
|
||||
function getParser(path) {
|
||||
if (
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
const flat = require("lodash/flatten");
|
||||
|
||||
const { isJSXNode } = require("../utils");
|
||||
const { locStart, locEnd } = require("../loc");
|
||||
const { hasNewlineInRange } = require("../../common/util");
|
||||
const handleComments = require("../comments");
|
||||
const { isJSXNode, isBlockComment } = require("../utils");
|
||||
const { locStart, locEnd } = require("../loc");
|
||||
const {
|
||||
builders: {
|
||||
concat,
|
||||
@@ -259,7 +257,7 @@ function printTernaryOperator(path, options, print, operatorOptions) {
|
||||
]).filter(Boolean);
|
||||
const shouldBreak = comments.some(
|
||||
(comment) =>
|
||||
handleComments.isBlockComment(comment) &&
|
||||
isBlockComment(comment) &&
|
||||
hasNewlineInRange(
|
||||
options.originalText,
|
||||
locStart(comment),
|
||||
|
||||
@@ -104,6 +104,8 @@ const {
|
||||
isTheOnlyJSXElementInMarkdown,
|
||||
isTSXFile,
|
||||
isTypeAnnotationAFunction,
|
||||
isBlockComment,
|
||||
isLineComment,
|
||||
matchJsxWhitespaceRegex,
|
||||
needsHardlineAfterDanglingComment,
|
||||
rawText,
|
||||
@@ -1158,8 +1160,8 @@ function printPathNoParens(path, options, print, args) {
|
||||
if (n.inexact) {
|
||||
let printed;
|
||||
if (hasDanglingComments(n)) {
|
||||
const hasLineComments = !n.comments.every(
|
||||
handleComments.isBlockComment
|
||||
const hasLineComments = !n.comments.every((comment) =>
|
||||
isBlockComment(comment)
|
||||
);
|
||||
const printedDanglingComments = comments.printDanglingComments(
|
||||
path,
|
||||
@@ -1626,8 +1628,7 @@ function printPathNoParens(path, options, print, args) {
|
||||
const commentOnOwnLine =
|
||||
(hasTrailingComment(n.consequent) &&
|
||||
n.consequent.comments.some(
|
||||
(comment) =>
|
||||
comment.trailing && !handleComments.isBlockComment(comment)
|
||||
(comment) => comment.trailing && !isBlockComment(comment)
|
||||
)) ||
|
||||
needsHardlineAfterDanglingComment(n);
|
||||
const elseOnSameLine =
|
||||
@@ -1810,7 +1811,7 @@ function printPathNoParens(path, options, print, args) {
|
||||
n.param.comments &&
|
||||
n.param.comments.some(
|
||||
(comment) =>
|
||||
!handleComments.isBlockComment(comment) ||
|
||||
!isBlockComment(comment) ||
|
||||
(comment.leading &&
|
||||
hasNewline(options.originalText, locEnd(comment))) ||
|
||||
(comment.trailing &&
|
||||
@@ -2111,7 +2112,7 @@ function printPathNoParens(path, options, print, args) {
|
||||
case "JSXClosingFragment": {
|
||||
const hasComment = n.comments && n.comments.length;
|
||||
const hasOwnLineComment =
|
||||
hasComment && !n.comments.every(handleComments.isBlockComment);
|
||||
hasComment && !n.comments.every((comment) => isBlockComment(comment));
|
||||
const isOpeningFragment = n.type === "JSXOpeningFragment";
|
||||
return concat([
|
||||
isOpeningFragment ? "<" : "</",
|
||||
@@ -2134,7 +2135,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(handleComments.isBlockComment);
|
||||
n.comments && !n.comments.every((comment) => isBlockComment(comment));
|
||||
|
||||
return concat([
|
||||
comments.printDanglingComments(
|
||||
@@ -4205,8 +4206,8 @@ function printTypeParameters(path, options, print, paramsKey) {
|
||||
if (!hasDanglingComments(n)) {
|
||||
return "";
|
||||
}
|
||||
const hasOnlyBlockComments = n.comments.every(
|
||||
handleComments.isBlockComment
|
||||
const hasOnlyBlockComments = n.comments.every((comment) =>
|
||||
isBlockComment(comment)
|
||||
);
|
||||
const printed = comments.printDanglingComments(
|
||||
path,
|
||||
@@ -5200,9 +5201,7 @@ function printReturnAndThrowArgument(path, options, print) {
|
||||
|
||||
const lastComment =
|
||||
Array.isArray(node.comments) && node.comments[node.comments.length - 1];
|
||||
const isLastCommentLine =
|
||||
lastComment &&
|
||||
(lastComment.type === "CommentLine" || lastComment.type === "Line");
|
||||
const isLastCommentLine = lastComment && isLineComment(lastComment);
|
||||
|
||||
if (isLastCommentLine) {
|
||||
parts.push(semi);
|
||||
@@ -5265,41 +5264,38 @@ function canAttachComment(node) {
|
||||
function printComment(commentPath, options) {
|
||||
const comment = commentPath.getValue();
|
||||
|
||||
switch (comment.type) {
|
||||
case "CommentBlock":
|
||||
case "Block": {
|
||||
if (isIndentableBlockComment(comment)) {
|
||||
const printed = printIndentableBlockComment(comment);
|
||||
// We need to prevent an edge case of a previous trailing comment
|
||||
// printed as a `lineSuffix` which causes the comments to be
|
||||
// interleaved. See https://github.com/prettier/prettier/issues/4412
|
||||
if (
|
||||
comment.trailing &&
|
||||
!hasNewline(options.originalText, locStart(comment), {
|
||||
backwards: true,
|
||||
})
|
||||
) {
|
||||
return concat([hardline, printed]);
|
||||
}
|
||||
return printed;
|
||||
}
|
||||
|
||||
const commentEnd = locEnd(comment);
|
||||
const isInsideFlowComment =
|
||||
options.originalText.slice(commentEnd - 3, commentEnd) === "*-/";
|
||||
|
||||
return "/*" + comment.value + (isInsideFlowComment ? "*-/" : "*/");
|
||||
}
|
||||
case "CommentLine":
|
||||
case "Line":
|
||||
// Supports `//`, `#!`, `<!--`, and `-->`
|
||||
return options.originalText
|
||||
.slice(locStart(comment), locEnd(comment))
|
||||
.trimEnd();
|
||||
default:
|
||||
/* istanbul ignore next */
|
||||
throw new Error("Not a comment: " + JSON.stringify(comment));
|
||||
if (isLineComment(comment)) {
|
||||
// Supports `//`, `#!`, `<!--`, and `-->`
|
||||
return options.originalText
|
||||
.slice(locStart(comment), locEnd(comment))
|
||||
.trimEnd();
|
||||
}
|
||||
|
||||
if (isBlockComment(comment)) {
|
||||
if (isIndentableBlockComment(comment)) {
|
||||
const printed = printIndentableBlockComment(comment);
|
||||
// We need to prevent an edge case of a previous trailing comment
|
||||
// printed as a `lineSuffix` which causes the comments to be
|
||||
// interleaved. See https://github.com/prettier/prettier/issues/4412
|
||||
if (
|
||||
comment.trailing &&
|
||||
!hasNewline(options.originalText, locStart(comment), {
|
||||
backwards: true,
|
||||
})
|
||||
) {
|
||||
return concat([hardline, printed]);
|
||||
}
|
||||
return printed;
|
||||
}
|
||||
|
||||
const commentEnd = locEnd(comment);
|
||||
const isInsideFlowComment =
|
||||
options.originalText.slice(commentEnd - 3, commentEnd) === "*-/";
|
||||
return "/*" + comment.value + (isInsideFlowComment ? "*-/" : "*/");
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
throw new Error("Not a comment: " + JSON.stringify(comment));
|
||||
}
|
||||
|
||||
function isIndentableBlockComment(comment) {
|
||||
@@ -5338,7 +5334,7 @@ module.exports = {
|
||||
willPrintOwnComments,
|
||||
canAttachComment,
|
||||
printComment,
|
||||
isBlockComment: handleComments.isBlockComment,
|
||||
isBlockComment,
|
||||
handleComments: {
|
||||
ownLine: handleComments.handleOwnLineComment,
|
||||
endOfLine: handleComments.handleEndOfLineComment,
|
||||
|
||||
@@ -153,6 +153,22 @@ function getLeftSidePathName(path, node) {
|
||||
throw new Error("Unexpected node has no left side.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Comment} comment
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isBlockComment(comment) {
|
||||
return comment.type === "Block" || comment.type === "CommentBlock";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Comment} comment
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isLineComment(comment) {
|
||||
return comment.type === "Line" || comment.type === "CommentLine";
|
||||
}
|
||||
|
||||
const exportDeclarationTypes = new Set([
|
||||
"ExportDefaultDeclaration",
|
||||
"ExportDefaultSpecifier",
|
||||
@@ -161,10 +177,6 @@ const exportDeclarationTypes = new Set([
|
||||
"ExportAllDeclaration",
|
||||
]);
|
||||
|
||||
function isBlockComment(comment) {
|
||||
return comment.type === "Block" || comment.type === "CommentBlock";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
@@ -1464,6 +1476,7 @@ module.exports = {
|
||||
identity,
|
||||
isBinaryish,
|
||||
isBlockComment,
|
||||
isLineComment,
|
||||
isCallOrOptionalCallExpression,
|
||||
isEmptyJSXElement,
|
||||
isExportDeclaration,
|
||||
|
||||
Reference in new issue
Block a user