[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | Object.defineProperty(exports, "NodePath", {
|
---|
| 7 | enumerable: true,
|
---|
| 8 | get: function () {
|
---|
| 9 | return _path.default;
|
---|
| 10 | }
|
---|
| 11 | });
|
---|
| 12 | Object.defineProperty(exports, "Scope", {
|
---|
| 13 | enumerable: true,
|
---|
| 14 | get: function () {
|
---|
| 15 | return _scope.default;
|
---|
| 16 | }
|
---|
| 17 | });
|
---|
| 18 | Object.defineProperty(exports, "Hub", {
|
---|
| 19 | enumerable: true,
|
---|
| 20 | get: function () {
|
---|
| 21 | return _hub.default;
|
---|
| 22 | }
|
---|
| 23 | });
|
---|
| 24 | exports.visitors = exports.default = void 0;
|
---|
| 25 |
|
---|
| 26 | var _context = require("./context");
|
---|
| 27 |
|
---|
| 28 | var visitors = require("./visitors");
|
---|
| 29 |
|
---|
| 30 | exports.visitors = visitors;
|
---|
| 31 |
|
---|
| 32 | var _t = require("@babel/types");
|
---|
| 33 |
|
---|
| 34 | var cache = require("./cache");
|
---|
| 35 |
|
---|
| 36 | var _path = require("./path");
|
---|
| 37 |
|
---|
| 38 | var _scope = require("./scope");
|
---|
| 39 |
|
---|
| 40 | var _hub = require("./hub");
|
---|
| 41 |
|
---|
| 42 | const {
|
---|
| 43 | VISITOR_KEYS,
|
---|
| 44 | removeProperties,
|
---|
| 45 | traverseFast
|
---|
| 46 | } = _t;
|
---|
| 47 |
|
---|
| 48 | function traverse(parent, opts = {}, scope, state, parentPath) {
|
---|
| 49 | if (!parent) return;
|
---|
| 50 |
|
---|
| 51 | if (!opts.noScope && !scope) {
|
---|
| 52 | if (parent.type !== "Program" && parent.type !== "File") {
|
---|
| 53 | throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + `Instead of that you tried to traverse a ${parent.type} node without ` + "passing scope and parentPath.");
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | if (!VISITOR_KEYS[parent.type]) {
|
---|
| 58 | return;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | visitors.explode(opts);
|
---|
| 62 | traverse.node(parent, opts, scope, state, parentPath);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | var _default = traverse;
|
---|
| 66 | exports.default = _default;
|
---|
| 67 | traverse.visitors = visitors;
|
---|
| 68 | traverse.verify = visitors.verify;
|
---|
| 69 | traverse.explode = visitors.explode;
|
---|
| 70 |
|
---|
| 71 | traverse.cheap = function (node, enter) {
|
---|
| 72 | return traverseFast(node, enter);
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
|
---|
| 76 | const keys = VISITOR_KEYS[node.type];
|
---|
| 77 | if (!keys) return;
|
---|
| 78 | const context = new _context.default(scope, opts, state, parentPath);
|
---|
| 79 |
|
---|
| 80 | for (const key of keys) {
|
---|
| 81 | if (skipKeys && skipKeys[key]) continue;
|
---|
| 82 | if (context.visit(node, key)) return;
|
---|
| 83 | }
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | traverse.clearNode = function (node, opts) {
|
---|
| 87 | removeProperties(node, opts);
|
---|
| 88 | cache.path.delete(node);
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | traverse.removeProperties = function (tree, opts) {
|
---|
| 92 | traverseFast(tree, traverse.clearNode, opts);
|
---|
| 93 | return tree;
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | function hasDenylistedType(path, state) {
|
---|
| 97 | if (path.node.type === state.type) {
|
---|
| 98 | state.has = true;
|
---|
| 99 | path.stop();
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | traverse.hasType = function (tree, type, denylistTypes) {
|
---|
| 104 | if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
|
---|
| 105 | if (tree.type === type) return true;
|
---|
| 106 | const state = {
|
---|
| 107 | has: false,
|
---|
| 108 | type: type
|
---|
| 109 | };
|
---|
| 110 | traverse(tree, {
|
---|
| 111 | noScope: true,
|
---|
| 112 | denylist: denylistTypes,
|
---|
| 113 | enter: hasDenylistedType
|
---|
| 114 | }, null, state);
|
---|
| 115 | return state.has;
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | traverse.cache = cache; |
---|