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.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 == null ? void 0 : denylist.includes(this.node.type);
|
---|
65 | }
|
---|
66 | {
|
---|
67 | exports.isBlacklisted = isDenylisted;
|
---|
68 | }
|
---|
69 | function restoreContext(path, context) {
|
---|
70 | if (path.context !== context) {
|
---|
71 | path.context = context;
|
---|
72 | path.state = context.state;
|
---|
73 | path.opts = context.opts;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | function visit() {
|
---|
77 | var _this$opts$shouldSkip, _this$opts;
|
---|
78 | if (!this.node) {
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | if (this.isDenylisted()) {
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 | if ((_this$opts$shouldSkip = (_this$opts = this.opts).shouldSkip) != null && _this$opts$shouldSkip.call(_this$opts, this)) {
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 | const currentContext = this.context;
|
---|
88 | if (this.shouldSkip || call.call(this, "enter")) {
|
---|
89 | this.debug("Skip...");
|
---|
90 | return this.shouldStop;
|
---|
91 | }
|
---|
92 | restoreContext(this, currentContext);
|
---|
93 | this.debug("Recursing into...");
|
---|
94 | this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
---|
95 | restoreContext(this, currentContext);
|
---|
96 | call.call(this, "exit");
|
---|
97 | return this.shouldStop;
|
---|
98 | }
|
---|
99 | function skip() {
|
---|
100 | this.shouldSkip = true;
|
---|
101 | }
|
---|
102 | function skipKey(key) {
|
---|
103 | if (this.skipKeys == null) {
|
---|
104 | this.skipKeys = {};
|
---|
105 | }
|
---|
106 | this.skipKeys[key] = true;
|
---|
107 | }
|
---|
108 | function stop() {
|
---|
109 | this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
|
---|
110 | }
|
---|
111 | function setScope() {
|
---|
112 | var _this$opts2, _this$scope;
|
---|
113 | if ((_this$opts2 = this.opts) != null && _this$opts2.noScope) return;
|
---|
114 | let path = this.parentPath;
|
---|
115 | if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
|
---|
116 | path = path.parentPath;
|
---|
117 | }
|
---|
118 | let target;
|
---|
119 | while (path && !target) {
|
---|
120 | var _path$opts;
|
---|
121 | if ((_path$opts = path.opts) != null && _path$opts.noScope) return;
|
---|
122 | target = path.scope;
|
---|
123 | path = path.parentPath;
|
---|
124 | }
|
---|
125 | this.scope = this.getScope(target);
|
---|
126 | (_this$scope = this.scope) == null || _this$scope.init();
|
---|
127 | }
|
---|
128 | function setContext(context) {
|
---|
129 | if (this.skipKeys != null) {
|
---|
130 | this.skipKeys = {};
|
---|
131 | }
|
---|
132 | this._traverseFlags = 0;
|
---|
133 | if (context) {
|
---|
134 | this.context = context;
|
---|
135 | this.state = context.state;
|
---|
136 | this.opts = context.opts;
|
---|
137 | }
|
---|
138 | setScope.call(this);
|
---|
139 | return this;
|
---|
140 | }
|
---|
141 | function resync() {
|
---|
142 | if (this.removed) return;
|
---|
143 | _resyncParent.call(this);
|
---|
144 | _resyncList.call(this);
|
---|
145 | _resyncKey.call(this);
|
---|
146 | }
|
---|
147 | function _resyncParent() {
|
---|
148 | if (this.parentPath) {
|
---|
149 | this.parent = this.parentPath.node;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | function _resyncKey() {
|
---|
153 | if (!this.container) return;
|
---|
154 | if (this.node === this.container[this.key]) {
|
---|
155 | return;
|
---|
156 | }
|
---|
157 | if (Array.isArray(this.container)) {
|
---|
158 | for (let i = 0; i < this.container.length; i++) {
|
---|
159 | if (this.container[i] === this.node) {
|
---|
160 | setKey.call(this, i);
|
---|
161 | return;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | } else {
|
---|
165 | for (const key of Object.keys(this.container)) {
|
---|
166 | if (this.container[key] === this.node) {
|
---|
167 | setKey.call(this, key);
|
---|
168 | return;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | this.key = null;
|
---|
173 | }
|
---|
174 | function _resyncList() {
|
---|
175 | if (!this.parent || !this.inList) return;
|
---|
176 | const newContainer = this.parent[this.listKey];
|
---|
177 | if (this.container === newContainer) return;
|
---|
178 | this.container = newContainer || null;
|
---|
179 | }
|
---|
180 | function _resyncRemoved() {
|
---|
181 | if (this.key == null || !this.container || this.container[this.key] !== this.node) {
|
---|
182 | _removal._markRemoved.call(this);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | function popContext() {
|
---|
186 | this.contexts.pop();
|
---|
187 | if (this.contexts.length > 0) {
|
---|
188 | this.setContext(this.contexts[this.contexts.length - 1]);
|
---|
189 | } else {
|
---|
190 | this.setContext(undefined);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | function pushContext(context) {
|
---|
194 | this.contexts.push(context);
|
---|
195 | this.setContext(context);
|
---|
196 | }
|
---|
197 | function setup(parentPath, container, listKey, key) {
|
---|
198 | this.listKey = listKey;
|
---|
199 | this.container = container;
|
---|
200 | this.parentPath = parentPath || this.parentPath;
|
---|
201 | setKey.call(this, key);
|
---|
202 | }
|
---|
203 | function setKey(key) {
|
---|
204 | var _this$node;
|
---|
205 | this.key = key;
|
---|
206 | this.node = this.container[this.key];
|
---|
207 | this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
|
---|
208 | }
|
---|
209 | function requeue(pathToQueue = this) {
|
---|
210 | if (pathToQueue.removed) return;
|
---|
211 | ;
|
---|
212 | const contexts = this.contexts;
|
---|
213 | for (const context of contexts) {
|
---|
214 | context.maybeQueue(pathToQueue);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | function requeueComputedKeyAndDecorators() {
|
---|
218 | const {
|
---|
219 | context,
|
---|
220 | node
|
---|
221 | } = this;
|
---|
222 | if (!t.isPrivate(node) && node.computed) {
|
---|
223 | context.maybeQueue(this.get("key"));
|
---|
224 | }
|
---|
225 | if (node.decorators) {
|
---|
226 | for (const decorator of this.get("decorators")) {
|
---|
227 | context.maybeQueue(decorator);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | function _getQueueContexts() {
|
---|
232 | let path = this;
|
---|
233 | let contexts = this.contexts;
|
---|
234 | while (!contexts.length) {
|
---|
235 | path = path.parentPath;
|
---|
236 | if (!path) break;
|
---|
237 | contexts = path.contexts;
|
---|
238 | }
|
---|
239 | return contexts;
|
---|
240 | }
|
---|
241 |
|
---|
242 | //# sourceMappingURL=context.js.map
|
---|