UNPKG

2.6 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.getBindingIdentifiers = getBindingIdentifiers;
5exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
6
7var _index = require("./index");
8
9var t = _interopRequireWildcard(_index);
10
11function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
12
13function getBindingIdentifiers(node, duplicates, outerOnly) {
14 var search = [].concat(node);
15 var ids = Object.create(null);
16
17 while (search.length) {
18 var id = search.shift();
19 if (!id) continue;
20
21 var keys = t.getBindingIdentifiers.keys[id.type];
22
23 if (t.isIdentifier(id)) {
24 if (duplicates) {
25 var _ids = ids[id.name] = ids[id.name] || [];
26 _ids.push(id);
27 } else {
28 ids[id.name] = id;
29 }
30 continue;
31 }
32
33 if (t.isExportDeclaration(id)) {
34 if (t.isDeclaration(id.declaration)) {
35 search.push(id.declaration);
36 }
37 continue;
38 }
39
40 if (outerOnly) {
41 if (t.isFunctionDeclaration(id)) {
42 search.push(id.id);
43 continue;
44 }
45
46 if (t.isFunctionExpression(id)) {
47 continue;
48 }
49 }
50
51 if (keys) {
52 for (var i = 0; i < keys.length; i++) {
53 var key = keys[i];
54 if (id[key]) {
55 search = search.concat(id[key]);
56 }
57 }
58 }
59 }
60
61 return ids;
62}
63
64getBindingIdentifiers.keys = {
65 DeclareClass: ["id"],
66 DeclareFunction: ["id"],
67 DeclareModule: ["id"],
68 DeclareVariable: ["id"],
69 InterfaceDeclaration: ["id"],
70 TypeAlias: ["id"],
71
72 CatchClause: ["param"],
73 LabeledStatement: ["label"],
74 UnaryExpression: ["argument"],
75 AssignmentExpression: ["left"],
76
77 ImportSpecifier: ["local"],
78 ImportNamespaceSpecifier: ["local"],
79 ImportDefaultSpecifier: ["local"],
80 ImportDeclaration: ["specifiers"],
81
82 ExportSpecifier: ["exported"],
83 ExportNamespaceSpecifier: ["exported"],
84 ExportDefaultSpecifier: ["exported"],
85
86 FunctionDeclaration: ["id", "params"],
87 FunctionExpression: ["id", "params"],
88
89 ClassDeclaration: ["id"],
90 ClassExpression: ["id"],
91
92 RestElement: ["argument"],
93 UpdateExpression: ["argument"],
94
95 ObjectProperty: ["value"],
96
97 AssignmentPattern: ["left"],
98 ArrayPattern: ["elements"],
99 ObjectPattern: ["properties"],
100
101 VariableDeclaration: ["declarations"],
102 VariableDeclarator: ["id"]
103};
104
105function getOuterBindingIdentifiers(node, duplicates) {
106 return getBindingIdentifiers(node, duplicates, true);
107}
\No newline at end of file