[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.call = call;
|
---|
| 7 | exports._call = _call;
|
---|
| 8 | exports.isBlacklisted = exports.isDenylisted = isDenylisted;
|
---|
| 9 | exports.visit = visit;
|
---|
| 10 | exports.skip = skip;
|
---|
| 11 | exports.skipKey = skipKey;
|
---|
| 12 | exports.stop = stop;
|
---|
| 13 | exports.setScope = setScope;
|
---|
| 14 | exports.setContext = setContext;
|
---|
| 15 | exports.resync = resync;
|
---|
| 16 | exports._resyncParent = _resyncParent;
|
---|
| 17 | exports._resyncKey = _resyncKey;
|
---|
| 18 | exports._resyncList = _resyncList;
|
---|
| 19 | exports._resyncRemoved = _resyncRemoved;
|
---|
| 20 | exports.popContext = popContext;
|
---|
| 21 | exports.pushContext = pushContext;
|
---|
| 22 | exports.setup = setup;
|
---|
| 23 | exports.setKey = setKey;
|
---|
| 24 | exports.requeue = requeue;
|
---|
| 25 | exports._getQueueContexts = _getQueueContexts;
|
---|
| 26 |
|
---|
| 27 | var _index = require("../index");
|
---|
| 28 |
|
---|
| 29 | var _index2 = require("./index");
|
---|
| 30 |
|
---|
| 31 | function call(key) {
|
---|
| 32 | const opts = this.opts;
|
---|
| 33 | this.debug(key);
|
---|
| 34 |
|
---|
| 35 | if (this.node) {
|
---|
| 36 | if (this._call(opts[key])) return true;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | if (this.node) {
|
---|
| 40 | return this._call(opts[this.node.type] && opts[this.node.type][key]);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return false;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function _call(fns) {
|
---|
| 47 | if (!fns) return false;
|
---|
| 48 |
|
---|
| 49 | for (const fn of fns) {
|
---|
| 50 | if (!fn) continue;
|
---|
| 51 | const node = this.node;
|
---|
| 52 | if (!node) return true;
|
---|
| 53 | const ret = fn.call(this.state, this, this.state);
|
---|
| 54 |
|
---|
| 55 | if (ret && typeof ret === "object" && typeof ret.then === "function") {
|
---|
| 56 | throw new Error(`You appear to be using a plugin with an async traversal visitor, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if (ret) {
|
---|
| 60 | throw new Error(`Unexpected return value from visitor method ${fn}`);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (this.node !== node) return true;
|
---|
| 64 | if (this._traverseFlags > 0) return true;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return false;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | function isDenylisted() {
|
---|
| 71 | var _this$opts$denylist;
|
---|
| 72 |
|
---|
| 73 | const denylist = (_this$opts$denylist = this.opts.denylist) != null ? _this$opts$denylist : this.opts.blacklist;
|
---|
| 74 | return denylist && denylist.indexOf(this.node.type) > -1;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | function visit() {
|
---|
| 78 | if (!this.node) {
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (this.isDenylisted()) {
|
---|
| 83 | return false;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (this.shouldSkip || this.call("enter") || this.shouldSkip) {
|
---|
| 91 | this.debug("Skip...");
|
---|
| 92 | return this.shouldStop;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | this.debug("Recursing into...");
|
---|
| 96 |
|
---|
| 97 | _index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
---|
| 98 |
|
---|
| 99 | this.call("exit");
|
---|
| 100 | return this.shouldStop;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | function skip() {
|
---|
| 104 | this.shouldSkip = true;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | function skipKey(key) {
|
---|
| 108 | if (this.skipKeys == null) {
|
---|
| 109 | this.skipKeys = {};
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | this.skipKeys[key] = true;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | function stop() {
|
---|
| 116 | this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | function setScope() {
|
---|
| 120 | if (this.opts && this.opts.noScope) return;
|
---|
| 121 | let path = this.parentPath;
|
---|
| 122 | if (this.key === "key" && path.isMethod()) path = path.parentPath;
|
---|
| 123 | let target;
|
---|
| 124 |
|
---|
| 125 | while (path && !target) {
|
---|
| 126 | if (path.opts && path.opts.noScope) return;
|
---|
| 127 | target = path.scope;
|
---|
| 128 | path = path.parentPath;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | this.scope = this.getScope(target);
|
---|
| 132 | if (this.scope) this.scope.init();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | function setContext(context) {
|
---|
| 136 | if (this.skipKeys != null) {
|
---|
| 137 | this.skipKeys = {};
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | this._traverseFlags = 0;
|
---|
| 141 |
|
---|
| 142 | if (context) {
|
---|
| 143 | this.context = context;
|
---|
| 144 | this.state = context.state;
|
---|
| 145 | this.opts = context.opts;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | this.setScope();
|
---|
| 149 | return this;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | function resync() {
|
---|
| 153 | if (this.removed) return;
|
---|
| 154 |
|
---|
| 155 | this._resyncParent();
|
---|
| 156 |
|
---|
| 157 | this._resyncList();
|
---|
| 158 |
|
---|
| 159 | this._resyncKey();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | function _resyncParent() {
|
---|
| 163 | if (this.parentPath) {
|
---|
| 164 | this.parent = this.parentPath.node;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | function _resyncKey() {
|
---|
| 169 | if (!this.container) return;
|
---|
| 170 | if (this.node === this.container[this.key]) return;
|
---|
| 171 |
|
---|
| 172 | if (Array.isArray(this.container)) {
|
---|
| 173 | for (let i = 0; i < this.container.length; i++) {
|
---|
| 174 | if (this.container[i] === this.node) {
|
---|
| 175 | return this.setKey(i);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | } else {
|
---|
| 179 | for (const key of Object.keys(this.container)) {
|
---|
| 180 | if (this.container[key] === this.node) {
|
---|
| 181 | return this.setKey(key);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | this.key = null;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | function _resyncList() {
|
---|
| 190 | if (!this.parent || !this.inList) return;
|
---|
| 191 | const newContainer = this.parent[this.listKey];
|
---|
| 192 | if (this.container === newContainer) return;
|
---|
| 193 | this.container = newContainer || null;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | function _resyncRemoved() {
|
---|
| 197 | if (this.key == null || !this.container || this.container[this.key] !== this.node) {
|
---|
| 198 | this._markRemoved();
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | function popContext() {
|
---|
| 203 | this.contexts.pop();
|
---|
| 204 |
|
---|
| 205 | if (this.contexts.length > 0) {
|
---|
| 206 | this.setContext(this.contexts[this.contexts.length - 1]);
|
---|
| 207 | } else {
|
---|
| 208 | this.setContext(undefined);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | function pushContext(context) {
|
---|
| 213 | this.contexts.push(context);
|
---|
| 214 | this.setContext(context);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | function setup(parentPath, container, listKey, key) {
|
---|
| 218 | this.listKey = listKey;
|
---|
| 219 | this.container = container;
|
---|
| 220 | this.parentPath = parentPath || this.parentPath;
|
---|
| 221 | this.setKey(key);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | function setKey(key) {
|
---|
| 225 | var _this$node;
|
---|
| 226 |
|
---|
| 227 | this.key = key;
|
---|
| 228 | this.node = this.container[this.key];
|
---|
| 229 | this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | function requeue(pathToQueue = this) {
|
---|
| 233 | if (pathToQueue.removed) return;
|
---|
| 234 | ;
|
---|
| 235 | const contexts = this.contexts;
|
---|
| 236 |
|
---|
| 237 | for (const context of contexts) {
|
---|
| 238 | context.maybeQueue(pathToQueue);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | function _getQueueContexts() {
|
---|
| 243 | let path = this;
|
---|
| 244 | let contexts = this.contexts;
|
---|
| 245 |
|
---|
| 246 | while (!contexts.length) {
|
---|
| 247 | path = path.parentPath;
|
---|
| 248 | if (!path) break;
|
---|
| 249 | contexts = path.contexts;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | return contexts;
|
---|
| 253 | } |
---|