From 1e673a6fedd6ad6ab56e8065a55129cfe0cec73f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 5 Nov 2020 11:09:12 +0800 Subject: [PATCH] JS: Use `isBlockComment`/`isLineComment` (#9575) --- src/language-js/clean.js | 4 +- src/language-js/comments.js | 1 - src/language-js/embed.js | 3 +- src/language-js/print/ternary.js | 8 ++- src/language-js/printer-estree.js | 90 +++++++++++++++---------------- src/language-js/utils.js | 21 ++++++-- 6 files changed, 68 insertions(+), 59 deletions(-) diff --git a/src/language-js/clean.js b/src/language-js/clean.js index f695551fe..efacac3aa 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -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} ` ) diff --git a/src/language-js/comments.js b/src/language-js/comments.js index ef28f4333..9190a0592 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -963,7 +963,6 @@ module.exports = { handleEndOfLineComment, handleRemainingComment, hasLeadingComment, - isBlockComment, isTypeCastComment, getGapRegex, getCommentChildNodes, diff --git a/src/language-js/embed.js b/src/language-js/embed.js index 1797bf414..9daf9b028 100644 --- a/src/language-js/embed.js +++ b/src/language-js/embed.js @@ -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 ( diff --git a/src/language-js/print/ternary.js b/src/language-js/print/ternary.js index a1001da8a..604ea79de 100644 --- a/src/language-js/print/ternary.js +++ b/src/language-js/print/ternary.js @@ -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), diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 6ccebc7cd..ccead2459 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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 ? "<" : " 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 `//`, `#!`, `` - 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 `//`, `#!`, `` + 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, diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 4fa5d4dd5..1a8f95b27 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -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,