Repository navigation
Expand file tree
/
Copy patharray.js
More file actions
237 lines (208 loc) · 6.37 KB
/
Copy patharray.js
File metadata and controls
237 lines (208 loc) · 6.37 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
import {
fill,
group,
hardline,
ifBreak,
indent,
line,
softline,
} from "../../document/builders.js";
import { printDanglingComments } from "../../main/comments/print.js";
import hasNewline from "../../utils/has-newline.js";
import isNextLineEmptyAfterIndex from "../../utils/is-next-line-empty.js";
import skipInlineComment from "../../utils/skip-inline-comment.js";
import skipTrailingComment from "../../utils/skip-trailing-comment.js";
import { locEnd, locStart } from "../loc.js";
import {
CommentCheckFlags,
hasComment,
isArrayOrTupleExpression,
isNumericLiteral,
isObjectOrRecordExpression,
isSignedNumericLiteral,
shouldPrintComma,
} from "../utils/index.js";
import { printOptionalToken } from "./misc.js";
import { printTypeAnnotationProperty } from "./type-annotation.js";
/** @typedef {import("../../document/builders.js").Doc} Doc */
function printEmptyArrayElements(path, options, openBracket, closeBracket) {
const { node } = path;
const inexact = node.inexact ? "..." : "";
if (!hasComment(node, CommentCheckFlags.Dangling)) {
return [openBracket, inexact, closeBracket];
}
return group([
openBracket,
inexact,
printDanglingComments(path, options, { indent: true }),
softline,
closeBracket,
]);
}
/*
- `ArrayExpression`
- `TupleExpression`
- `ArrayPattern`
- `TSTupleType`(TypeScript)
- `TupleTypeAnnotation`(Flow)
*/
function printArray(path, options, print) {
const { node } = path;
/** @type{Doc[]} */
const parts = [];
const openBracket = node.type === "TupleExpression" ? "#[" : "[";
const closeBracket = "]";
const elementsProperty =
// TODO: Remove `types` when babel changes AST of `TupleTypeAnnotation`
node.type === "TupleTypeAnnotation" && node.types
? "types"
: node.type === "TSTupleType" || node.type === "TupleTypeAnnotation"
? "elementTypes"
: "elements";
const elements = node[elementsProperty];
if (elements.length === 0) {
parts.push(
printEmptyArrayElements(path, options, openBracket, closeBracket),
);
} else {
const lastElem = elements.at(-1);
const canHaveTrailingComma =
lastElem?.type !== "RestElement" && !node.inexact;
// JavaScript allows you to have empty elements in an array which
// changes its length based on the number of commas. The algorithm
// is that if the last argument is null, we need to force insert
// a comma to ensure JavaScript recognizes it.
// [,].length === 1
// [1,].length === 1
// [1,,].length === 2
//
// Note that getLast returns null if the array is empty, but
// we already check for an empty array just above so we are safe
const needsForcedTrailingComma = lastElem === null;
const groupId = Symbol("array");
const shouldBreak =
!options.__inJestEach &&
elements.length > 1 &&
elements.every((element, i, elements) => {
const elementType = element?.type;
if (
!isArrayOrTupleExpression(element) &&
!isObjectOrRecordExpression(element)
) {
return false;
}
const nextElement = elements[i + 1];
if (nextElement && elementType !== nextElement.type) {
return false;
}
const itemsKey = isArrayOrTupleExpression(element)
? "elements"
: "properties";
return element[itemsKey] && element[itemsKey].length > 1;
});
const shouldUseConciseFormatting = isConciselyPrintedArray(node, options);
const trailingComma = !canHaveTrailingComma
? ""
: needsForcedTrailingComma
? ","
: !shouldPrintComma(options)
? ""
: shouldUseConciseFormatting
? ifBreak(",", "", { groupId })
: ifBreak(",");
parts.push(
group(
[
openBracket,
indent([
softline,
shouldUseConciseFormatting
? printArrayElementsConcisely(path, options, print, trailingComma)
: [
printArrayElements(
path,
options,
elementsProperty,
node.inexact,
print,
),
trailingComma,
],
printDanglingComments(path, options),
]),
softline,
closeBracket,
],
{ shouldBreak, id: groupId },
),
);
}
parts.push(
printOptionalToken(path),
printTypeAnnotationProperty(path, print),
);
return parts;
}
function isConciselyPrintedArray(node, options) {
return (
isArrayOrTupleExpression(node) &&
node.elements.length > 1 &&
node.elements.every(
(element) =>
element &&
(isNumericLiteral(element) ||
(isSignedNumericLiteral(element) && !hasComment(element.argument))) &&
!hasComment(
element,
CommentCheckFlags.Trailing | CommentCheckFlags.Line,
(comment) =>
!hasNewline(options.originalText, locStart(comment), {
backwards: true,
}),
),
)
);
}
function isLineAfterElementEmpty({ node }, { originalText: text }) {
const skipComment = (idx) =>
skipInlineComment(text, skipTrailingComment(text, idx));
const skipToComma = (currentIdx) =>
text[currentIdx] === ","
? currentIdx
: skipToComma(skipComment(currentIdx + 1));
return isNextLineEmptyAfterIndex(text, skipToComma(locEnd(node)));
}
function printArrayElements(path, options, elementsProperty, inexact, print) {
const parts = [];
path.each(({ node, isLast }) => {
parts.push(node ? group(print()) : "");
if (!isLast || inexact) {
parts.push([
",",
line,
node && isLineAfterElementEmpty(path, options) ? softline : "",
]);
}
}, elementsProperty);
if (inexact) {
parts.push("...");
}
return parts;
}
function printArrayElementsConcisely(path, options, print, trailingComma) {
const parts = [];
path.each(({ isLast, next }) => {
parts.push([print(), isLast ? trailingComma : ","]);
if (!isLast) {
parts.push(
isLineAfterElementEmpty(path, options)
? [hardline, hardline]
: hasComment(next, CommentCheckFlags.Leading | CommentCheckFlags.Line)
? hardline
: line,
);
}
}, "elements");
return fill(parts);
}
export { isConciselyPrintedArray, printArray };