From 384e2b49594ea1584a125bb7fc8d2afe6414a400 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 4 Feb 2024 14:33:39 +0800 Subject: [PATCH] Remove `getDocParts` (#16026) --- src/document/printer.js | 8 ++++---- src/document/utils.js | 14 -------------- .../print/parenthesized-value-group.js | 3 +-- src/language-js/print/binaryish.js | 8 ++++---- 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/document/printer.js b/src/document/printer.js index a12c3a1ec..9b897e63a 100644 --- a/src/document/printer.js +++ b/src/document/printer.js @@ -19,7 +19,7 @@ import { DOC_TYPE_TRIM, } from "./constants.js"; import InvalidDocError from "./invalid-doc-error.js"; -import { getDocParts, getDocType, propagateBreaks } from "./utils.js"; +import { getDocType, propagateBreaks } from "./utils.js"; /** @typedef {typeof MODE_BREAK | typeof MODE_FLAT} Mode */ /** @typedef {{ ind: any, doc: any, mode: Mode }} Command */ @@ -222,8 +222,8 @@ function fits( } const { mode, doc } = cmds.pop(); - - switch (getDocType(doc)) { + const docType = getDocType(doc); + switch (docType) { case DOC_TYPE_STRING: out.push(doc); width -= getStringWidth(doc); @@ -231,7 +231,7 @@ function fits( case DOC_TYPE_ARRAY: case DOC_TYPE_FILL: { - const parts = getDocParts(doc); + const parts = docType === DOC_TYPE_ARRAY ? doc : doc.parts; for (let i = parts.length - 1; i >= 0; i--) { cmds.push({ mode, doc: parts[i] }); } diff --git a/src/document/utils.js b/src/document/utils.js index 9e46c792e..ba9e9a8bb 100644 --- a/src/document/utils.js +++ b/src/document/utils.js @@ -20,19 +20,6 @@ import InvalidDocError from "./invalid-doc-error.js"; import getDocType from "./utils/get-doc-type.js"; import traverseDoc from "./utils/traverse-doc.js"; -const getDocParts = (doc) => { - if (Array.isArray(doc)) { - return doc; - } - - /* c8 ignore next 3 */ - if (doc.type !== DOC_TYPE_FILL) { - throw new Error(`Expect doc to be 'array' or '${DOC_TYPE_FILL}'.`); - } - - return doc.parts; -}; - function mapDoc(doc, cb) { // Avoid creating `Map` if (typeof doc === "string") { @@ -432,7 +419,6 @@ export { canBreak, cleanDoc, findInDoc, - getDocParts, getDocType, inheritLabel, mapDoc, diff --git a/src/language-css/print/parenthesized-value-group.js b/src/language-css/print/parenthesized-value-group.js index 0add301d3..ecb937f19 100644 --- a/src/language-css/print/parenthesized-value-group.js +++ b/src/language-css/print/parenthesized-value-group.js @@ -9,7 +9,6 @@ import { line, softline, } from "../../document/builders.js"; -import { getDocParts } from "../../document/utils.js"; import isNextLineEmpty from "../../utils/is-next-line-empty.js"; import isNonEmptyArray from "../../utils/is-non-empty-array.js"; import { locEnd, locStart } from "../loc.js"; @@ -94,7 +93,7 @@ function printParenthesizedValueGroup(path, options, print) { child.groups[0].type !== "value-paren_group" && child.groups[2]?.type === "value-paren_group" ) { - const parts = getDocParts(doc.contents.contents); + const { parts } = doc.contents.contents; parts[1] = group(parts[1]); doc = group(dedent(doc)); } diff --git a/src/language-js/print/binaryish.js b/src/language-js/print/binaryish.js index 872c9820f..b04ee3964 100644 --- a/src/language-js/print/binaryish.js +++ b/src/language-js/print/binaryish.js @@ -8,7 +8,7 @@ import { softline, } from "../../document/builders.js"; import { DOC_TYPE_FILL, DOC_TYPE_GROUP } from "../../document/constants.js"; -import { cleanDoc, getDocParts } from "../../document/utils.js"; +import { cleanDoc } from "../../document/utils.js"; import { printComments } from "../../main/comments/print.js"; import { CommentCheckFlags, @@ -301,11 +301,11 @@ function printBinaryishExpressions( if (isNested && hasComment(node)) { const printed = cleanDoc(printComments(path, parts, options)); /* c8 ignore next 3 */ - if (Array.isArray(printed) || printed.type === DOC_TYPE_FILL) { - return getDocParts(printed); + if (printed.type === DOC_TYPE_FILL) { + return printed.parts; } - return [printed]; + return Array.isArray(printed) ? printed : [printed]; } return parts;