Repository navigation
Expand file tree
/
Copy pathrange-util.js
More file actions
262 lines (240 loc) · 6.07 KB
/
Copy pathrange-util.js
File metadata and controls
262 lines (240 loc) · 6.07 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
import assert from "node:assert";
import { getSortedChildNodes } from "./comments/attach.js";
const isJsonParser = ({ parser }) =>
parser === "json" ||
parser === "json5" ||
parser === "jsonc" ||
parser === "json-stringify";
function findCommonAncestor(startNodeAndParents, endNodeAndParents) {
const startNodeAndAncestors = [
startNodeAndParents.node,
...startNodeAndParents.parentNodes,
];
const endNodeAndAncestors = new Set([
endNodeAndParents.node,
...endNodeAndParents.parentNodes,
]);
return startNodeAndAncestors.find(
(node) =>
jsonSourceElements.has(node.type) && endNodeAndAncestors.has(node),
);
}
function dropRootParents(parents) {
const index = parents.findLastIndex(
(node) => node.type !== "Program" && node.type !== "File",
);
if (index === -1) {
return parents;
}
return parents.slice(0, index + 1);
}
function findSiblingAncestors(
startNodeAndParents,
endNodeAndParents,
{ locStart, locEnd },
) {
let resultStartNode = startNodeAndParents.node;
let resultEndNode = endNodeAndParents.node;
if (resultStartNode === resultEndNode) {
return {
startNode: resultStartNode,
endNode: resultEndNode,
};
}
const startNodeStart = locStart(startNodeAndParents.node);
for (const endParent of dropRootParents(endNodeAndParents.parentNodes)) {
if (locStart(endParent) >= startNodeStart) {
resultEndNode = endParent;
} else {
break;
}
}
const endNodeEnd = locEnd(endNodeAndParents.node);
for (const startParent of dropRootParents(startNodeAndParents.parentNodes)) {
if (locEnd(startParent) <= endNodeEnd) {
resultStartNode = startParent;
} else {
break;
}
if (resultStartNode === resultEndNode) {
break;
}
}
return {
startNode: resultStartNode,
endNode: resultEndNode,
};
}
function findNodeAtOffset(
node,
offset,
options,
predicate,
parentNodes = [],
type,
) {
const { locStart, locEnd } = options;
const start = locStart(node);
const end = locEnd(node);
if (
offset > end ||
offset < start ||
(type === "rangeEnd" && offset === start) ||
(type === "rangeStart" && offset === end)
) {
return;
}
for (const childNode of getSortedChildNodes(node, options)) {
const childResult = findNodeAtOffset(
childNode,
offset,
options,
predicate,
[node, ...parentNodes],
type,
);
if (childResult) {
return childResult;
}
}
if (!predicate || predicate(node, parentNodes[0])) {
return {
node,
parentNodes,
};
}
}
// See https://www.ecma-international.org/ecma-262/5.1/#sec-A.5
function isJsSourceElement(type, parentType) {
return (
parentType !== "DeclareExportDeclaration" &&
type !== "TypeParameterDeclaration" &&
(type === "Directive" ||
type === "TypeAlias" ||
type === "TSExportAssignment" ||
type.startsWith("Declare") ||
type.startsWith("TSDeclare") ||
type.endsWith("Statement") ||
type.endsWith("Declaration"))
);
}
const jsonSourceElements = new Set([
"JsonRoot",
"ObjectExpression",
"ArrayExpression",
"StringLiteral",
"NumericLiteral",
"BooleanLiteral",
"NullLiteral",
"UnaryExpression",
"TemplateLiteral",
]);
const graphqlSourceElements = new Set([
"OperationDefinition",
"FragmentDefinition",
"VariableDefinition",
"TypeExtensionDefinition",
"ObjectTypeDefinition",
"FieldDefinition",
"DirectiveDefinition",
"EnumTypeDefinition",
"EnumValueDefinition",
"InputValueDefinition",
"InputObjectTypeDefinition",
"SchemaDefinition",
"OperationTypeDefinition",
"InterfaceTypeDefinition",
"UnionTypeDefinition",
"ScalarTypeDefinition",
]);
function isSourceElement(opts, node, parentNode) {
/* c8 ignore next 3 */
if (!node) {
return false;
}
switch (opts.parser) {
case "flow":
case "babel":
case "babel-flow":
case "babel-ts":
case "typescript":
case "acorn":
case "espree":
case "meriyah":
case "__babel_estree":
return isJsSourceElement(node.type, parentNode?.type);
case "json":
case "json5":
case "jsonc":
case "json-stringify":
return jsonSourceElements.has(node.type);
case "graphql":
return graphqlSourceElements.has(node.kind);
case "vue":
return node.tag !== "root";
}
return false;
}
function calculateRange(text, opts, ast) {
let { rangeStart: start, rangeEnd: end, locStart, locEnd } = opts;
assert.ok(end > start);
// Contract the range so that it has non-whitespace characters at its endpoints.
// This ensures we can format a range that doesn't end on a node.
const firstNonWhitespaceCharacterIndex = text.slice(start, end).search(/\S/u);
const isAllWhitespace = firstNonWhitespaceCharacterIndex === -1;
if (!isAllWhitespace) {
start += firstNonWhitespaceCharacterIndex;
for (; end > start; --end) {
if (/\S/u.test(text[end - 1])) {
break;
}
}
}
const startNodeAndParents = findNodeAtOffset(
ast,
start,
opts,
(node, parentNode) => isSourceElement(opts, node, parentNode),
[],
"rangeStart",
);
const endNodeAndParents =
// No need find Node at `end`, it will be the same as `startNodeAndParents`
isAllWhitespace
? startNodeAndParents
: findNodeAtOffset(
ast,
end,
opts,
(node) => isSourceElement(opts, node),
[],
"rangeEnd",
);
if (!startNodeAndParents || !endNodeAndParents) {
return {
rangeStart: 0,
rangeEnd: 0,
};
}
let startNode;
let endNode;
if (isJsonParser(opts)) {
const commonAncestor = findCommonAncestor(
startNodeAndParents,
endNodeAndParents,
);
startNode = commonAncestor;
endNode = commonAncestor;
} else {
({ startNode, endNode } = findSiblingAncestors(
startNodeAndParents,
endNodeAndParents,
opts,
));
}
return {
rangeStart: Math.min(locStart(startNode), locStart(endNode)),
rangeEnd: Math.max(locEnd(startNode), locEnd(endNode)),
};
}
export { calculateRange, findNodeAtOffset };