From 4c464133ac37a966c792cacc660853d74c776841 Mon Sep 17 00:00:00 2001 From: Mike Grip Date: Thu, 19 Apr 2018 16:31:08 -0400 Subject: [PATCH] feat: adding plugin ability for isBlockComment (#4347) * adding plugin ability for isBlockComment * refactor to remove private util def * remove old fn def --- src/common/util.js | 26 -------------------------- src/language-js/comments.js | 7 ++++++- src/language-js/needs-parens.js | 18 +++++++++++++++++- src/language-js/printer-estree.js | 9 +++++---- src/main/comments.js | 6 ++++-- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/common/util.js b/src/common/util.js index 16b789a5..aa47df2f 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -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, diff --git a/src/language-js/comments.js b/src/language-js/comments.js index a8c2993c..4ad90155 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -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 }; diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 9aef0fde..5e385d11 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -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 diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 114d1be7..f973e426 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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, diff --git a/src/main/comments.js b/src/main/comments.js index 4113a925..4933ff42 100644 --- a/src/main/comments.js +++ b/src/main/comments.js @@ -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