1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.call = call;
|
7 | exports._call = _call;
|
8 | exports.isBlacklisted = isBlacklisted;
|
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 = _interopRequireDefault(require("../index"));
|
28 |
|
29 | var _index2 = require("./index");
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | function 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 |
|
48 | function _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 |
|
72 | function isBlacklisted() {
|
73 | const blacklist = this.opts.blacklist;
|
74 | return blacklist && blacklist.indexOf(this.node.type) > -1;
|
75 | }
|
76 |
|
77 | function 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 |
|
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 | 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 |
|
134 | function 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 |
|
151 | function resync() {
|
152 | if (this.removed) return;
|
153 |
|
154 | this._resyncParent();
|
155 |
|
156 | this._resyncList();
|
157 |
|
158 | this._resyncKey();
|
159 | }
|
160 |
|
161 | function _resyncParent() {
|
162 | if (this.parentPath) {
|
163 | this.parent = this.parentPath.node;
|
164 | }
|
165 | }
|
166 |
|
167 | function _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 |
|
188 | function _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 |
|
195 | function _resyncRemoved() {
|
196 | if (this.key == null || !this.container || this.container[this.key] !== this.node) {
|
197 | this._markRemoved();
|
198 | }
|
199 | }
|
200 |
|
201 | function 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 |
|
211 | function pushContext(context) {
|
212 | this.contexts.push(context);
|
213 | this.setContext(context);
|
214 | }
|
215 |
|
216 | function 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 |
|
223 | function setKey(key) {
|
224 | this.key = key;
|
225 | this.node = this.container[this.key];
|
226 | this.type = this.node && this.node.type;
|
227 | }
|
228 |
|
229 | function 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 |
|
238 | function _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 |