forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
418 lines (366 loc) · 9.76 KB
/
utils.js
File metadata and controls
418 lines (366 loc) · 9.76 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { join, literalline } from "./builders.js";
import {
DOC_TYPE_ALIGN,
DOC_TYPE_ARRAY,
DOC_TYPE_BREAK_PARENT,
DOC_TYPE_CURSOR,
DOC_TYPE_FILL,
DOC_TYPE_GROUP,
DOC_TYPE_IF_BREAK,
DOC_TYPE_INDENT,
DOC_TYPE_INDENT_IF_BREAK,
DOC_TYPE_LABEL,
DOC_TYPE_LINE,
DOC_TYPE_LINE_SUFFIX,
DOC_TYPE_LINE_SUFFIX_BOUNDARY,
DOC_TYPE_STRING,
DOC_TYPE_TRIM,
} from "./constants.js";
import InvalidDocError from "./invalid-doc-error.js";
import getDocType from "./utils/get-doc-type.js";
import traverseDoc from "./utils/traverse-doc.js";
function mapDoc(doc, cb) {
// Avoid creating `Map`
if (typeof doc === "string") {
return cb(doc);
}
// Within a doc tree, the same subtrees can be found multiple times.
// E.g., often this happens in conditional groups.
// As an optimization (those subtrees can be huge) and to maintain the
// reference structure of the tree, the mapping results are cached in
// a map and reused.
const mapped = new Map();
return rec(doc);
function rec(doc) {
if (mapped.has(doc)) {
return mapped.get(doc);
}
const result = process(doc);
mapped.set(doc, result);
return result;
}
function process(doc) {
switch (getDocType(doc)) {
case DOC_TYPE_ARRAY:
return cb(doc.map(rec));
case DOC_TYPE_FILL:
return cb({ ...doc, parts: doc.parts.map(rec) });
case DOC_TYPE_IF_BREAK:
return cb({
...doc,
breakContents: rec(doc.breakContents),
flatContents: rec(doc.flatContents),
});
case DOC_TYPE_GROUP: {
let { expandedStates, contents } = doc;
if (expandedStates) {
expandedStates = expandedStates.map(rec);
contents = expandedStates[0];
} else {
contents = rec(contents);
}
return cb({ ...doc, contents, expandedStates });
}
case DOC_TYPE_ALIGN:
case DOC_TYPE_INDENT:
case DOC_TYPE_INDENT_IF_BREAK:
case DOC_TYPE_LABEL:
case DOC_TYPE_LINE_SUFFIX:
return cb({ ...doc, contents: rec(doc.contents) });
case DOC_TYPE_STRING:
case DOC_TYPE_CURSOR:
case DOC_TYPE_TRIM:
case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
case DOC_TYPE_LINE:
case DOC_TYPE_BREAK_PARENT:
return cb(doc);
default:
/* c8 ignore next 3 */
throw new InvalidDocError(doc);
}
}
}
function findInDoc(doc, fn, defaultValue) {
let result = defaultValue;
let shouldSkipFurtherProcessing = false;
function findInDocOnEnterFn(doc) {
if (shouldSkipFurtherProcessing) {
return false;
}
const maybeResult = fn(doc);
if (maybeResult !== undefined) {
shouldSkipFurtherProcessing = true;
result = maybeResult;
}
}
traverseDoc(doc, findInDocOnEnterFn);
return result;
}
function willBreakFn(doc) {
if (doc.type === DOC_TYPE_GROUP && doc.break) {
return true;
}
if (doc.type === DOC_TYPE_LINE && doc.hard) {
return true;
}
if (doc.type === DOC_TYPE_BREAK_PARENT) {
return true;
}
}
function willBreak(doc) {
return findInDoc(doc, willBreakFn, false);
}
function breakParentGroup(groupStack) {
if (groupStack.length > 0) {
const parentGroup = groupStack.at(-1);
// Breaks are not propagated through conditional groups because
// the user is expected to manually handle what breaks.
if (!parentGroup.expandedStates && !parentGroup.break) {
// An alternative truthy value allows to distinguish propagated group breaks
// and not to print them as `group(..., { break: true })` in `--debug-print-doc`.
parentGroup.break = "propagated";
}
}
return null;
}
function propagateBreaks(doc) {
const alreadyVisitedSet = new Set();
const groupStack = [];
function propagateBreaksOnEnterFn(doc) {
if (doc.type === DOC_TYPE_BREAK_PARENT) {
breakParentGroup(groupStack);
}
if (doc.type === DOC_TYPE_GROUP) {
groupStack.push(doc);
if (alreadyVisitedSet.has(doc)) {
return false;
}
alreadyVisitedSet.add(doc);
}
}
function propagateBreaksOnExitFn(doc) {
if (doc.type === DOC_TYPE_GROUP) {
const group = groupStack.pop();
if (group.break) {
breakParentGroup(groupStack);
}
}
}
traverseDoc(
doc,
propagateBreaksOnEnterFn,
propagateBreaksOnExitFn,
/* shouldTraverseConditionalGroups */ true,
);
}
function removeLinesFn(doc) {
// Force this doc into flat mode by statically converting all
// lines into spaces (or soft lines into nothing). Hard lines
// should still output because there's too great of a chance
// of breaking existing assumptions otherwise.
if (doc.type === DOC_TYPE_LINE && !doc.hard) {
return doc.soft ? "" : " ";
}
if (doc.type === DOC_TYPE_IF_BREAK) {
return doc.flatContents;
}
return doc;
}
function removeLines(doc) {
return mapDoc(doc, removeLinesFn);
}
function stripTrailingHardlineFromParts(parts) {
parts = [...parts];
while (
parts.length >= 2 &&
parts.at(-2).type === DOC_TYPE_LINE &&
parts.at(-1).type === DOC_TYPE_BREAK_PARENT
) {
parts.length -= 2;
}
if (parts.length > 0) {
const lastPart = stripTrailingHardlineFromDoc(parts.at(-1));
parts[parts.length - 1] = lastPart;
}
return parts;
}
function stripTrailingHardlineFromDoc(doc) {
switch (getDocType(doc)) {
case DOC_TYPE_INDENT:
case DOC_TYPE_INDENT_IF_BREAK:
case DOC_TYPE_GROUP:
case DOC_TYPE_LINE_SUFFIX:
case DOC_TYPE_LABEL: {
const contents = stripTrailingHardlineFromDoc(doc.contents);
return { ...doc, contents };
}
case DOC_TYPE_IF_BREAK:
return {
...doc,
breakContents: stripTrailingHardlineFromDoc(doc.breakContents),
flatContents: stripTrailingHardlineFromDoc(doc.flatContents),
};
case DOC_TYPE_FILL:
return { ...doc, parts: stripTrailingHardlineFromParts(doc.parts) };
case DOC_TYPE_ARRAY:
return stripTrailingHardlineFromParts(doc);
case DOC_TYPE_STRING:
return doc.replace(/[\n\r]*$/u, "");
case DOC_TYPE_ALIGN:
case DOC_TYPE_CURSOR:
case DOC_TYPE_TRIM:
case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
case DOC_TYPE_LINE:
case DOC_TYPE_BREAK_PARENT:
// No op
break;
default:
throw new InvalidDocError(doc);
}
return doc;
}
function stripTrailingHardline(doc) {
// HACK remove ending hardline, original PR: #1984
return stripTrailingHardlineFromDoc(cleanDoc(doc));
}
function cleanDocFn(doc) {
switch (getDocType(doc)) {
case DOC_TYPE_FILL:
if (doc.parts.every((part) => part === "")) {
return "";
}
break;
case DOC_TYPE_GROUP:
if (!doc.contents && !doc.id && !doc.break && !doc.expandedStates) {
return "";
}
// Remove nested only group
if (
doc.contents.type === DOC_TYPE_GROUP &&
doc.contents.id === doc.id &&
doc.contents.break === doc.break &&
doc.contents.expandedStates === doc.expandedStates
) {
return doc.contents;
}
break;
case DOC_TYPE_ALIGN:
case DOC_TYPE_INDENT:
case DOC_TYPE_INDENT_IF_BREAK:
case DOC_TYPE_LINE_SUFFIX:
if (!doc.contents) {
return "";
}
break;
case DOC_TYPE_IF_BREAK:
if (!doc.flatContents && !doc.breakContents) {
return "";
}
break;
case DOC_TYPE_ARRAY: {
// Flat array, concat strings
const parts = [];
for (const part of doc) {
if (!part) {
continue;
}
const [currentPart, ...restParts] = Array.isArray(part) ? part : [part];
if (
typeof currentPart === "string" &&
typeof parts.at(-1) === "string"
) {
parts[parts.length - 1] += currentPart;
} else {
parts.push(currentPart);
}
parts.push(...restParts);
}
if (parts.length === 0) {
return "";
}
if (parts.length === 1) {
return parts[0];
}
return parts;
}
case DOC_TYPE_STRING:
case DOC_TYPE_CURSOR:
case DOC_TYPE_TRIM:
case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
case DOC_TYPE_LINE:
case DOC_TYPE_LABEL:
case DOC_TYPE_BREAK_PARENT:
// No op
break;
default:
/* c8 ignore next 3 */
throw new InvalidDocError(doc);
}
return doc;
}
// - concat strings
// - flat arrays except for parts of `fill`
// - merge arrays of strings into single strings
// - remove nested `group`s and empty `fill`/`align`/`indent`/`line-suffix`/`if-break` if possible
function cleanDoc(doc) {
return mapDoc(doc, (currentDoc) => cleanDocFn(currentDoc));
}
function normalizeParts(parts) {
const newParts = [];
const restParts = parts.filter(Boolean);
while (restParts.length > 0) {
const part = restParts.shift();
if (!part) {
continue;
}
if (Array.isArray(part)) {
restParts.unshift(...part);
continue;
}
if (
newParts.length > 0 &&
typeof newParts.at(-1) === "string" &&
typeof part === "string"
) {
newParts[newParts.length - 1] += part;
continue;
}
newParts.push(part);
}
return newParts;
}
function replaceEndOfLine(doc, replacement = literalline) {
return mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string"
? join(replacement, currentDoc.split("\n"))
: currentDoc,
);
}
function canBreakFn(doc) {
if (doc.type === DOC_TYPE_LINE) {
return true;
}
}
function canBreak(doc) {
return findInDoc(doc, canBreakFn, false);
}
function inheritLabel(doc, fn) {
return doc.type === DOC_TYPE_LABEL
? { ...doc, contents: fn(doc.contents) }
: fn(doc);
}
export {
canBreak,
cleanDoc,
findInDoc,
getDocType,
inheritLabel,
mapDoc,
normalizeParts,
propagateBreaks,
removeLines,
replaceEndOfLine,
stripTrailingHardline,
traverseDoc,
willBreak,
};