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