Remove getDocParts (#16026)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-02-04 14:33:39 +08:00
1 parent 76b8f95061
commit 384e2b4959
4 files changed
+9 -24

No files matched your search

+4 -4
View File
@@ -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] });
}
-14
View File
@@ -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,
@@ -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));
}
+4 -4
View File
@@ -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;