UNPKG

7.82 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4
5var _typeof2 = require("babel-runtime/helpers/typeof");
6
7var _typeof3 = _interopRequireDefault(_typeof2);
8
9var _getIterator2 = require("babel-runtime/core-js/get-iterator");
10
11var _getIterator3 = _interopRequireDefault(_getIterator2);
12
13exports.default = explode;
14exports.verify = verify;
15
16var _babelTypes = require("babel-types");
17
18var t = _interopRequireWildcard(_babelTypes);
19
20var _lodash = require("lodash.clone");
21
22var _lodash2 = _interopRequireDefault(_lodash);
23
24function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28/**
29 * explode() will take a visitor object with all of the various shorthands
30 * that we support, and validates & normalizes it into a common format, ready
31 * to be used in traversal
32 *
33 * The various shorthands are:
34 * * `Identifier() { ... }` -> `Identifier: { enter() { ... } }`
35 * * `"Identifier|NumericLiteral": { ... }` -> `Identifier: { ... }, NumericLiteral: { ... }`
36 * * Aliases in `babel-types`: e.g. `Property: { ... }` -> `ObjectProperty: { ... }, ClassProperty: { ... }`
37 *
38 * Other normalizations are:
39 * * `enter` and `exit` functions are wrapped in arrays, to ease merging of
40 * visitors
41 */
42// Copied from babel-traverse, but with virtual types handling removed
43// https://github.com/babel/babel/blob/07b3dc18a09f2217b38a3a63c8613add6df1b47d/packages/babel-traverse/src/visitors.js
44
45// import * as messages from 'babel-messages';
46function explode(visitor) {
47 if (visitor._exploded) return visitor;
48 visitor._exploded = true;
49
50 // normalise pipes
51 for (var nodeType in visitor) {
52 if (shouldIgnoreKey(nodeType)) continue;
53
54 var parts = nodeType.split("|");
55 if (parts.length === 1) continue;
56
57 var fns = visitor[nodeType];
58 delete visitor[nodeType];
59
60 for (var _iterator = parts, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
61 var _ref;
62
63 if (_isArray) {
64 if (_i >= _iterator.length) break;
65 _ref = _iterator[_i++];
66 } else {
67 _i = _iterator.next();
68 if (_i.done) break;
69 _ref = _i.value;
70 }
71
72 var part = _ref;
73
74 visitor[part] = fns;
75 }
76 }
77
78 // verify data structure
79 verify(visitor);
80
81 // make sure there's no __esModule type since this is because we're using loose mode
82 // and it sets __esModule to be enumerable on all modules :(
83 delete visitor.__esModule;
84
85 // ensure visitors are objects
86 ensureEntranceObjects(visitor);
87
88 // ensure enter/exit callbacks are arrays
89 ensureCallbackArrays(visitor);
90
91 // add aliases
92 for (var _nodeType in visitor) {
93 if (shouldIgnoreKey(_nodeType)) continue;
94
95 var _fns = visitor[_nodeType];
96
97 var aliases = t.FLIPPED_ALIAS_KEYS[_nodeType];
98
99 var deprecratedKey = t.DEPRECATED_KEYS[_nodeType];
100 if (deprecratedKey) {
101 console.trace("Visitor defined for " + _nodeType + " but it has been renamed to " + deprecratedKey);
102 aliases = [deprecratedKey];
103 }
104
105 if (!aliases) continue;
106
107 // clear it from the visitor
108 delete visitor[_nodeType];
109
110 for (var _iterator2 = aliases, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
111 var _ref2;
112
113 if (_isArray2) {
114 if (_i2 >= _iterator2.length) break;
115 _ref2 = _iterator2[_i2++];
116 } else {
117 _i2 = _iterator2.next();
118 if (_i2.done) break;
119 _ref2 = _i2.value;
120 }
121
122 var alias = _ref2;
123
124 var existing = visitor[alias];
125 if (existing) {
126 mergePair(existing, _fns);
127 } else {
128 visitor[alias] = (0, _lodash2.default)(_fns);
129 }
130 }
131 }
132
133 for (var _nodeType2 in visitor) {
134 if (shouldIgnoreKey(_nodeType2)) continue;
135
136 ensureCallbackArrays(visitor[_nodeType2]);
137 }
138
139 return visitor;
140}
141
142function verify(visitor) {
143 if (visitor._verified) return;
144
145 if (typeof visitor === "function") {
146 // throw new Error(messages.get("traverseVerifyRootFunction"));
147 throw new Error("You passed `traverse()` a function when it expected a visitor object, are you sure you didn't mean `{ enter: Function }`?");
148 }
149
150 for (var nodeType in visitor) {
151 if (nodeType === "enter" || nodeType === "exit") {
152 validateVisitorMethods(nodeType, visitor[nodeType]);
153 }
154
155 if (shouldIgnoreKey(nodeType)) continue;
156
157 if (t.TYPES.indexOf(nodeType) < 0) {
158 // throw new Error(messages.get("traverseVerifyNodeType", nodeType));
159 throw new Error("You gave us a visitor for the node type " + nodeType + " but it's not a valid type");
160 }
161
162 var visitors = visitor[nodeType];
163 if ((typeof visitors === "undefined" ? "undefined" : (0, _typeof3.default)(visitors)) === "object") {
164 for (var visitorKey in visitors) {
165 if (visitorKey === "enter" || visitorKey === "exit") {
166 // verify that it just contains functions
167 validateVisitorMethods(nodeType + "." + visitorKey, visitors[visitorKey]);
168 } else {
169 // throw new Error(messages.get("traverseVerifyVisitorProperty", nodeType, visitorKey));
170 throw new Error("You passed `traverse()` a visitor object with the property " + nodeType + " that has the invalid property " + visitorKey);
171 }
172 }
173 }
174 }
175
176 visitor._verified = true;
177}
178
179function validateVisitorMethods(path, val) {
180 var fns = [].concat(val);
181 for (var _iterator3 = fns, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
182 var _ref3;
183
184 if (_isArray3) {
185 if (_i3 >= _iterator3.length) break;
186 _ref3 = _iterator3[_i3++];
187 } else {
188 _i3 = _iterator3.next();
189 if (_i3.done) break;
190 _ref3 = _i3.value;
191 }
192
193 var fn = _ref3;
194
195 if (typeof fn !== "function") {
196 throw new TypeError("Non-function found defined in " + path + " with type " + (typeof fn === "undefined" ? "undefined" : (0, _typeof3.default)(fn)));
197 }
198 }
199}
200
201function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
202 var newVisitor = {};
203
204 var _loop = function _loop(key) {
205 var fns = oldVisitor[key];
206
207 // not an enter/exit array of callbacks
208 if (!Array.isArray(fns)) return "continue";
209
210 fns = fns.map(function (fn) {
211 var newFn = fn;
212
213 if (state) {
214 newFn = function newFn(path) {
215 return fn.call(state, path, state);
216 };
217 }
218
219 if (wrapper) {
220 newFn = wrapper(state.key, key, newFn);
221 }
222
223 return newFn;
224 });
225
226 newVisitor[key] = fns;
227 };
228
229 for (var key in oldVisitor) {
230 var _ret = _loop(key);
231
232 if (_ret === "continue") continue;
233 }
234
235 return newVisitor;
236}
237
238function ensureEntranceObjects(obj) {
239 for (var key in obj) {
240 if (shouldIgnoreKey(key)) continue;
241
242 var fns = obj[key];
243 if (typeof fns === "function") {
244 obj[key] = { enter: fns };
245 }
246 }
247}
248
249function ensureCallbackArrays(obj) {
250 if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
251 if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
252}
253
254function shouldIgnoreKey(key) {
255 // internal/hidden key
256 if (key[0] === "_") return true;
257
258 // ignore function keys
259 if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
260
261 // ignore other options
262 if (key === "blacklist" || key === "noScope" || key === "skipKeys") return true;
263
264 return false;
265}
266
267function mergePair(dest, src) {
268 for (var key in src) {
269 dest[key] = [].concat(dest[key] || [], src[key]);
270 }
271}
\No newline at end of file