Files
test-repo/src/doc-debug.js
T

98 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-01-20 09:54:32 -08:00
"use strict";
var jsesc = require("jsesc");
function flattenDoc(doc) {
2017-01-20 10:12:37 -08:00
if (!doc || typeof doc === "string" || doc.type === "line") {
2017-01-20 09:54:32 -08:00
return doc;
}
if (doc.type === "concat") {
var res = [];
for (var i = 0; i < doc.parts.length; ++i) {
const doc2 = doc.parts[i];
if (typeof doc2 !== "string" && doc2.type === "concat") {
[].push.apply(res, flattenDoc(doc2).parts);
} else {
const flattened = flattenDoc(doc2);
if (flattened !== "") {
res.push(flattened);
}
}
}
2017-01-20 10:12:37 -08:00
return Object.assign({}, doc, { parts: res });
2017-01-20 09:54:32 -08:00
}
if (doc.type === "indent") {
2017-01-20 10:12:37 -08:00
return Object.assign({}, doc, { contents: flattenDoc(doc.contents) });
2017-01-20 09:54:32 -08:00
}
if (doc.type === "if-break") {
return Object.assign({}, doc, {
breakContents: flattenDoc(doc.breakContents),
2017-01-20 10:12:37 -08:00
flatContents: flattenDoc(doc.flatContents)
2017-01-20 09:54:32 -08:00
});
}
if (doc.type === "group") {
return Object.assign({}, doc, {
contents: flattenDoc(doc.contents),
2017-01-20 10:12:37 -08:00
expandedStates: doc.expandedStates
? doc.expandedStates.map(flattenDoc)
: doc.expandedStates
2017-01-20 09:54:32 -08:00
});
}
}
function printDoc(doc) {
if (typeof doc === "string") {
2017-01-20 10:12:37 -08:00
return jsesc(doc, { wrap: true });
2017-01-20 09:54:32 -08:00
}
if (doc.type === "line") {
if (doc.literalline) {
return "literalline";
}
if (doc.hard) {
return "hardline";
}
if (doc.soft) {
return "softline";
}
return "line";
}
if (doc.type === "concat") {
return "[" + doc.parts.map(printDoc).join(", ") + "]";
}
if (doc.type === "indent") {
return "indent(" + doc.n + ", " + printDoc(doc.contents) + ")";
}
if (doc.type === "if-break") {
return "ifBreak(" +
printDoc(doc.breakContents) +
(doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
")";
}
if (doc.type === "group") {
2017-01-20 10:12:37 -08:00
return (doc.break
? "multilineGroup"
: doc.expandedStates ? "conditionalGroup" : "group") +
"(" +
printDoc(doc.contents) +
(doc.expandedStates
? ", [" + doc.expandedStates.map(printDoc).join(",") + "]"
: "") +
2017-01-20 09:54:32 -08:00
")";
}
}
module.exports = {
printDocToDebug: function(doc) {
return printDoc(flattenDoc(doc));
}
2017-01-20 10:12:37 -08:00
};