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