UNPKG

5.41 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.call = call;
7exports._call = _call;
8exports.isBlacklisted = isBlacklisted;
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 isBlacklisted() {
73 const blacklist = this.opts.blacklist;
74 return blacklist && blacklist.indexOf(this.node.type) > -1;
75}
76
77function visit() {
78 if (!this.node) {
79 return false;
80 }
81
82 if (this.isBlacklisted()) {
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
103function skip() {
104 this.shouldSkip = true;
105}
106
107function skipKey(key) {
108 if (this.skipKeys == null) {
109 this.skipKeys = {};
110 }
111
112 this.skipKeys[key] = true;
113}
114
115function stop() {
116 this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
117}
118
119function setScope() {
120 if (this.opts && this.opts.noScope) return;
121 let path = this.parentPath;
122 let target;
123
124 while (path && !target) {
125 if (path.opts && path.opts.noScope) return;
126 target = path.scope;
127 path = path.parentPath;
128 }
129
130 this.scope = this.getScope(target);
131 if (this.scope) this.scope.init();
132}
133
134function setContext(context) {
135 if (this.skipKeys != null) {
136 this.skipKeys = {};
137 }
138
139 this._traverseFlags = 0;
140
141 if (context) {
142 this.context = context;
143 this.state = context.state;
144 this.opts = context.opts;
145 }
146
147 this.setScope();
148 return this;
149}
150
151function resync() {
152 if (this.removed) return;
153
154 this._resyncParent();
155
156 this._resyncList();
157
158 this._resyncKey();
159}
160
161function _resyncParent() {
162 if (this.parentPath) {
163 this.parent = this.parentPath.node;
164 }
165}
166
167function _resyncKey() {
168 if (!this.container) return;
169 if (this.node === this.container[this.key]) return;
170
171 if (Array.isArray(this.container)) {
172 for (let i = 0; i < this.container.length; i++) {
173 if (this.container[i] === this.node) {
174 return this.setKey(i);
175 }
176 }
177 } else {
178 for (const key of Object.keys(this.container)) {
179 if (this.container[key] === this.node) {
180 return this.setKey(key);
181 }
182 }
183 }
184
185 this.key = null;
186}
187
188function _resyncList() {
189 if (!this.parent || !this.inList) return;
190 const newContainer = this.parent[this.listKey];
191 if (this.container === newContainer) return;
192 this.container = newContainer || null;
193}
194
195function _resyncRemoved() {
196 if (this.key == null || !this.container || this.container[this.key] !== this.node) {
197 this._markRemoved();
198 }
199}
200
201function popContext() {
202 this.contexts.pop();
203
204 if (this.contexts.length > 0) {
205 this.setContext(this.contexts[this.contexts.length - 1]);
206 } else {
207 this.setContext(undefined);
208 }
209}
210
211function pushContext(context) {
212 this.contexts.push(context);
213 this.setContext(context);
214}
215
216function setup(parentPath, container, listKey, key) {
217 this.listKey = listKey;
218 this.container = container;
219 this.parentPath = parentPath || this.parentPath;
220 this.setKey(key);
221}
222
223function setKey(key) {
224 this.key = key;
225 this.node = this.container[this.key];
226 this.type = this.node && this.node.type;
227}
228
229function requeue(pathToQueue = this) {
230 if (pathToQueue.removed) return;
231 const contexts = this.contexts;
232
233 for (const context of contexts) {
234 context.maybeQueue(pathToQueue);
235 }
236}
237
238function _getQueueContexts() {
239 let path = this;
240 let contexts = this.contexts;
241
242 while (!contexts.length) {
243 path = path.parentPath;
244 if (!path) break;
245 contexts = path.contexts;
246 }
247
248 return contexts;
249}
\No newline at end of file