diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index 708b75c2b..b59c95a8d 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -6,8 +6,8 @@ const { hasIgnoreComment, hasNewline, isFrontMatterNode, + isNextLineEmpty, } = require("../common/util"); -const { isNextLineEmpty } = require("../common/util-shared"); const { builders: { concat, diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 537acd5e5..22fa0a206 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -10,8 +10,7 @@ const { indent, ifBreak, } = require("../document").builders; -const { hasIgnoreComment } = require("../common/util"); -const { isNextLineEmpty } = require("../common/util-shared"); +const { hasIgnoreComment, isNextLineEmpty } = require("../common/util"); const { insertPragma } = require("./pragma"); function genericPrint(path, options, print) { diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 99224c58c..a039e808d 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -1,12 +1,17 @@ "use strict"; -const privateUtil = require("../common/util"); const { + getLast, + hasNewline, + getNextNonSpaceNonCommentCharacterIndexWithStartIndex, + getNextNonSpaceNonCommentCharacter, + hasNewlineInRange, + isNodeIgnoreComment, addLeadingComment, addTrailingComment, addDanglingComment, getNextNonSpaceNonCommentCharacterIndex, -} = require("../common/util-shared"); +} = require("../common/util"); function handleOwnLineComment(comment, text, options, ast, isLastComment) { const { precedingNode, enclosingNode, followingNode } = comment; @@ -247,7 +252,7 @@ function handleIfStatementComments( // if (a /* comment */) {} // The only workaround I found is to look at the next character to see if // it is a ). - const nextCharacter = privateUtil.getNextNonSpaceNonCommentCharacter( + const nextCharacter = getNextNonSpaceNonCommentCharacter( text, comment, options.locEnd @@ -316,7 +321,7 @@ function handleWhileComments( // while (a /* comment */) {} // The only workaround I found is to look at the next character to see if // it is a ). - const nextCharacter = privateUtil.getNextNonSpaceNonCommentCharacter( + const nextCharacter = getNextNonSpaceNonCommentCharacter( text, comment, options.locEnd @@ -398,7 +403,7 @@ function handleConditionalExpressionComments( ) { const isSameLineAsPrecedingNode = precedingNode && - !privateUtil.hasNewlineInRange( + !hasNewlineInRange( text, options.locEnd(precedingNode), options.locStart(comment) @@ -508,11 +513,8 @@ function handleMethodNameComments( enclosingNode.key === precedingNode && // special Property case: { key: /*comment*/(value) }; // comment should be attached to value instead of key - privateUtil.getNextNonSpaceNonCommentCharacter( - text, - precedingNode, - options.locEnd - ) !== ":" + getNextNonSpaceNonCommentCharacter(text, precedingNode, options.locEnd) !== + ":" ) { addTrailingComment(precedingNode, comment); return true; @@ -546,11 +548,7 @@ function handleFunctionNameComments( options ) { if ( - privateUtil.getNextNonSpaceNonCommentCharacter( - text, - comment, - options.locEnd - ) !== "(" + getNextNonSpaceNonCommentCharacter(text, comment, options.locEnd) !== "(" ) { return false; } @@ -590,11 +588,7 @@ function handleCommentAfterArrowParams(text, enclosingNode, comment, options) { function handleCommentInEmptyParens(text, enclosingNode, comment, options) { if ( - privateUtil.getNextNonSpaceNonCommentCharacter( - text, - comment, - options.locEnd - ) !== ")" + getNextNonSpaceNonCommentCharacter(text, comment, options.locEnd) !== ")" ) { return false; } @@ -653,11 +647,7 @@ function handleLastFunctionArgComments( precedingNode.type === "AssignmentPattern") && enclosingNode && isRealFunctionLikeNode(enclosingNode) && - privateUtil.getNextNonSpaceNonCommentCharacter( - text, - comment, - options.locEnd - ) === ")" + getNextNonSpaceNonCommentCharacter(text, comment, options.locEnd) === ")" ) { addTrailingComment(precedingNode, comment); return true; @@ -671,20 +661,18 @@ function handleLastFunctionArgComments( ) { const functionParamRightParenIndex = (() => { if ((enclosingNode.params || enclosingNode.parameters).length !== 0) { - return privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex( + return getNextNonSpaceNonCommentCharacterIndexWithStartIndex( text, options.locEnd( - privateUtil.getLast( - enclosingNode.params || enclosingNode.parameters - ) + getLast(enclosingNode.params || enclosingNode.parameters) ) ); } - const functionParamLeftParenIndex = privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex( + const functionParamLeftParenIndex = getNextNonSpaceNonCommentCharacterIndexWithStartIndex( text, options.locEnd(enclosingNode.id) ); - return privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex( + return getNextNonSpaceNonCommentCharacterIndexWithStartIndex( text, functionParamLeftParenIndex + 1 ); @@ -753,7 +741,7 @@ function handleUnionTypeComments( (enclosingNode.type === "UnionTypeAnnotation" || enclosingNode.type === "TSUnionType") ) { - if (privateUtil.isNodeIgnoreComment(comment)) { + if (isNodeIgnoreComment(comment)) { followingNode.prettierIgnore = true; comment.unignore = true; } @@ -768,7 +756,7 @@ function handleUnionTypeComments( followingNode && (followingNode.type === "UnionTypeAnnotation" || followingNode.type === "TSUnionType") && - privateUtil.isNodeIgnoreComment(comment) + isNodeIgnoreComment(comment) ) { followingNode.types[0].prettierIgnore = true; comment.unignore = true; @@ -839,7 +827,7 @@ function handleImportDeclarationComments( precedingNode.type === "ImportSpecifier" && enclosingNode && enclosingNode.type === "ImportDeclaration" && - privateUtil.hasNewline(text, options.locEnd(comment)) + hasNewline(text, options.locEnd(comment)) ) { addTrailingComment(precedingNode, comment); return true; @@ -898,11 +886,7 @@ function handleTSFunctionTrailingComments( (enclosingNode.type === "TSMethodSignature" || enclosingNode.type === "TSDeclareFunction" || enclosingNode.type === "TSAbstractMethodDefinition") && - privateUtil.getNextNonSpaceNonCommentCharacter( - text, - comment, - options.locEnd - ) === ";" + getNextNonSpaceNonCommentCharacter(text, comment, options.locEnd) === ";" ) { addTrailingComment(enclosingNode, comment); return true; diff --git a/src/language-js/print/call-arguments.js b/src/language-js/print/call-arguments.js index 07bf6299d..085741b0b 100644 --- a/src/language-js/print/call-arguments.js +++ b/src/language-js/print/call-arguments.js @@ -1,8 +1,11 @@ "use strict"; const comments = require("../../main/comments"); -const { getLast, getPenultimate } = require("../../common/util"); -const { isNextLineEmpty } = require("../../common/util-shared"); +const { + getLast, + getPenultimate, + isNextLineEmpty, +} = require("../../common/util"); const { hasLeadingComment, hasTrailingComment, diff --git a/src/language-js/print/member-chain.js b/src/language-js/print/member-chain.js index 1732a3066..e824b9e7c 100644 --- a/src/language-js/print/member-chain.js +++ b/src/language-js/print/member-chain.js @@ -3,12 +3,12 @@ const flat = require("lodash/flatten"); const comments = require("../../main/comments"); -const { getLast } = require("../../common/util"); const { + getLast, isNextLineEmpty, isNextLineEmptyAfterIndex, getNextNonSpaceNonCommentCharacterIndex, -} = require("../../common/util-shared"); +} = require("../../common/util"); const pathNeedsParens = require("../needs-parens"); const { hasLeadingComment, diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index d5d093778..2c7d4fca7 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -1,6 +1,11 @@ "use strict"; -const privateUtil = require("../common/util"); +const { + getLast, + getMinNotPresentContinuousCount, + getMaxContinuousCount, + getStringWidth, +} = require("../common/util"); const { builders: { breakParent, @@ -152,8 +157,8 @@ function genericPrint(path, options, print) { (prevNode && prevNode.type === "sentence" && prevNode.children.length > 0 && - privateUtil.getLast(prevNode.children).type === "word" && - !privateUtil.getLast(prevNode.children).hasTrailingPunctuation) || + getLast(prevNode.children).type === "word" && + !getLast(prevNode.children).hasTrailingPunctuation) || (nextNode && nextNode.type === "sentence" && nextNode.children.length > 0 && @@ -169,10 +174,7 @@ function genericPrint(path, options, print) { case "delete": return concat(["~~", printChildren(path, options, print), "~~"]); case "inlineCode": { - const backtickCount = privateUtil.getMinNotPresentContinuousCount( - node.value, - "`" - ); + const backtickCount = getMinNotPresentContinuousCount(node.value, "`"); const style = "`".repeat(backtickCount || 1); const gap = backtickCount && !/^\s/.test(node.value) ? " " : ""; return concat([style, gap, node.value, gap, style]); @@ -239,10 +241,7 @@ function genericPrint(path, options, print) { // fenced code block const styleUnit = options.__inJsTemplate ? "~" : "`"; const style = styleUnit.repeat( - Math.max( - 3, - privateUtil.getMaxContinuousCount(node.value, styleUnit) + 1 - ) + Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1) ); return concat([ style, @@ -268,8 +267,7 @@ function genericPrint(path, options, print) { case "html": { const parentNode = path.getParentNode(); const value = - parentNode.type === "root" && - privateUtil.getLast(parentNode.children) === node + parentNode.type === "root" && getLast(parentNode.children) === node ? node.value.trimEnd() : node.value; const isHtmlComment = /^$/.test(value); @@ -586,7 +584,7 @@ function printTable(path, options, print) { const columnMaxWidths = contents.reduce( (currentWidths, rowContents) => currentWidths.map((width, columnIndex) => - Math.max(width, privateUtil.getStringWidth(rowContents[columnIndex])) + Math.max(width, getStringWidth(rowContents[columnIndex])) ), contents[0].map(() => 3) // minimum width = 3 (---, :--, :-:, --:) ); @@ -663,17 +661,17 @@ function printTable(path, options, print) { } function alignLeft(text, width) { - const spaces = width - privateUtil.getStringWidth(text); + const spaces = width - getStringWidth(text); return concat([text, " ".repeat(spaces)]); } function alignRight(text, width) { - const spaces = width - privateUtil.getStringWidth(text); + const spaces = width - getStringWidth(text); return concat([" ".repeat(spaces), text]); } function alignCenter(text, width) { - const spaces = width - privateUtil.getStringWidth(text); + const spaces = width - getStringWidth(text); const left = Math.floor(spaces / 2); const right = spaces - left; return concat([" ".repeat(left), text, " ".repeat(right)]); diff --git a/src/main/comments.js b/src/main/comments.js index e64574e2c..c4f49d89c 100644 --- a/src/main/comments.js +++ b/src/main/comments.js @@ -16,12 +16,10 @@ const { skipNewline, skipSpaces, isPreviousLineEmpty, -} = require("../common/util"); -const { addLeadingComment, addDanglingComment, addTrailingComment, -} = require("../common/util-shared"); +} = require("../common/util"); const childNodesCacheKey = Symbol("child-nodes"); function getSortedChildNodes(node, options, resultArray) { diff --git a/src/main/core.js b/src/main/core.js index 72ab155e9..3faeefc2a 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -6,7 +6,7 @@ const { printer: { printDocToString }, debug: { printDocToDebug }, } = require("../document"); -const privateUtil = require("../common/util"); +const { isNodeIgnoreComment, getAlignmentSize } = require("../common/util"); const { guessEndOfLine, convertEndOfLineToChars, @@ -33,7 +33,7 @@ function ensureAllCommentsPrinted(astComments) { } for (let i = 0; i < astComments.length; ++i) { - if (privateUtil.isNodeIgnoreComment(astComments[i])) { + if (isNodeIgnoreComment(astComments[i])) { // If there's a prettier-ignore, we're not printing that sub-tree so we // don't know if the comments was printed or not. return; @@ -187,10 +187,7 @@ function formatRange(text, opts) { ); const indentString = text.slice(rangeStart2, rangeStart).match(/^\s*/)[0]; - const alignmentSize = privateUtil.getAlignmentSize( - indentString, - opts.tabWidth - ); + const alignmentSize = getAlignmentSize(indentString, opts.tabWidth); const rangeResult = coreFormat( rangeString,