forked from babel/babel
/
family.js
298 lines (264 loc) 路 7.79 KB
/
family.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
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
// @flow
// This file contains methods responsible for dealing with/retrieving children or siblings.
import type TraversalContext from "../index";
import NodePath from "./index";
import * as t from "@babel/types";
export function getOpposite(): ?NodePath {
if (this.key === "left") {
return this.getSibling("right");
} else if (this.key === "right") {
return this.getSibling("left");
}
}
function addCompletionRecords(path, paths) {
if (path) return paths.concat(path.getCompletionRecords());
return paths;
}
function findBreak(statements): ?NodePath {
let breakStatement;
if (!Array.isArray(statements)) {
statements = [statements];
}
for (const statement of statements) {
if (
statement.isDoExpression() ||
statement.isProgram() ||
statement.isBlockStatement() ||
statement.isCatchClause() ||
statement.isLabeledStatement()
) {
breakStatement = findBreak(statement.get("body"));
} else if (statement.isIfStatement()) {
breakStatement =
findBreak(statement.get("consequent")) ??
findBreak(statement.get("alternate"));
} else if (statement.isTryStatement()) {
breakStatement =
findBreak(statement.get("block")) ??
findBreak(statement.get("handler"));
} else if (statement.isBreakStatement()) {
breakStatement = statement;
}
if (breakStatement) {
return breakStatement;
}
}
return null;
}
function completionRecordForSwitch(cases, paths) {
let isLastCaseWithConsequent = true;
for (let i = cases.length - 1; i >= 0; i--) {
const switchCase = cases[i];
const consequent = switchCase.get("consequent");
let breakStatement = findBreak(consequent);
if (breakStatement) {
while (
breakStatement.key === 0 &&
breakStatement.parentPath.isBlockStatement()
) {
breakStatement = breakStatement.parentPath;
}
const prevSibling = breakStatement.getPrevSibling();
if (
breakStatement.key > 0 &&
(prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())
) {
paths = addCompletionRecords(prevSibling, paths);
breakStatement.remove();
} else {
breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode());
paths = addCompletionRecords(breakStatement, paths);
}
} else if (isLastCaseWithConsequent) {
const statementFinder = statement =>
!statement.isBlockStatement() ||
statement.get("body").some(statementFinder);
const hasConsequent = consequent.some(statementFinder);
if (hasConsequent) {
paths = addCompletionRecords(consequent[consequent.length - 1], paths);
isLastCaseWithConsequent = false;
}
}
}
return paths;
}
export function getCompletionRecords(): NodePath[] {
let paths = [];
if (this.isIfStatement()) {
paths = addCompletionRecords(this.get("consequent"), paths);
paths = addCompletionRecords(this.get("alternate"), paths);
} else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
paths = addCompletionRecords(this.get("body"), paths);
} else if (this.isProgram() || this.isBlockStatement()) {
paths = addCompletionRecords(this.get("body").pop(), paths);
} else if (this.isFunction()) {
return this.get("body").getCompletionRecords();
} else if (this.isTryStatement()) {
paths = addCompletionRecords(this.get("block"), paths);
paths = addCompletionRecords(this.get("handler"), paths);
} else if (this.isCatchClause()) {
paths = addCompletionRecords(this.get("body"), paths);
} else if (this.isSwitchStatement()) {
paths = completionRecordForSwitch(this.get("cases"), paths);
} else {
paths.push(this);
}
return paths;
}
export function getSibling(key: string): NodePath {
return NodePath.get({
parentPath: this.parentPath,
parent: this.parent,
container: this.container,
listKey: this.listKey,
key: key,
});
}
export function getPrevSibling(): NodePath {
return this.getSibling(this.key - 1);
}
export function getNextSibling(): NodePath {
return this.getSibling(this.key + 1);
}
export function getAllNextSiblings(): NodePath[] {
let _key = this.key;
let sibling = this.getSibling(++_key);
const siblings = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(++_key);
}
return siblings;
}
export function getAllPrevSiblings(): NodePath[] {
let _key = this.key;
let sibling = this.getSibling(--_key);
const siblings = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(--_key);
}
return siblings;
}
export function get(
key: string,
context?: boolean | TraversalContext,
): NodePath | NodePath[] {
if (context === true) context = this.context;
const parts = key.split(".");
if (parts.length === 1) {
// "foo"
return this._getKey(key, context);
} else {
// "foo.bar"
return this._getPattern(parts, context);
}
}
export function _getKey(
key: string,
context?: TraversalContext,
): NodePath | NodePath[] {
const node = this.node;
const container = node[key];
if (Array.isArray(container)) {
// requested a container so give them all the paths
return container.map((_, i) => {
return NodePath.get({
listKey: key,
parentPath: this,
parent: node,
container: container,
key: i,
}).setContext(context);
});
} else {
return NodePath.get({
parentPath: this,
parent: node,
container: node,
key: key,
}).setContext(context);
}
}
export function _getPattern(
parts: string[],
context?: TraversalContext,
): NodePath | NodePath[] {
let path = this;
for (const part of parts) {
if (part === ".") {
path = path.parentPath;
} else {
if (Array.isArray(path)) {
path = path[part];
} else {
path = path.get(part, context);
}
}
}
return path;
}
export function getBindingIdentifiers(duplicates?: boolean): Object {
return t.getBindingIdentifiers(this.node, duplicates);
}
export function getOuterBindingIdentifiers(duplicates?: boolean): Object {
return t.getOuterBindingIdentifiers(this.node, duplicates);
}
// original source - https://github.com/babel/babel/blob/master/packages/babel-types/src/retrievers.js
// path.getBindingIdentifiers returns nodes where the following re-implementation
// returns paths
export function getBindingIdentifierPaths(
duplicates?: boolean = false,
outerOnly?: boolean = false,
): { [string]: NodePath } {
const path = this;
let search = [].concat(path);
const ids = Object.create(null);
while (search.length) {
const id = search.shift();
if (!id) continue;
if (!id.node) continue;
const keys = t.getBindingIdentifiers.keys[id.node.type];
if (id.isIdentifier()) {
if (duplicates) {
const _ids = (ids[id.node.name] = ids[id.node.name] || []);
_ids.push(id);
} else {
ids[id.node.name] = id;
}
continue;
}
if (id.isExportDeclaration()) {
const declaration = id.get("declaration");
if (declaration.isDeclaration()) {
search.push(declaration);
}
continue;
}
if (outerOnly) {
if (id.isFunctionDeclaration()) {
search.push(id.get("id"));
continue;
}
if (id.isFunctionExpression()) {
continue;
}
}
if (keys) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const child = id.get(key);
if (Array.isArray(child) || child.node) {
search = search.concat(child);
}
}
}
}
// $FlowIssue Object.create() is object type
return ids;
}
export function getOuterBindingIdentifierPaths(
duplicates?: boolean,
): { [string]: NodePath } {
return this.getBindingIdentifierPaths(duplicates, true);
}