1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _typeof2 = require("babel-runtime/helpers/typeof");
|
6 |
|
7 | var _typeof3 = _interopRequireDefault(_typeof2);
|
8 |
|
9 | var _getIterator2 = require("babel-runtime/core-js/get-iterator");
|
10 |
|
11 | var _getIterator3 = _interopRequireDefault(_getIterator2);
|
12 |
|
13 | exports.default = explode;
|
14 | exports.verify = verify;
|
15 |
|
16 | var _babelTypes = require("babel-types");
|
17 |
|
18 | var t = _interopRequireWildcard(_babelTypes);
|
19 |
|
20 | var _lodash = require("lodash.clone");
|
21 |
|
22 | var _lodash2 = _interopRequireDefault(_lodash);
|
23 |
|
24 | function _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 |
|
26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function explode(visitor) {
|
47 | if (visitor._exploded) return visitor;
|
48 | visitor._exploded = true;
|
49 |
|
50 |
|
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 |
|
79 | verify(visitor);
|
80 |
|
81 |
|
82 |
|
83 | delete visitor.__esModule;
|
84 |
|
85 |
|
86 | ensureEntranceObjects(visitor);
|
87 |
|
88 |
|
89 | ensureCallbackArrays(visitor);
|
90 |
|
91 |
|
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 |
|
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 |
|
142 | function verify(visitor) {
|
143 | if (visitor._verified) return;
|
144 |
|
145 | if (typeof visitor === "function") {
|
146 |
|
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 |
|
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 |
|
167 | validateVisitorMethods(nodeType + "." + visitorKey, visitors[visitorKey]);
|
168 | } else {
|
169 |
|
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 |
|
179 | function 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 |
|
201 | function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
|
202 | var newVisitor = {};
|
203 |
|
204 | var _loop = function _loop(key) {
|
205 | var fns = oldVisitor[key];
|
206 |
|
207 |
|
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 |
|
238 | function 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 |
|
249 | function 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 |
|
254 | function shouldIgnoreKey(key) {
|
255 |
|
256 | if (key[0] === "_") return true;
|
257 |
|
258 |
|
259 | if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
|
260 |
|
261 |
|
262 | if (key === "blacklist" || key === "noScope" || key === "skipKeys") return true;
|
263 |
|
264 | return false;
|
265 | }
|
266 |
|
267 | function 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 |