-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.js
252 lines (224 loc) · 4.72 KB
/
doc.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
/**
* TBD properly tagged union for Doc object type is needed here.
*
* @typedef {object} DocObject
* @property {string} type
* @property {boolean} [hard]
* @property {boolean} [literal]
*
* @typedef {string | DocObject} Doc
*/
/**
* @param {Doc} val
*/
function assertDoc(val) {
/* istanbul ignore if */
if (
!(typeof val === 'string' || (val != null && typeof val.type === 'string'))
) {
throw new Error(
'Value ' + JSON.stringify(val) + ' is not a valid document'
);
}
}
/**
* @param {Doc[]} parts
* @returns Doc
*/
function concat(parts) {
if (process.env.NODE_ENV !== 'production') {
parts.forEach(assertDoc);
}
// We cannot do this until we change `printJSXElement` to not
// access the internals of a document directly.
// if(parts.length === 1) {
// // If it's a single document, no need to concat it.
// return parts[0];
// }
return { type: 'concat', parts };
}
/**
* @param {Doc} contents
* @returns Doc
*/
function indent(contents) {
if (process.env.NODE_ENV !== 'production') {
assertDoc(contents);
}
return { type: 'indent', contents };
}
/**
* @param {number} n
* @param {Doc} contents
* @returns Doc
*/
function align(n, contents) {
if (process.env.NODE_ENV !== 'production') {
assertDoc(contents);
}
return { type: 'align', contents, n };
}
/**
* @param {Doc} contents
* @param {object} [opts] - TBD ???
* @returns Doc
*/
function group(contents, opts) {
opts = opts || {};
if (process.env.NODE_ENV !== 'production') {
assertDoc(contents);
}
return {
type: 'group',
id: opts.id,
contents: contents,
break: !!opts.shouldBreak,
expandedStates: opts.expandedStates
};
}
/**
* @param {Doc} contents
* @returns Doc
*/
function dedentToRoot(contents) {
return align(-Infinity, contents);
}
/**
* @param {Doc} contents
* @returns Doc
*/
function markAsRoot(contents) {
// @ts-ignore - TBD ???:
return align({ type: 'root' }, contents);
}
/**
* @param {Doc} contents
* @returns Doc
*/
function dedent(contents) {
return align(-1, contents);
}
/**
* @param {Doc[]} states
* @param {object} [opts] - TBD ???
* @returns Doc
*/
function conditionalGroup(states, opts) {
return group(
states[0],
Object.assign(opts || {}, { expandedStates: states })
);
}
/**
* @param {Doc[]} parts
* @returns Doc
*/
function fill(parts) {
if (process.env.NODE_ENV !== 'production') {
parts.forEach(assertDoc);
}
return { type: 'fill', parts };
}
/**
* @param {Doc} [breakContents]
* @param {Doc} [flatContents]
* @param {object} [opts] - TBD ???
* @returns Doc
*/
function ifBreak(breakContents, flatContents, opts) {
opts = opts || {};
if (process.env.NODE_ENV !== 'production') {
if (breakContents) {
assertDoc(breakContents);
}
if (flatContents) {
assertDoc(flatContents);
}
}
return {
type: 'if-break',
breakContents,
flatContents,
groupId: opts.groupId
};
}
/**
* @param {Doc} contents
* @returns Doc
*/
function lineSuffix(contents) {
if (process.env.NODE_ENV !== 'production') {
assertDoc(contents);
}
return { type: 'line-suffix', contents };
}
const lineSuffixBoundary = { type: 'line-suffix-boundary' };
const breakParent = { type: 'break-parent' };
const trim = { type: 'trim' };
const line = { type: 'line' };
const softline = { type: 'line', soft: true };
const hardline = concat([{ type: 'line', hard: true }, breakParent]);
const literalline = concat([
{ type: 'line', hard: true, literal: true },
breakParent
]);
const cursor = { type: 'cursor', placeholder: Symbol('cursor') };
/**
* @param {Doc} sep
* @param {Doc[]} arr
* @returns Doc
*/
function join(sep, arr) {
const res = [];
for (let i = 0; i < arr.length; i++) {
if (i !== 0) {
res.push(sep);
}
res.push(arr[i]);
}
return concat(res);
}
/**
* @param {Doc} doc
* @param {number} size
* @param {number} tabWidth
*/
function addAlignmentToDoc(doc, size, tabWidth) {
let aligned = doc;
if (size > 0) {
// Use indent to add tabs for all the levels of tabs we need
for (let i = 0; i < Math.floor(size / tabWidth); ++i) {
aligned = indent(aligned);
}
// Use align for all the spaces that are needed
aligned = align(size % tabWidth, aligned);
// size is absolute from 0 and not relative to the current
// indentation, so we use -Infinity to reset the indentation to 0
aligned = align(-Infinity, aligned);
}
return aligned;
}
module.exports = {
concat,
join,
line,
softline,
hardline,
literalline,
group,
conditionalGroup,
fill,
lineSuffix,
lineSuffixBoundary,
cursor,
breakParent,
ifBreak,
trim,
indent,
align,
addAlignmentToDoc,
markAsRoot,
dedentToRoot,
dedent
};