2017-01-20 09:54:32 -08:00
|
|
|
"use strict";
|
2017-01-28 07:50:22 -08:00
|
|
|
|
2021-01-04 10:04:30 +08:00
|
|
|
const { isConcat, getDocParts } = require("./doc-utils");
|
2017-01-20 09:54:32 -08:00
|
|
|
|
2021-01-04 10:04:30 +08:00
|
|
|
function flattenDoc(doc) {
|
|
|
|
|
if (isConcat(doc)) {
|
|
|
|
|
const res = [];
|
2021-01-29 21:48:34 +02:00
|
|
|
for (const part of getDocParts(doc)) {
|
|
|
|
|
if (isConcat(part)) {
|
|
|
|
|
res.push(...flattenDoc(part).parts);
|
2017-01-20 09:54:32 -08:00
|
|
|
} else {
|
2021-01-29 21:48:34 +02:00
|
|
|
const flattened = flattenDoc(part);
|
2017-01-20 09:54:32 -08:00
|
|
|
if (flattened !== "") {
|
|
|
|
|
res.push(flattened);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 10:04:30 +08:00
|
|
|
return { type: "concat", parts: res };
|
2017-01-23 18:47:11 -05:00
|
|
|
} else if (doc.type === "if-break") {
|
2020-01-19 06:57:54 +08:00
|
|
|
return {
|
|
|
|
|
...doc,
|
2017-07-08 14:07:39 +02:00
|
|
|
breakContents:
|
|
|
|
|
doc.breakContents != null ? flattenDoc(doc.breakContents) : null,
|
|
|
|
|
flatContents:
|
|
|
|
|
doc.flatContents != null ? flattenDoc(doc.flatContents) : null,
|
2020-01-19 06:57:54 +08:00
|
|
|
};
|
2017-01-23 18:47:11 -05:00
|
|
|
} else if (doc.type === "group") {
|
2020-01-19 06:57:54 +08:00
|
|
|
return {
|
|
|
|
|
...doc,
|
2017-01-20 09:54:32 -08:00
|
|
|
contents: flattenDoc(doc.contents),
|
2017-02-15 19:56:11 -08:00
|
|
|
expandedStates: doc.expandedStates
|
|
|
|
|
? doc.expandedStates.map(flattenDoc)
|
|
|
|
|
: doc.expandedStates,
|
2020-01-19 06:57:54 +08:00
|
|
|
};
|
2021-01-29 21:48:34 +02:00
|
|
|
} else if (doc.type === "fill") {
|
|
|
|
|
return { type: "fill", parts: doc.parts.map(flattenDoc) };
|
2017-01-23 18:47:11 -05:00
|
|
|
} else if (doc.contents) {
|
2020-01-19 06:57:54 +08:00
|
|
|
return { ...doc, contents: flattenDoc(doc.contents) };
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
2017-06-05 18:12:59 -04:00
|
|
|
return doc;
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
function printDocToDebug(doc) {
|
|
|
|
|
/** @type Record<symbol, string> */
|
|
|
|
|
const printedSymbols = Object.create(null);
|
|
|
|
|
/** @type Set<string> */
|
|
|
|
|
const usedKeysForSymbols = new Set();
|
|
|
|
|
return printDoc(flattenDoc(doc));
|
2017-01-20 09:54:32 -08:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
function printDoc(doc, index, parentParts) {
|
|
|
|
|
if (typeof doc === "string") {
|
|
|
|
|
return JSON.stringify(doc);
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
2021-01-29 21:48:34 +02:00
|
|
|
|
|
|
|
|
if (isConcat(doc)) {
|
2021-01-30 18:25:43 +02:00
|
|
|
const printed = getDocParts(doc).map(printDoc).filter(Boolean);
|
|
|
|
|
return printed.length === 1 ? printed[0] : `[${printed.join(", ")}]`;
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
2021-01-29 21:48:34 +02:00
|
|
|
|
|
|
|
|
if (doc.type === "line") {
|
|
|
|
|
const withBreakParent =
|
|
|
|
|
Array.isArray(parentParts) &&
|
|
|
|
|
parentParts[index + 1] &&
|
|
|
|
|
parentParts[index + 1].type === "break-parent";
|
|
|
|
|
if (doc.literal) {
|
|
|
|
|
return withBreakParent
|
|
|
|
|
? "literalline"
|
|
|
|
|
: "literallineWithoutBreakParent";
|
|
|
|
|
}
|
|
|
|
|
if (doc.hard) {
|
|
|
|
|
return withBreakParent ? "hardline" : "hardlineWithoutBreakParent";
|
|
|
|
|
}
|
|
|
|
|
if (doc.soft) {
|
|
|
|
|
return "softline";
|
|
|
|
|
}
|
|
|
|
|
return "line";
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (doc.type === "break-parent") {
|
|
|
|
|
const afterHardline =
|
|
|
|
|
Array.isArray(parentParts) &&
|
|
|
|
|
parentParts[index - 1] &&
|
|
|
|
|
parentParts[index - 1].type === "line" &&
|
|
|
|
|
parentParts[index - 1].hard;
|
|
|
|
|
return afterHardline ? undefined : "breakParent";
|
|
|
|
|
}
|
2017-01-23 18:47:11 -05:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (doc.type === "trim") {
|
|
|
|
|
return "trim";
|
|
|
|
|
}
|
2018-10-06 14:18:52 +02:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (doc.type === "indent") {
|
|
|
|
|
return "indent(" + printDoc(doc.contents) + ")";
|
|
|
|
|
}
|
2017-04-06 23:49:37 -03:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (doc.type === "align") {
|
|
|
|
|
return doc.n === Number.NEGATIVE_INFINITY
|
|
|
|
|
? "dedentToRoot(" + printDoc(doc.contents) + ")"
|
|
|
|
|
: doc.n < 0
|
|
|
|
|
? "dedent(" + printDoc(doc.contents) + ")"
|
|
|
|
|
: doc.n.type === "root"
|
|
|
|
|
? "markAsRoot(" + printDoc(doc.contents) + ")"
|
|
|
|
|
: "align(" +
|
|
|
|
|
JSON.stringify(doc.n) +
|
|
|
|
|
", " +
|
|
|
|
|
printDoc(doc.contents) +
|
|
|
|
|
")";
|
|
|
|
|
}
|
2017-01-20 09:54:32 -08:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (doc.type === "if-break") {
|
2017-04-12 10:16:11 -07:00
|
|
|
return (
|
2021-01-29 21:48:34 +02:00
|
|
|
"ifBreak(" +
|
|
|
|
|
printDoc(doc.breakContents) +
|
|
|
|
|
(doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
|
|
|
|
|
(doc.groupId
|
|
|
|
|
? (!doc.flatContents ? ', ""' : "") +
|
|
|
|
|
`, { groupId: ${printGroupId(doc.groupId)} }`
|
|
|
|
|
: "") +
|
|
|
|
|
")"
|
2017-04-12 10:16:11 -07:00
|
|
|
);
|
2017-01-23 18:47:11 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-05 01:03:40 +02:00
|
|
|
if (doc.type === "indent-if-break") {
|
|
|
|
|
const optionsParts = [];
|
|
|
|
|
|
|
|
|
|
if (doc.negate) {
|
|
|
|
|
optionsParts.push("negate: true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.groupId) {
|
|
|
|
|
optionsParts.push(`groupId: ${printGroupId(doc.groupId)}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const options =
|
|
|
|
|
optionsParts.length > 0 ? `, { ${optionsParts.join(", ")} }` : "";
|
|
|
|
|
|
|
|
|
|
return `indentIfBreak(${printDoc(doc.contents)}${options})`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (doc.type === "group") {
|
|
|
|
|
const optionsParts = [];
|
|
|
|
|
|
|
|
|
|
if (doc.break && doc.break !== "propagated") {
|
2021-02-06 14:41:02 +02:00
|
|
|
optionsParts.push("shouldBreak: true");
|
2021-01-29 21:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.id) {
|
|
|
|
|
optionsParts.push(`id: ${printGroupId(doc.id)}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const options =
|
|
|
|
|
optionsParts.length > 0 ? `, { ${optionsParts.join(", ")} }` : "";
|
|
|
|
|
|
|
|
|
|
if (doc.expandedStates) {
|
|
|
|
|
return `conditionalGroup([${doc.expandedStates
|
2021-02-04 13:42:47 +02:00
|
|
|
.map((part) => printDoc(part))
|
2021-01-29 21:48:34 +02:00
|
|
|
.join(",")}]${options})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `group(${printDoc(doc.contents)}${options})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.type === "fill") {
|
2021-01-30 18:25:43 +02:00
|
|
|
return `fill([${doc.parts.map((part) => printDoc(part)).join(", ")}])`;
|
2021-01-29 21:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.type === "line-suffix") {
|
|
|
|
|
return "lineSuffix(" + printDoc(doc.contents) + ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.type === "line-suffix-boundary") {
|
|
|
|
|
return "lineSuffixBoundary";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error("Unknown doc type " + doc.type);
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
2017-02-13 06:46:44 -08:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
function printGroupId(id) {
|
|
|
|
|
if (typeof id !== "symbol") {
|
|
|
|
|
return JSON.stringify(String(id));
|
|
|
|
|
}
|
2017-05-11 00:13:21 +01:00
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
if (id in printedSymbols) {
|
|
|
|
|
return printedSymbols[id];
|
|
|
|
|
}
|
2017-02-13 06:46:44 -08:00
|
|
|
|
2021-02-04 13:42:47 +02:00
|
|
|
// TODO: use Symbol.prototype.description instead of slice once Node 10 is dropped
|
|
|
|
|
const prefix = String(id).slice(7, -1) || "symbol";
|
2021-01-29 21:48:34 +02:00
|
|
|
for (let counter = 0; ; counter++) {
|
|
|
|
|
const key = prefix + (counter > 0 ? ` #${counter}` : "");
|
|
|
|
|
if (!usedKeysForSymbols.has(key)) {
|
|
|
|
|
usedKeysForSymbols.add(key);
|
|
|
|
|
return (printedSymbols[id] = `Symbol.for(${JSON.stringify(key)})`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-07 22:15:35 +01:00
|
|
|
}
|
2017-01-20 09:54:32 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 21:48:34 +02:00
|
|
|
module.exports = { printDocToDebug };
|