forked from babel/babel
/
getBindingIdentifiers.ts
143 lines (119 loc) 路 3.19 KB
/
getBindingIdentifiers.ts
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
import {
isExportDeclaration,
isIdentifier,
isDeclaration,
isFunctionDeclaration,
isFunctionExpression,
isExportAllDeclaration,
} from "../validators/generated";
import type * as t from "..";
export { getBindingIdentifiers as default };
function getBindingIdentifiers(
node: t.Node,
duplicates: true,
outerOnly?: boolean,
): Record<string, Array<t.Identifier>>;
function getBindingIdentifiers(
node: t.Node,
duplicates?: false,
outerOnly?: boolean,
): Record<string, t.Identifier>;
function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean,
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>>;
/**
* Return a list of binding identifiers associated with the input `node`.
*/
function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean,
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> {
let search = [].concat(node);
const ids = Object.create(null);
while (search.length) {
const id = search.shift();
if (!id) continue;
const keys = getBindingIdentifiers.keys[id.type];
if (isIdentifier(id)) {
if (duplicates) {
const _ids = (ids[id.name] = ids[id.name] || []);
_ids.push(id);
} else {
ids[id.name] = id;
}
continue;
}
if (isExportDeclaration(id) && !isExportAllDeclaration(id)) {
if (isDeclaration(id.declaration)) {
search.push(id.declaration);
}
continue;
}
if (outerOnly) {
if (isFunctionDeclaration(id)) {
search.push(id.id);
continue;
}
if (isFunctionExpression(id)) {
continue;
}
}
if (keys) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (id[key]) {
search = search.concat(id[key]);
}
}
}
}
// $FlowIssue Object.create() seems broken
return ids;
}
/**
* Mapping of types to their identifier keys.
*/
getBindingIdentifiers.keys = {
DeclareClass: ["id"],
DeclareFunction: ["id"],
DeclareModule: ["id"],
DeclareVariable: ["id"],
DeclareInterface: ["id"],
DeclareTypeAlias: ["id"],
DeclareOpaqueType: ["id"],
InterfaceDeclaration: ["id"],
TypeAlias: ["id"],
OpaqueType: ["id"],
CatchClause: ["param"],
LabeledStatement: ["label"],
UnaryExpression: ["argument"],
AssignmentExpression: ["left"],
ImportSpecifier: ["local"],
ImportNamespaceSpecifier: ["local"],
ImportDefaultSpecifier: ["local"],
ImportDeclaration: ["specifiers"],
ExportSpecifier: ["exported"],
ExportNamespaceSpecifier: ["exported"],
ExportDefaultSpecifier: ["exported"],
FunctionDeclaration: ["id", "params"],
FunctionExpression: ["id", "params"],
ArrowFunctionExpression: ["params"],
ObjectMethod: ["params"],
ClassMethod: ["params"],
ClassPrivateMethod: ["params"],
ForInStatement: ["left"],
ForOfStatement: ["left"],
ClassDeclaration: ["id"],
ClassExpression: ["id"],
RestElement: ["argument"],
UpdateExpression: ["argument"],
ObjectProperty: ["value"],
AssignmentPattern: ["left"],
ArrayPattern: ["elements"],
ObjectPattern: ["properties"],
VariableDeclaration: ["declarations"],
VariableDeclarator: ["id"],
};