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 = _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 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 |
|
79 | function 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 |
|
105 | function skip() {
|
106 | this.shouldSkip = true;
|
107 | }
|
108 |
|
109 | function skipKey(key) {
|
110 | if (this.skipKeys == null) {
|
111 | this.skipKeys = {};
|
112 | }
|
113 |
|
114 | this.skipKeys[key] = true;
|
115 | }
|
116 |
|
117 | function stop() {
|
118 | this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
|
119 | }
|
120 |
|
121 | function 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 |
|
136 | function 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 |
|
153 | function resync() {
|
154 | if (this.removed) return;
|
155 |
|
156 | this._resyncParent();
|
157 |
|
158 | this._resyncList();
|
159 |
|
160 | this._resyncKey();
|
161 | }
|
162 |
|
163 | function _resyncParent() {
|
164 | if (this.parentPath) {
|
165 | this.parent = this.parentPath.node;
|
166 | }
|
167 | }
|
168 |
|
169 | function _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 |
|
190 | function _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 |
|
197 | function _resyncRemoved() {
|
198 | if (this.key == null || !this.container || this.container[this.key] !== this.node) {
|
199 | this._markRemoved();
|
200 | }
|
201 | }
|
202 |
|
203 | function 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 |
|
213 | function pushContext(context) {
|
214 | this.contexts.push(context);
|
215 | this.setContext(context);
|
216 | }
|
217 |
|
218 | function 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 |
|
225 | function 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 |
|
233 | function 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 |
|
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 | } |
\ | No newline at end of file |