visitors.js
8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"use strict";
exports.__esModule = true;
var _typeof2 = require("babel-runtime/helpers/typeof");
var _typeof3 = _interopRequireDefault(_typeof2);
var _keys = require("babel-runtime/core-js/object/keys");
var _keys2 = _interopRequireDefault(_keys);
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
exports.explode = explode;
exports.verify = verify;
exports.merge = merge;
var _virtualTypes = require("./path/lib/virtual-types");
var virtualTypes = _interopRequireWildcard(_virtualTypes);
var _babelMessages = require("babel-messages");
var messages = _interopRequireWildcard(_babelMessages);
var _babelTypes = require("babel-types");
var t = _interopRequireWildcard(_babelTypes);
var _clone = require("lodash/clone");
var _clone2 = _interopRequireDefault(_clone);
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; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function explode(visitor) {
if (visitor._exploded) return visitor;
visitor._exploded = true;
for (var nodeType in visitor) {
if (shouldIgnoreKey(nodeType)) continue;
var parts = nodeType.split("|");
if (parts.length === 1) continue;
var fns = visitor[nodeType];
delete visitor[nodeType];
for (var _iterator = parts, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var part = _ref;
visitor[part] = fns;
}
}
verify(visitor);
delete visitor.__esModule;
ensureEntranceObjects(visitor);
ensureCallbackArrays(visitor);
for (var _iterator2 = (0, _keys2.default)(visitor), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var _nodeType3 = _ref2;
if (shouldIgnoreKey(_nodeType3)) continue;
var wrapper = virtualTypes[_nodeType3];
if (!wrapper) continue;
var _fns2 = visitor[_nodeType3];
for (var type in _fns2) {
_fns2[type] = wrapCheck(wrapper, _fns2[type]);
}
delete visitor[_nodeType3];
if (wrapper.types) {
for (var _iterator4 = wrapper.types, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
var _ref4;
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
_ref4 = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
_ref4 = _i4.value;
}
var _type = _ref4;
if (visitor[_type]) {
mergePair(visitor[_type], _fns2);
} else {
visitor[_type] = _fns2;
}
}
} else {
mergePair(visitor, _fns2);
}
}
for (var _nodeType in visitor) {
if (shouldIgnoreKey(_nodeType)) continue;
var _fns = visitor[_nodeType];
var aliases = t.FLIPPED_ALIAS_KEYS[_nodeType];
var deprecratedKey = t.DEPRECATED_KEYS[_nodeType];
if (deprecratedKey) {
console.trace("Visitor defined for " + _nodeType + " but it has been renamed to " + deprecratedKey);
aliases = [deprecratedKey];
}
if (!aliases) continue;
delete visitor[_nodeType];
for (var _iterator3 = aliases, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
var _ref3;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
var alias = _ref3;
var existing = visitor[alias];
if (existing) {
mergePair(existing, _fns);
} else {
visitor[alias] = (0, _clone2.default)(_fns);
}
}
}
for (var _nodeType2 in visitor) {
if (shouldIgnoreKey(_nodeType2)) continue;
ensureCallbackArrays(visitor[_nodeType2]);
}
return visitor;
}
function verify(visitor) {
if (visitor._verified) return;
if (typeof visitor === "function") {
throw new Error(messages.get("traverseVerifyRootFunction"));
}
for (var nodeType in visitor) {
if (nodeType === "enter" || nodeType === "exit") {
validateVisitorMethods(nodeType, visitor[nodeType]);
}
if (shouldIgnoreKey(nodeType)) continue;
if (t.TYPES.indexOf(nodeType) < 0) {
throw new Error(messages.get("traverseVerifyNodeType", nodeType));
}
var visitors = visitor[nodeType];
if ((typeof visitors === "undefined" ? "undefined" : (0, _typeof3.default)(visitors)) === "object") {
for (var visitorKey in visitors) {
if (visitorKey === "enter" || visitorKey === "exit") {
validateVisitorMethods(nodeType + "." + visitorKey, visitors[visitorKey]);
} else {
throw new Error(messages.get("traverseVerifyVisitorProperty", nodeType, visitorKey));
}
}
}
}
visitor._verified = true;
}
function validateVisitorMethods(path, val) {
var fns = [].concat(val);
for (var _iterator5 = fns, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) {
var _ref5;
if (_isArray5) {
if (_i5 >= _iterator5.length) break;
_ref5 = _iterator5[_i5++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
_ref5 = _i5.value;
}
var fn = _ref5;
if (typeof fn !== "function") {
throw new TypeError("Non-function found defined in " + path + " with type " + (typeof fn === "undefined" ? "undefined" : (0, _typeof3.default)(fn)));
}
}
}
function merge(visitors) {
var states = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var wrapper = arguments[2];
var rootVisitor = {};
for (var i = 0; i < visitors.length; i++) {
var visitor = visitors[i];
var state = states[i];
explode(visitor);
for (var type in visitor) {
var visitorType = visitor[type];
if (state || wrapper) {
visitorType = wrapWithStateOrWrapper(visitorType, state, wrapper);
}
var nodeVisitor = rootVisitor[type] = rootVisitor[type] || {};
mergePair(nodeVisitor, visitorType);
}
}
return rootVisitor;
}
function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
var newVisitor = {};
var _loop = function _loop(key) {
var fns = oldVisitor[key];
if (!Array.isArray(fns)) return "continue";
fns = fns.map(function (fn) {
var newFn = fn;
if (state) {
newFn = function newFn(path) {
return fn.call(state, path, state);
};
}
if (wrapper) {
newFn = wrapper(state.key, key, newFn);
}
return newFn;
});
newVisitor[key] = fns;
};
for (var key in oldVisitor) {
var _ret = _loop(key);
if (_ret === "continue") continue;
}
return newVisitor;
}
function ensureEntranceObjects(obj) {
for (var key in obj) {
if (shouldIgnoreKey(key)) continue;
var fns = obj[key];
if (typeof fns === "function") {
obj[key] = { enter: fns };
}
}
}
function ensureCallbackArrays(obj) {
if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
}
function wrapCheck(wrapper, fn) {
var newFn = function newFn(path) {
if (wrapper.checkPath(path)) {
return fn.apply(this, arguments);
}
};
newFn.toString = function () {
return fn.toString();
};
return newFn;
}
function shouldIgnoreKey(key) {
if (key[0] === "_") return true;
if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
if (key === "blacklist" || key === "noScope" || key === "skipKeys") return true;
return false;
}
function mergePair(dest, src) {
for (var key in src) {
dest[key] = [].concat(dest[key] || [], src[key]);
}
}