UNPKG

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