-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
doc-debug.js
126 lines (109 loc) · 2.72 KB
/
doc-debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use strict";
function flattenDoc(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);
}
}
}
return Object.assign({}, doc, { parts: res });
} else if (doc.type === "if-break") {
return Object.assign({}, doc, {
breakContents: doc.breakContents != null
? flattenDoc(doc.breakContents)
: null,
flatContents: doc.flatContents != null
? flattenDoc(doc.flatContents)
: null
});
} else if (doc.type === "group") {
return Object.assign({}, doc, {
contents: flattenDoc(doc.contents),
expandedStates: doc.expandedStates
? doc.expandedStates.map(flattenDoc)
: doc.expandedStates
});
} else if (doc.contents) {
return Object.assign({}, doc, { contents: flattenDoc(doc.contents) });
} else {
return doc;
}
}
function printDoc(doc) {
if (typeof doc === "string") {
return JSON.stringify(doc);
}
if (doc.type === "line") {
if (doc.literalline) {
return "literalline";
}
if (doc.hard) {
return "hardline";
}
if (doc.soft) {
return "softline";
}
return "line";
}
if (doc.type === "break-parent") {
return "breakParent";
}
if (doc.type === "concat") {
return "[" + doc.parts.map(printDoc).join(", ") + "]";
}
if (doc.type === "indent") {
return "indent(" + printDoc(doc.contents) + ")";
}
if (doc.type === "align") {
return "align(" + doc.n + ", " + printDoc(doc.contents) + ")";
}
if (doc.type === "if-break") {
return (
"ifBreak(" +
printDoc(doc.breakContents) +
(doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
")"
);
}
if (doc.type === "group") {
if (doc.expandedStates) {
return (
"conditionalGroup(" +
"[" +
doc.expandedStates.map(printDoc).join(",") +
"])"
);
}
return (
(doc.break ? "wrappedGroup" : "group") +
"(" +
printDoc(doc.contents) +
")"
);
}
if (doc.type === "fill") {
return ("fill") +
"(" +
doc.parts.map(printDoc).join(", ") +
")";
}
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);
}
module.exports = {
printDocToDebug: function(doc) {
return printDoc(flattenDoc(doc));
}
};