@babel/traverse 7.1.0 → 7.23.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/cache.js +27 -7
- package/lib/cache.js.map +1 -0
- package/lib/context.js +22 -59
- package/lib/context.js.map +1 -0
- package/lib/hub.js +2 -6
- package/lib/hub.js.map +1 -0
- package/lib/index.js +42 -77
- package/lib/index.js.map +1 -0
- package/lib/path/ancestry.js +15 -62
- package/lib/path/ancestry.js.map +1 -0
- package/lib/path/comments.js +31 -24
- package/lib/path/comments.js.map +1 -0
- package/lib/path/context.js +68 -91
- package/lib/path/context.js.map +1 -0
- package/lib/path/conversion.js +275 -273
- package/lib/path/conversion.js.map +1 -0
- package/lib/path/evaluation.js +84 -149
- package/lib/path/evaluation.js.map +1 -0
- package/lib/path/family.js +189 -94
- package/lib/path/family.js.map +1 -0
- package/lib/path/index.js +106 -132
- package/lib/path/index.js.map +1 -0
- package/lib/path/inference/index.js +81 -64
- package/lib/path/inference/index.js.map +1 -0
- package/lib/path/inference/inferer-reference.js +22 -52
- package/lib/path/inference/inferer-reference.js.map +1 -0
- package/lib/path/inference/inferers.js +110 -125
- package/lib/path/inference/inferers.js.map +1 -0
- package/lib/path/inference/util.js +30 -0
- package/lib/path/inference/util.js.map +1 -0
- package/lib/path/introspection.js +182 -168
- package/lib/path/introspection.js.map +1 -0
- package/lib/path/lib/hoister.js +37 -54
- package/lib/path/lib/hoister.js.map +1 -0
- package/lib/path/lib/removal-hooks.js +4 -4
- package/lib/path/lib/removal-hooks.js.map +1 -0
- package/lib/path/lib/virtual-types-validator.js +161 -0
- package/lib/path/lib/virtual-types-validator.js.map +1 -0
- package/lib/path/lib/virtual-types.js +21 -189
- package/lib/path/lib/virtual-types.js.map +1 -0
- package/lib/path/modification.js +103 -98
- package/lib/path/modification.js.map +1 -0
- package/lib/path/removal.js +18 -23
- package/lib/path/removal.js.map +1 -0
- package/lib/path/replacement.js +91 -144
- package/lib/path/replacement.js.map +1 -0
- package/lib/scope/binding.js +28 -16
- package/lib/scope/binding.js.map +1 -0
- package/lib/scope/index.js +407 -414
- package/lib/scope/index.js.map +1 -0
- package/lib/scope/lib/renamer.js +45 -70
- package/lib/scope/lib/renamer.js.map +1 -0
- package/lib/traverse-node.js +29 -0
- package/lib/traverse-node.js.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/visitors.js +77 -113
- package/lib/visitors.js.map +1 -0
- package/package.json +28 -17
package/lib/scope/index.js
CHANGED
@@ -4,375 +4,424 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
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
|
-
return data;
|
62
|
-
}
|
63
|
-
|
64
|
-
var _cache = require("../cache");
|
65
|
-
|
66
|
-
function _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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
67
|
-
|
68
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
69
|
-
|
7
|
+
var _renamer = require("./lib/renamer.js");
|
8
|
+
var _index = require("../index.js");
|
9
|
+
var _binding = require("./binding.js");
|
10
|
+
var _globals = require("globals");
|
11
|
+
var _t = require("@babel/types");
|
12
|
+
var t = _t;
|
13
|
+
var _cache = require("../cache.js");
|
14
|
+
var _visitors = require("../visitors.js");
|
15
|
+
const {
|
16
|
+
NOT_LOCAL_BINDING,
|
17
|
+
callExpression,
|
18
|
+
cloneNode,
|
19
|
+
getBindingIdentifiers,
|
20
|
+
identifier,
|
21
|
+
isArrayExpression,
|
22
|
+
isBinary,
|
23
|
+
isClass,
|
24
|
+
isClassBody,
|
25
|
+
isClassDeclaration,
|
26
|
+
isExportAllDeclaration,
|
27
|
+
isExportDefaultDeclaration,
|
28
|
+
isExportNamedDeclaration,
|
29
|
+
isFunctionDeclaration,
|
30
|
+
isIdentifier,
|
31
|
+
isImportDeclaration,
|
32
|
+
isLiteral,
|
33
|
+
isMethod,
|
34
|
+
isModuleSpecifier,
|
35
|
+
isNullLiteral,
|
36
|
+
isObjectExpression,
|
37
|
+
isProperty,
|
38
|
+
isPureish,
|
39
|
+
isRegExpLiteral,
|
40
|
+
isSuper,
|
41
|
+
isTaggedTemplateExpression,
|
42
|
+
isTemplateLiteral,
|
43
|
+
isThisExpression,
|
44
|
+
isUnaryExpression,
|
45
|
+
isVariableDeclaration,
|
46
|
+
matchesPattern,
|
47
|
+
memberExpression,
|
48
|
+
numericLiteral,
|
49
|
+
toIdentifier,
|
50
|
+
variableDeclaration,
|
51
|
+
variableDeclarator,
|
52
|
+
isRecordExpression,
|
53
|
+
isTupleExpression,
|
54
|
+
isObjectProperty,
|
55
|
+
isTopicReference,
|
56
|
+
isMetaProperty,
|
57
|
+
isPrivateName,
|
58
|
+
isExportDeclaration,
|
59
|
+
buildUndefinedNode
|
60
|
+
} = _t;
|
70
61
|
function gatherNodeParts(node, parts) {
|
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
|
-
|
62
|
+
switch (node == null ? void 0 : node.type) {
|
63
|
+
default:
|
64
|
+
if (isImportDeclaration(node) || isExportDeclaration(node)) {
|
65
|
+
var _node$specifiers;
|
66
|
+
if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
|
67
|
+
gatherNodeParts(node.source, parts);
|
68
|
+
} else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && (_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
|
69
|
+
for (const e of node.specifiers) gatherNodeParts(e, parts);
|
70
|
+
} else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
|
71
|
+
gatherNodeParts(node.declaration, parts);
|
72
|
+
}
|
73
|
+
} else if (isModuleSpecifier(node)) {
|
74
|
+
gatherNodeParts(node.local, parts);
|
75
|
+
} else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
|
76
|
+
parts.push(node.value);
|
77
|
+
}
|
78
|
+
break;
|
79
|
+
case "MemberExpression":
|
80
|
+
case "OptionalMemberExpression":
|
81
|
+
case "JSXMemberExpression":
|
82
|
+
gatherNodeParts(node.object, parts);
|
83
|
+
gatherNodeParts(node.property, parts);
|
84
|
+
break;
|
85
|
+
case "Identifier":
|
86
|
+
case "JSXIdentifier":
|
87
|
+
parts.push(node.name);
|
88
|
+
break;
|
89
|
+
case "CallExpression":
|
90
|
+
case "OptionalCallExpression":
|
91
|
+
case "NewExpression":
|
92
|
+
gatherNodeParts(node.callee, parts);
|
93
|
+
break;
|
94
|
+
case "ObjectExpression":
|
95
|
+
case "ObjectPattern":
|
96
|
+
for (const e of node.properties) {
|
97
|
+
gatherNodeParts(e, parts);
|
98
|
+
}
|
99
|
+
break;
|
100
|
+
case "SpreadElement":
|
101
|
+
case "RestElement":
|
102
|
+
gatherNodeParts(node.argument, parts);
|
103
|
+
break;
|
104
|
+
case "ObjectProperty":
|
105
|
+
case "ObjectMethod":
|
106
|
+
case "ClassProperty":
|
107
|
+
case "ClassMethod":
|
108
|
+
case "ClassPrivateProperty":
|
109
|
+
case "ClassPrivateMethod":
|
110
|
+
gatherNodeParts(node.key, parts);
|
111
|
+
break;
|
112
|
+
case "ThisExpression":
|
113
|
+
parts.push("this");
|
114
|
+
break;
|
115
|
+
case "Super":
|
116
|
+
parts.push("super");
|
117
|
+
break;
|
118
|
+
case "Import":
|
119
|
+
parts.push("import");
|
120
|
+
break;
|
121
|
+
case "DoExpression":
|
122
|
+
parts.push("do");
|
123
|
+
break;
|
124
|
+
case "YieldExpression":
|
125
|
+
parts.push("yield");
|
126
|
+
gatherNodeParts(node.argument, parts);
|
127
|
+
break;
|
128
|
+
case "AwaitExpression":
|
129
|
+
parts.push("await");
|
130
|
+
gatherNodeParts(node.argument, parts);
|
131
|
+
break;
|
132
|
+
case "AssignmentExpression":
|
133
|
+
gatherNodeParts(node.left, parts);
|
134
|
+
break;
|
135
|
+
case "VariableDeclarator":
|
136
|
+
gatherNodeParts(node.id, parts);
|
137
|
+
break;
|
138
|
+
case "FunctionExpression":
|
139
|
+
case "FunctionDeclaration":
|
140
|
+
case "ClassExpression":
|
141
|
+
case "ClassDeclaration":
|
142
|
+
gatherNodeParts(node.id, parts);
|
143
|
+
break;
|
144
|
+
case "PrivateName":
|
145
|
+
gatherNodeParts(node.id, parts);
|
146
|
+
break;
|
147
|
+
case "ParenthesizedExpression":
|
148
|
+
gatherNodeParts(node.expression, parts);
|
149
|
+
break;
|
150
|
+
case "UnaryExpression":
|
151
|
+
case "UpdateExpression":
|
152
|
+
gatherNodeParts(node.argument, parts);
|
153
|
+
break;
|
154
|
+
case "MetaProperty":
|
155
|
+
gatherNodeParts(node.meta, parts);
|
156
|
+
gatherNodeParts(node.property, parts);
|
157
|
+
break;
|
158
|
+
case "JSXElement":
|
159
|
+
gatherNodeParts(node.openingElement, parts);
|
160
|
+
break;
|
161
|
+
case "JSXOpeningElement":
|
162
|
+
gatherNodeParts(node.name, parts);
|
163
|
+
break;
|
164
|
+
case "JSXFragment":
|
165
|
+
gatherNodeParts(node.openingFragment, parts);
|
166
|
+
break;
|
167
|
+
case "JSXOpeningFragment":
|
168
|
+
parts.push("Fragment");
|
169
|
+
break;
|
170
|
+
case "JSXNamespacedName":
|
171
|
+
gatherNodeParts(node.namespace, parts);
|
172
|
+
gatherNodeParts(node.name, parts);
|
173
|
+
break;
|
102
174
|
}
|
103
175
|
}
|
104
|
-
|
105
176
|
const collectorVisitor = {
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
177
|
+
ForStatement(path) {
|
178
|
+
const declar = path.get("init");
|
179
|
+
if (declar.isVar()) {
|
180
|
+
const {
|
181
|
+
scope
|
182
|
+
} = path;
|
183
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
184
|
+
parentScope.registerBinding("var", declar);
|
114
185
|
}
|
115
186
|
},
|
116
|
-
|
117
187
|
Declaration(path) {
|
118
188
|
if (path.isBlockScoped()) return;
|
119
|
-
|
120
|
-
if (path.isExportDeclaration()
|
121
|
-
return;
|
122
|
-
}
|
123
|
-
|
189
|
+
if (path.isImportDeclaration()) return;
|
190
|
+
if (path.isExportDeclaration()) return;
|
124
191
|
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
125
192
|
parent.registerDeclaration(path);
|
126
193
|
},
|
127
|
-
|
194
|
+
ImportDeclaration(path) {
|
195
|
+
const parent = path.scope.getBlockParent();
|
196
|
+
parent.registerDeclaration(path);
|
197
|
+
},
|
128
198
|
ReferencedIdentifier(path, state) {
|
129
199
|
state.references.push(path);
|
130
200
|
},
|
131
|
-
|
132
201
|
ForXStatement(path, state) {
|
133
202
|
const left = path.get("left");
|
134
|
-
|
135
203
|
if (left.isPattern() || left.isIdentifier()) {
|
136
204
|
state.constantViolations.push(path);
|
205
|
+
} else if (left.isVar()) {
|
206
|
+
const {
|
207
|
+
scope
|
208
|
+
} = path;
|
209
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
210
|
+
parentScope.registerBinding("var", left);
|
137
211
|
}
|
138
212
|
},
|
139
|
-
|
140
213
|
ExportDeclaration: {
|
141
214
|
exit(path) {
|
142
215
|
const {
|
143
216
|
node,
|
144
217
|
scope
|
145
218
|
} = path;
|
219
|
+
if (isExportAllDeclaration(node)) return;
|
146
220
|
const declar = node.declaration;
|
147
|
-
|
148
|
-
if (t().isClassDeclaration(declar) || t().isFunctionDeclaration(declar)) {
|
221
|
+
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
149
222
|
const id = declar.id;
|
150
223
|
if (!id) return;
|
151
224
|
const binding = scope.getBinding(id.name);
|
152
|
-
|
153
|
-
} else if (
|
225
|
+
binding == null ? void 0 : binding.reference(path);
|
226
|
+
} else if (isVariableDeclaration(declar)) {
|
154
227
|
for (const decl of declar.declarations) {
|
155
|
-
const
|
156
|
-
|
157
|
-
for (const name in ids) {
|
228
|
+
for (const name of Object.keys(getBindingIdentifiers(decl))) {
|
158
229
|
const binding = scope.getBinding(name);
|
159
|
-
|
230
|
+
binding == null ? void 0 : binding.reference(path);
|
160
231
|
}
|
161
232
|
}
|
162
233
|
}
|
163
234
|
}
|
164
|
-
|
165
235
|
},
|
166
|
-
|
167
236
|
LabeledStatement(path) {
|
168
|
-
path.scope.getProgramParent().addGlobal(path.node);
|
169
237
|
path.scope.getBlockParent().registerDeclaration(path);
|
170
238
|
},
|
171
|
-
|
172
239
|
AssignmentExpression(path, state) {
|
173
240
|
state.assignments.push(path);
|
174
241
|
},
|
175
|
-
|
176
242
|
UpdateExpression(path, state) {
|
177
243
|
state.constantViolations.push(path);
|
178
244
|
},
|
179
|
-
|
180
245
|
UnaryExpression(path, state) {
|
181
246
|
if (path.node.operator === "delete") {
|
182
247
|
state.constantViolations.push(path);
|
183
248
|
}
|
184
249
|
},
|
185
|
-
|
186
250
|
BlockScoped(path) {
|
187
251
|
let scope = path.scope;
|
188
252
|
if (scope.path === path) scope = scope.parent;
|
189
|
-
scope.getBlockParent()
|
253
|
+
const parent = scope.getBlockParent();
|
254
|
+
parent.registerDeclaration(path);
|
255
|
+
if (path.isClassDeclaration() && path.node.id) {
|
256
|
+
const id = path.node.id;
|
257
|
+
const name = id.name;
|
258
|
+
path.scope.bindings[name] = path.scope.parent.getBinding(name);
|
259
|
+
}
|
190
260
|
},
|
191
|
-
|
192
|
-
|
193
|
-
const id = path.node.id;
|
194
|
-
if (!id) return;
|
195
|
-
const name = id.name;
|
196
|
-
path.scope.bindings[name] = path.scope.getBinding(name);
|
261
|
+
CatchClause(path) {
|
262
|
+
path.scope.registerBinding("let", path);
|
197
263
|
},
|
198
|
-
|
199
|
-
|
200
|
-
const
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
264
|
+
Function(path) {
|
265
|
+
const params = path.get("params");
|
266
|
+
for (const param of params) {
|
267
|
+
path.scope.registerBinding("param", param);
|
268
|
+
}
|
269
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
270
|
+
path.scope.registerBinding("local", path.get("id"), path);
|
271
|
+
}
|
272
|
+
},
|
273
|
+
ClassExpression(path) {
|
274
|
+
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
275
|
+
path.scope.registerBinding("local", path);
|
206
276
|
}
|
207
277
|
}
|
208
|
-
|
209
278
|
};
|
210
279
|
let uid = 0;
|
211
|
-
|
212
280
|
class Scope {
|
213
281
|
constructor(path) {
|
282
|
+
this.uid = void 0;
|
283
|
+
this.path = void 0;
|
284
|
+
this.block = void 0;
|
285
|
+
this.labels = void 0;
|
286
|
+
this.inited = void 0;
|
287
|
+
this.bindings = void 0;
|
288
|
+
this.references = void 0;
|
289
|
+
this.globals = void 0;
|
290
|
+
this.uids = void 0;
|
291
|
+
this.data = void 0;
|
292
|
+
this.crawling = void 0;
|
214
293
|
const {
|
215
294
|
node
|
216
295
|
} = path;
|
217
|
-
|
218
296
|
const cached = _cache.scope.get(node);
|
219
|
-
|
220
|
-
if (cached && cached.path === path) {
|
297
|
+
if ((cached == null ? void 0 : cached.path) === path) {
|
221
298
|
return cached;
|
222
299
|
}
|
223
|
-
|
224
300
|
_cache.scope.set(node, this);
|
225
|
-
|
226
301
|
this.uid = uid++;
|
227
302
|
this.block = node;
|
228
303
|
this.path = path;
|
229
304
|
this.labels = new Map();
|
305
|
+
this.inited = false;
|
230
306
|
}
|
231
|
-
|
232
307
|
get parent() {
|
233
|
-
|
234
|
-
|
308
|
+
var _parent;
|
309
|
+
let parent,
|
310
|
+
path = this.path;
|
311
|
+
do {
|
312
|
+
const shouldSkip = path.key === "key" || path.listKey === "decorators";
|
313
|
+
path = path.parentPath;
|
314
|
+
if (shouldSkip && path.isMethod()) path = path.parentPath;
|
315
|
+
if (path && path.isScope()) parent = path;
|
316
|
+
} while (path && !parent);
|
317
|
+
return (_parent = parent) == null ? void 0 : _parent.scope;
|
235
318
|
}
|
236
|
-
|
237
319
|
get parentBlock() {
|
238
320
|
return this.path.parent;
|
239
321
|
}
|
240
|
-
|
241
322
|
get hub() {
|
242
323
|
return this.path.hub;
|
243
324
|
}
|
244
|
-
|
245
325
|
traverse(node, opts, state) {
|
246
326
|
(0, _index.default)(node, opts, this, state, this.path);
|
247
327
|
}
|
248
|
-
|
249
328
|
generateDeclaredUidIdentifier(name) {
|
250
329
|
const id = this.generateUidIdentifier(name);
|
251
330
|
this.push({
|
252
331
|
id
|
253
332
|
});
|
254
|
-
return
|
333
|
+
return cloneNode(id);
|
255
334
|
}
|
256
|
-
|
257
335
|
generateUidIdentifier(name) {
|
258
|
-
return
|
336
|
+
return identifier(this.generateUid(name));
|
259
337
|
}
|
260
|
-
|
261
338
|
generateUid(name = "temp") {
|
262
|
-
name =
|
339
|
+
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
263
340
|
let uid;
|
264
|
-
let i =
|
265
|
-
|
341
|
+
let i = 1;
|
266
342
|
do {
|
267
343
|
uid = this._generateUid(name, i);
|
268
344
|
i++;
|
269
345
|
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
270
|
-
|
271
346
|
const program = this.getProgramParent();
|
272
347
|
program.references[uid] = true;
|
273
348
|
program.uids[uid] = true;
|
274
349
|
return uid;
|
275
350
|
}
|
276
|
-
|
277
351
|
_generateUid(name, i) {
|
278
352
|
let id = name;
|
279
353
|
if (i > 1) id += i;
|
280
354
|
return `_${id}`;
|
281
355
|
}
|
282
|
-
|
283
|
-
generateUidBasedOnNode(parent, defaultName) {
|
284
|
-
let node = parent;
|
285
|
-
|
286
|
-
if (t().isAssignmentExpression(parent)) {
|
287
|
-
node = parent.left;
|
288
|
-
} else if (t().isVariableDeclarator(parent)) {
|
289
|
-
node = parent.id;
|
290
|
-
} else if (t().isObjectProperty(node) || t().isObjectMethod(node)) {
|
291
|
-
node = node.key;
|
292
|
-
}
|
293
|
-
|
356
|
+
generateUidBasedOnNode(node, defaultName) {
|
294
357
|
const parts = [];
|
295
358
|
gatherNodeParts(node, parts);
|
296
359
|
let id = parts.join("$");
|
297
360
|
id = id.replace(/^_/, "") || defaultName || "ref";
|
298
361
|
return this.generateUid(id.slice(0, 20));
|
299
362
|
}
|
300
|
-
|
301
|
-
|
302
|
-
return t().identifier(this.generateUidBasedOnNode(parent, defaultName));
|
363
|
+
generateUidIdentifierBasedOnNode(node, defaultName) {
|
364
|
+
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
303
365
|
}
|
304
|
-
|
305
366
|
isStatic(node) {
|
306
|
-
if (
|
367
|
+
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
|
307
368
|
return true;
|
308
369
|
}
|
309
|
-
|
310
|
-
if (t().isIdentifier(node)) {
|
370
|
+
if (isIdentifier(node)) {
|
311
371
|
const binding = this.getBinding(node.name);
|
312
|
-
|
313
372
|
if (binding) {
|
314
373
|
return binding.constant;
|
315
374
|
} else {
|
316
375
|
return this.hasBinding(node.name);
|
317
376
|
}
|
318
377
|
}
|
319
|
-
|
320
378
|
return false;
|
321
379
|
}
|
322
|
-
|
323
380
|
maybeGenerateMemoised(node, dontPush) {
|
324
381
|
if (this.isStatic(node)) {
|
325
382
|
return null;
|
326
383
|
} else {
|
327
384
|
const id = this.generateUidIdentifierBasedOnNode(node);
|
328
|
-
|
329
385
|
if (!dontPush) {
|
330
386
|
this.push({
|
331
387
|
id
|
332
388
|
});
|
333
|
-
return
|
389
|
+
return cloneNode(id);
|
334
390
|
}
|
335
|
-
|
336
391
|
return id;
|
337
392
|
}
|
338
393
|
}
|
339
|
-
|
340
394
|
checkBlockScopedCollisions(local, kind, name, id) {
|
341
395
|
if (kind === "param") return;
|
342
396
|
if (local.kind === "local") return;
|
343
|
-
|
344
|
-
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const");
|
345
|
-
|
397
|
+
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
346
398
|
if (duplicate) {
|
347
399
|
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
348
400
|
}
|
349
401
|
}
|
350
|
-
|
351
|
-
rename(oldName, newName, block) {
|
402
|
+
rename(oldName, newName) {
|
352
403
|
const binding = this.getBinding(oldName);
|
353
|
-
|
354
404
|
if (binding) {
|
355
|
-
newName
|
356
|
-
|
405
|
+
newName || (newName = this.generateUidIdentifier(oldName).name);
|
406
|
+
const renamer = new _renamer.default(binding, oldName, newName);
|
407
|
+
{
|
408
|
+
renamer.rename(arguments[2]);
|
409
|
+
}
|
357
410
|
}
|
358
411
|
}
|
359
|
-
|
360
412
|
_renameFromMap(map, oldName, newName, value) {
|
361
413
|
if (map[oldName]) {
|
362
414
|
map[newName] = value;
|
363
415
|
map[oldName] = null;
|
364
416
|
}
|
365
417
|
}
|
366
|
-
|
367
418
|
dump() {
|
368
|
-
const sep =
|
419
|
+
const sep = "-".repeat(60);
|
369
420
|
console.log(sep);
|
370
421
|
let scope = this;
|
371
|
-
|
372
422
|
do {
|
373
423
|
console.log("#", scope.block.type);
|
374
|
-
|
375
|
-
for (const name in scope.bindings) {
|
424
|
+
for (const name of Object.keys(scope.bindings)) {
|
376
425
|
const binding = scope.bindings[name];
|
377
426
|
console.log(" -", name, {
|
378
427
|
constant: binding.constant,
|
@@ -382,56 +431,48 @@ class Scope {
|
|
382
431
|
});
|
383
432
|
}
|
384
433
|
} while (scope = scope.parent);
|
385
|
-
|
386
434
|
console.log(sep);
|
387
435
|
}
|
388
|
-
|
389
|
-
|
390
|
-
if (t().isIdentifier(node)) {
|
436
|
+
toArray(node, i, arrayLikeIsIterable) {
|
437
|
+
if (isIdentifier(node)) {
|
391
438
|
const binding = this.getBinding(node.name);
|
392
|
-
|
393
|
-
if (binding && binding.constant && binding.path.isGenericType("Array")) {
|
439
|
+
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
394
440
|
return node;
|
395
441
|
}
|
396
442
|
}
|
397
|
-
|
398
|
-
if (t().isArrayExpression(node)) {
|
443
|
+
if (isArrayExpression(node)) {
|
399
444
|
return node;
|
400
445
|
}
|
401
|
-
|
402
|
-
if (t().isIdentifier(node, {
|
446
|
+
if (isIdentifier(node, {
|
403
447
|
name: "arguments"
|
404
448
|
})) {
|
405
|
-
return
|
449
|
+
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
406
450
|
}
|
407
|
-
|
408
451
|
let helperName;
|
409
452
|
const args = [node];
|
410
|
-
|
411
453
|
if (i === true) {
|
412
454
|
helperName = "toConsumableArray";
|
413
|
-
} else if (i) {
|
414
|
-
args.push(
|
455
|
+
} else if (typeof i === "number") {
|
456
|
+
args.push(numericLiteral(i));
|
415
457
|
helperName = "slicedToArray";
|
416
458
|
} else {
|
417
459
|
helperName = "toArray";
|
418
460
|
}
|
419
|
-
|
420
|
-
|
461
|
+
if (arrayLikeIsIterable) {
|
462
|
+
args.unshift(this.hub.addHelper(helperName));
|
463
|
+
helperName = "maybeArrayLike";
|
464
|
+
}
|
465
|
+
return callExpression(this.hub.addHelper(helperName), args);
|
421
466
|
}
|
422
|
-
|
423
467
|
hasLabel(name) {
|
424
468
|
return !!this.getLabel(name);
|
425
469
|
}
|
426
|
-
|
427
470
|
getLabel(name) {
|
428
471
|
return this.labels.get(name);
|
429
472
|
}
|
430
|
-
|
431
473
|
registerLabel(path) {
|
432
474
|
this.labels.set(path.node.label.name, path);
|
433
475
|
}
|
434
|
-
|
435
476
|
registerDeclaration(path) {
|
436
477
|
if (path.isLabeledStatement()) {
|
437
478
|
this.registerLabel(path);
|
@@ -439,21 +480,24 @@ class Scope {
|
|
439
480
|
this.registerBinding("hoisted", path.get("id"), path);
|
440
481
|
} else if (path.isVariableDeclaration()) {
|
441
482
|
const declarations = path.get("declarations");
|
442
|
-
|
483
|
+
const {
|
484
|
+
kind
|
485
|
+
} = path.node;
|
443
486
|
for (const declar of declarations) {
|
444
|
-
this.registerBinding(
|
487
|
+
this.registerBinding(kind === "using" || kind === "await using" ? "const" : kind, declar);
|
445
488
|
}
|
446
489
|
} else if (path.isClassDeclaration()) {
|
490
|
+
if (path.node.declare) return;
|
447
491
|
this.registerBinding("let", path);
|
448
492
|
} else if (path.isImportDeclaration()) {
|
493
|
+
const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
|
449
494
|
const specifiers = path.get("specifiers");
|
450
|
-
|
451
495
|
for (const specifier of specifiers) {
|
452
|
-
|
496
|
+
const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
|
497
|
+
this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
|
453
498
|
}
|
454
499
|
} else if (path.isExportDeclaration()) {
|
455
500
|
const declar = path.get("declaration");
|
456
|
-
|
457
501
|
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
|
458
502
|
this.registerDeclaration(declar);
|
459
503
|
}
|
@@ -461,51 +505,35 @@ class Scope {
|
|
461
505
|
this.registerBinding("unknown", path);
|
462
506
|
}
|
463
507
|
}
|
464
|
-
|
465
508
|
buildUndefinedNode() {
|
466
|
-
|
467
|
-
return t().unaryExpression("void", t().numericLiteral(0), true);
|
468
|
-
} else {
|
469
|
-
return t().identifier("undefined");
|
470
|
-
}
|
509
|
+
return buildUndefinedNode();
|
471
510
|
}
|
472
|
-
|
473
511
|
registerConstantViolation(path) {
|
474
512
|
const ids = path.getBindingIdentifiers();
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
if (binding) binding.reassign(path);
|
513
|
+
for (const name of Object.keys(ids)) {
|
514
|
+
var _this$getBinding;
|
515
|
+
(_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.reassign(path);
|
479
516
|
}
|
480
517
|
}
|
481
|
-
|
482
518
|
registerBinding(kind, path, bindingPath = path) {
|
483
519
|
if (!kind) throw new ReferenceError("no `kind`");
|
484
|
-
|
485
520
|
if (path.isVariableDeclaration()) {
|
486
521
|
const declarators = path.get("declarations");
|
487
|
-
|
488
522
|
for (const declar of declarators) {
|
489
523
|
this.registerBinding(kind, declar);
|
490
524
|
}
|
491
|
-
|
492
525
|
return;
|
493
526
|
}
|
494
|
-
|
495
527
|
const parent = this.getProgramParent();
|
496
|
-
const ids = path.
|
497
|
-
|
498
|
-
|
528
|
+
const ids = path.getOuterBindingIdentifiers(true);
|
529
|
+
for (const name of Object.keys(ids)) {
|
530
|
+
parent.references[name] = true;
|
499
531
|
for (const id of ids[name]) {
|
500
532
|
const local = this.getOwnBinding(name);
|
501
|
-
|
502
533
|
if (local) {
|
503
534
|
if (local.identifier === id) continue;
|
504
535
|
this.checkBlockScopedCollisions(local, kind, name, id);
|
505
536
|
}
|
506
|
-
|
507
|
-
parent.references[name] = true;
|
508
|
-
|
509
537
|
if (local) {
|
510
538
|
this.registerConstantViolation(bindingPath);
|
511
539
|
} else {
|
@@ -519,121 +547,115 @@ class Scope {
|
|
519
547
|
}
|
520
548
|
}
|
521
549
|
}
|
522
|
-
|
523
550
|
addGlobal(node) {
|
524
551
|
this.globals[node.name] = node;
|
525
552
|
}
|
526
|
-
|
527
553
|
hasUid(name) {
|
528
554
|
let scope = this;
|
529
|
-
|
530
555
|
do {
|
531
556
|
if (scope.uids[name]) return true;
|
532
557
|
} while (scope = scope.parent);
|
533
|
-
|
534
558
|
return false;
|
535
559
|
}
|
536
|
-
|
537
560
|
hasGlobal(name) {
|
538
561
|
let scope = this;
|
539
|
-
|
540
562
|
do {
|
541
563
|
if (scope.globals[name]) return true;
|
542
564
|
} while (scope = scope.parent);
|
543
|
-
|
544
565
|
return false;
|
545
566
|
}
|
546
|
-
|
547
567
|
hasReference(name) {
|
548
|
-
|
549
|
-
|
550
|
-
do {
|
551
|
-
if (scope.references[name]) return true;
|
552
|
-
} while (scope = scope.parent);
|
553
|
-
|
554
|
-
return false;
|
568
|
+
return !!this.getProgramParent().references[name];
|
555
569
|
}
|
556
|
-
|
557
570
|
isPure(node, constantsOnly) {
|
558
|
-
if (
|
571
|
+
if (isIdentifier(node)) {
|
559
572
|
const binding = this.getBinding(node.name);
|
560
573
|
if (!binding) return false;
|
561
574
|
if (constantsOnly) return binding.constant;
|
562
575
|
return true;
|
563
|
-
} else if (
|
576
|
+
} else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
|
577
|
+
return true;
|
578
|
+
} else if (isClass(node)) {
|
579
|
+
var _node$decorators;
|
564
580
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
565
581
|
return false;
|
566
582
|
}
|
567
|
-
|
583
|
+
if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
|
584
|
+
return false;
|
585
|
+
}
|
568
586
|
return this.isPure(node.body, constantsOnly);
|
569
|
-
} else if (
|
587
|
+
} else if (isClassBody(node)) {
|
570
588
|
for (const method of node.body) {
|
571
589
|
if (!this.isPure(method, constantsOnly)) return false;
|
572
590
|
}
|
573
|
-
|
574
591
|
return true;
|
575
|
-
} else if (
|
592
|
+
} else if (isBinary(node)) {
|
576
593
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
577
|
-
} else if (
|
594
|
+
} else if (isArrayExpression(node) || isTupleExpression(node)) {
|
578
595
|
for (const elem of node.elements) {
|
579
|
-
if (!this.isPure(elem, constantsOnly)) return false;
|
596
|
+
if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
|
580
597
|
}
|
581
|
-
|
582
598
|
return true;
|
583
|
-
} else if (
|
599
|
+
} else if (isObjectExpression(node) || isRecordExpression(node)) {
|
584
600
|
for (const prop of node.properties) {
|
585
601
|
if (!this.isPure(prop, constantsOnly)) return false;
|
586
602
|
}
|
587
|
-
|
588
603
|
return true;
|
589
|
-
} else if (
|
604
|
+
} else if (isMethod(node)) {
|
605
|
+
var _node$decorators2;
|
590
606
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
591
|
-
if (node.
|
607
|
+
if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
|
608
|
+
return false;
|
609
|
+
}
|
592
610
|
return true;
|
593
|
-
} else if (
|
611
|
+
} else if (isProperty(node)) {
|
612
|
+
var _node$decorators3;
|
594
613
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
595
|
-
|
596
|
-
|
614
|
+
if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
|
615
|
+
return false;
|
616
|
+
}
|
617
|
+
if (isObjectProperty(node) || node.static) {
|
618
|
+
if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
|
619
|
+
return false;
|
620
|
+
}
|
621
|
+
}
|
622
|
+
return true;
|
623
|
+
} else if (isUnaryExpression(node)) {
|
597
624
|
return this.isPure(node.argument, constantsOnly);
|
598
|
-
} else if (
|
599
|
-
return
|
600
|
-
} else if (
|
625
|
+
} else if (isTaggedTemplateExpression(node)) {
|
626
|
+
return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
627
|
+
} else if (isTemplateLiteral(node)) {
|
601
628
|
for (const expression of node.expressions) {
|
602
629
|
if (!this.isPure(expression, constantsOnly)) return false;
|
603
630
|
}
|
604
|
-
|
605
631
|
return true;
|
606
632
|
} else {
|
607
|
-
return
|
633
|
+
return isPureish(node);
|
608
634
|
}
|
609
635
|
}
|
610
|
-
|
611
636
|
setData(key, val) {
|
612
637
|
return this.data[key] = val;
|
613
638
|
}
|
614
|
-
|
615
639
|
getData(key) {
|
616
640
|
let scope = this;
|
617
|
-
|
618
641
|
do {
|
619
642
|
const data = scope.data[key];
|
620
643
|
if (data != null) return data;
|
621
644
|
} while (scope = scope.parent);
|
622
645
|
}
|
623
|
-
|
624
646
|
removeData(key) {
|
625
647
|
let scope = this;
|
626
|
-
|
627
648
|
do {
|
628
649
|
const data = scope.data[key];
|
629
650
|
if (data != null) scope.data[key] = null;
|
630
651
|
} while (scope = scope.parent);
|
631
652
|
}
|
632
|
-
|
633
653
|
init() {
|
634
|
-
if (!this.
|
654
|
+
if (!this.inited) {
|
655
|
+
this.inited = true;
|
656
|
+
this.crawl();
|
657
|
+
}
|
635
658
|
}
|
636
|
-
|
637
659
|
crawl() {
|
638
660
|
const path = this.path;
|
639
661
|
this.references = Object.create(null);
|
@@ -641,255 +663,226 @@ class Scope {
|
|
641
663
|
this.globals = Object.create(null);
|
642
664
|
this.uids = Object.create(null);
|
643
665
|
this.data = Object.create(null);
|
644
|
-
|
645
|
-
if (
|
646
|
-
for (const key of t().FOR_INIT_KEYS) {
|
647
|
-
const node = path.get(key);
|
648
|
-
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
|
649
|
-
}
|
650
|
-
}
|
651
|
-
|
652
|
-
if (path.isFunctionExpression() && path.has("id")) {
|
653
|
-
if (!path.get("id").node[t().NOT_LOCAL_BINDING]) {
|
654
|
-
this.registerBinding("local", path.get("id"), path);
|
655
|
-
}
|
656
|
-
}
|
657
|
-
|
658
|
-
if (path.isClassExpression() && path.has("id")) {
|
659
|
-
if (!path.get("id").node[t().NOT_LOCAL_BINDING]) {
|
660
|
-
this.registerBinding("local", path);
|
661
|
-
}
|
662
|
-
}
|
663
|
-
|
664
|
-
if (path.isFunction()) {
|
665
|
-
const params = path.get("params");
|
666
|
-
|
667
|
-
for (const param of params) {
|
668
|
-
this.registerBinding("param", param);
|
669
|
-
}
|
670
|
-
}
|
671
|
-
|
672
|
-
if (path.isCatchClause()) {
|
673
|
-
this.registerBinding("let", path);
|
674
|
-
}
|
675
|
-
|
676
|
-
const parent = this.getProgramParent();
|
677
|
-
if (parent.crawling) return;
|
666
|
+
const programParent = this.getProgramParent();
|
667
|
+
if (programParent.crawling) return;
|
678
668
|
const state = {
|
679
669
|
references: [],
|
680
670
|
constantViolations: [],
|
681
671
|
assignments: []
|
682
672
|
};
|
683
673
|
this.crawling = true;
|
674
|
+
if (path.type !== "Program" && (0, _visitors.isExplodedVisitor)(collectorVisitor)) {
|
675
|
+
for (const visit of collectorVisitor.enter) {
|
676
|
+
visit.call(state, path, state);
|
677
|
+
}
|
678
|
+
const typeVisitors = collectorVisitor[path.type];
|
679
|
+
if (typeVisitors) {
|
680
|
+
for (const visit of typeVisitors.enter) {
|
681
|
+
visit.call(state, path, state);
|
682
|
+
}
|
683
|
+
}
|
684
|
+
}
|
684
685
|
path.traverse(collectorVisitor, state);
|
685
686
|
this.crawling = false;
|
686
|
-
|
687
687
|
for (const path of state.assignments) {
|
688
688
|
const ids = path.getBindingIdentifiers();
|
689
|
-
|
690
|
-
|
691
|
-
for (const name in ids) {
|
689
|
+
for (const name of Object.keys(ids)) {
|
692
690
|
if (path.scope.getBinding(name)) continue;
|
693
|
-
programParent = programParent || path.scope.getProgramParent();
|
694
691
|
programParent.addGlobal(ids[name]);
|
695
692
|
}
|
696
|
-
|
697
693
|
path.scope.registerConstantViolation(path);
|
698
694
|
}
|
699
|
-
|
700
695
|
for (const ref of state.references) {
|
701
696
|
const binding = ref.scope.getBinding(ref.node.name);
|
702
|
-
|
703
697
|
if (binding) {
|
704
698
|
binding.reference(ref);
|
705
699
|
} else {
|
706
|
-
|
700
|
+
programParent.addGlobal(ref.node);
|
707
701
|
}
|
708
702
|
}
|
709
|
-
|
710
703
|
for (const path of state.constantViolations) {
|
711
704
|
path.scope.registerConstantViolation(path);
|
712
705
|
}
|
713
706
|
}
|
714
|
-
|
715
707
|
push(opts) {
|
716
708
|
let path = this.path;
|
717
|
-
|
718
|
-
|
709
|
+
if (path.isPattern()) {
|
710
|
+
path = this.getPatternParent().path;
|
711
|
+
} else if (!path.isBlockStatement() && !path.isProgram()) {
|
719
712
|
path = this.getBlockParent().path;
|
720
713
|
}
|
721
|
-
|
722
714
|
if (path.isSwitchStatement()) {
|
723
715
|
path = (this.getFunctionParent() || this.getProgramParent()).path;
|
724
716
|
}
|
725
|
-
|
717
|
+
const {
|
718
|
+
init,
|
719
|
+
unique,
|
720
|
+
kind = "var",
|
721
|
+
id
|
722
|
+
} = opts;
|
723
|
+
if (!init && !unique && (kind === "var" || kind === "let") && path.isFunction() && !path.node.name && t.isCallExpression(path.parent, {
|
724
|
+
callee: path.node
|
725
|
+
}) && path.parent.arguments.length <= path.node.params.length && t.isIdentifier(id)) {
|
726
|
+
path.pushContainer("params", id);
|
727
|
+
path.scope.registerBinding("param", path.get("params")[path.node.params.length - 1]);
|
728
|
+
return;
|
729
|
+
}
|
726
730
|
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
|
727
731
|
path.ensureBlock();
|
728
732
|
path = path.get("body");
|
729
733
|
}
|
730
|
-
|
731
|
-
const unique = opts.unique;
|
732
|
-
const kind = opts.kind || "var";
|
733
734
|
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
734
735
|
const dataKey = `declaration:${kind}:${blockHoist}`;
|
735
736
|
let declarPath = !unique && path.getData(dataKey);
|
736
|
-
|
737
737
|
if (!declarPath) {
|
738
|
-
const declar =
|
738
|
+
const declar = variableDeclaration(kind, []);
|
739
739
|
declar._blockHoist = blockHoist;
|
740
740
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
741
741
|
if (!unique) path.setData(dataKey, declarPath);
|
742
742
|
}
|
743
|
-
|
744
|
-
const
|
745
|
-
|
746
|
-
this.registerBinding(kind, declarPath.get("declarations").pop());
|
743
|
+
const declarator = variableDeclarator(id, init);
|
744
|
+
const len = declarPath.node.declarations.push(declarator);
|
745
|
+
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
747
746
|
}
|
748
|
-
|
749
747
|
getProgramParent() {
|
750
748
|
let scope = this;
|
751
|
-
|
752
749
|
do {
|
753
750
|
if (scope.path.isProgram()) {
|
754
751
|
return scope;
|
755
752
|
}
|
756
753
|
} while (scope = scope.parent);
|
757
|
-
|
758
754
|
throw new Error("Couldn't find a Program");
|
759
755
|
}
|
760
|
-
|
761
756
|
getFunctionParent() {
|
762
757
|
let scope = this;
|
763
|
-
|
764
758
|
do {
|
765
759
|
if (scope.path.isFunctionParent()) {
|
766
760
|
return scope;
|
767
761
|
}
|
768
762
|
} while (scope = scope.parent);
|
769
|
-
|
770
763
|
return null;
|
771
764
|
}
|
772
|
-
|
773
765
|
getBlockParent() {
|
774
766
|
let scope = this;
|
775
|
-
|
776
767
|
do {
|
777
768
|
if (scope.path.isBlockParent()) {
|
778
769
|
return scope;
|
779
770
|
}
|
780
771
|
} while (scope = scope.parent);
|
781
|
-
|
782
772
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
783
773
|
}
|
784
|
-
|
774
|
+
getPatternParent() {
|
775
|
+
let scope = this;
|
776
|
+
do {
|
777
|
+
if (!scope.path.isPattern()) {
|
778
|
+
return scope.getBlockParent();
|
779
|
+
}
|
780
|
+
} while (scope = scope.parent.parent);
|
781
|
+
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
782
|
+
}
|
785
783
|
getAllBindings() {
|
786
784
|
const ids = Object.create(null);
|
787
785
|
let scope = this;
|
788
|
-
|
789
786
|
do {
|
790
|
-
(
|
787
|
+
for (const key of Object.keys(scope.bindings)) {
|
788
|
+
if (key in ids === false) {
|
789
|
+
ids[key] = scope.bindings[key];
|
790
|
+
}
|
791
|
+
}
|
791
792
|
scope = scope.parent;
|
792
793
|
} while (scope);
|
793
|
-
|
794
794
|
return ids;
|
795
795
|
}
|
796
|
-
|
797
|
-
getAllBindingsOfKind() {
|
796
|
+
getAllBindingsOfKind(...kinds) {
|
798
797
|
const ids = Object.create(null);
|
799
|
-
|
800
|
-
for (const kind of arguments) {
|
798
|
+
for (const kind of kinds) {
|
801
799
|
let scope = this;
|
802
|
-
|
803
800
|
do {
|
804
|
-
for (const name
|
801
|
+
for (const name of Object.keys(scope.bindings)) {
|
805
802
|
const binding = scope.bindings[name];
|
806
803
|
if (binding.kind === kind) ids[name] = binding;
|
807
804
|
}
|
808
|
-
|
809
805
|
scope = scope.parent;
|
810
806
|
} while (scope);
|
811
807
|
}
|
812
|
-
|
813
808
|
return ids;
|
814
809
|
}
|
815
|
-
|
816
810
|
bindingIdentifierEquals(name, node) {
|
817
811
|
return this.getBindingIdentifier(name) === node;
|
818
812
|
}
|
819
|
-
|
820
813
|
getBinding(name) {
|
821
814
|
let scope = this;
|
822
|
-
|
815
|
+
let previousPath;
|
823
816
|
do {
|
824
817
|
const binding = scope.getOwnBinding(name);
|
825
|
-
if (binding)
|
818
|
+
if (binding) {
|
819
|
+
var _previousPath;
|
820
|
+
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
|
821
|
+
return binding;
|
822
|
+
}
|
823
|
+
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
824
|
+
break;
|
825
|
+
}
|
826
|
+
previousPath = scope.path;
|
826
827
|
} while (scope = scope.parent);
|
827
828
|
}
|
828
|
-
|
829
829
|
getOwnBinding(name) {
|
830
830
|
return this.bindings[name];
|
831
831
|
}
|
832
|
-
|
833
832
|
getBindingIdentifier(name) {
|
834
|
-
|
835
|
-
return
|
833
|
+
var _this$getBinding2;
|
834
|
+
return (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.identifier;
|
836
835
|
}
|
837
|
-
|
838
836
|
getOwnBindingIdentifier(name) {
|
839
837
|
const binding = this.bindings[name];
|
840
|
-
return binding
|
838
|
+
return binding == null ? void 0 : binding.identifier;
|
841
839
|
}
|
842
|
-
|
843
840
|
hasOwnBinding(name) {
|
844
841
|
return !!this.getOwnBinding(name);
|
845
842
|
}
|
846
|
-
|
847
|
-
|
843
|
+
hasBinding(name, opts) {
|
844
|
+
var _opts, _opts2, _opts3;
|
848
845
|
if (!name) return false;
|
849
846
|
if (this.hasOwnBinding(name)) return true;
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
847
|
+
{
|
848
|
+
if (typeof opts === "boolean") opts = {
|
849
|
+
noGlobals: opts
|
850
|
+
};
|
851
|
+
}
|
852
|
+
if (this.parentHasBinding(name, opts)) return true;
|
853
|
+
if (!((_opts = opts) != null && _opts.noUids) && this.hasUid(name)) return true;
|
854
|
+
if (!((_opts2 = opts) != null && _opts2.noGlobals) && Scope.globals.includes(name)) return true;
|
855
|
+
if (!((_opts3 = opts) != null && _opts3.noGlobals) && Scope.contextVariables.includes(name)) return true;
|
854
856
|
return false;
|
855
857
|
}
|
856
|
-
|
857
|
-
|
858
|
-
return
|
858
|
+
parentHasBinding(name, opts) {
|
859
|
+
var _this$parent;
|
860
|
+
return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
|
859
861
|
}
|
860
|
-
|
861
862
|
moveBindingTo(name, scope) {
|
862
863
|
const info = this.getBinding(name);
|
863
|
-
|
864
864
|
if (info) {
|
865
865
|
info.scope.removeOwnBinding(name);
|
866
866
|
info.scope = scope;
|
867
867
|
scope.bindings[name] = info;
|
868
868
|
}
|
869
869
|
}
|
870
|
-
|
871
870
|
removeOwnBinding(name) {
|
872
871
|
delete this.bindings[name];
|
873
872
|
}
|
874
|
-
|
875
873
|
removeBinding(name) {
|
876
|
-
|
877
|
-
|
878
|
-
if (info) {
|
879
|
-
info.scope.removeOwnBinding(name);
|
880
|
-
}
|
881
|
-
|
874
|
+
var _this$getBinding3;
|
875
|
+
(_this$getBinding3 = this.getBinding(name)) == null ? void 0 : _this$getBinding3.scope.removeOwnBinding(name);
|
882
876
|
let scope = this;
|
883
|
-
|
884
877
|
do {
|
885
878
|
if (scope.uids[name]) {
|
886
879
|
scope.uids[name] = false;
|
887
880
|
}
|
888
881
|
} while (scope = scope.parent);
|
889
882
|
}
|
890
|
-
|
891
883
|
}
|
892
|
-
|
893
884
|
exports.default = Scope;
|
894
|
-
Scope.globals = Object.keys(_globals
|
895
|
-
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|
885
|
+
Scope.globals = Object.keys(_globals.builtin);
|
886
|
+
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|
887
|
+
|
888
|
+
//# sourceMappingURL=index.js.map
|