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