UNPKG

1.95 kBJavaScriptView Raw
1var estraverse = require('estraverse');
2var assign = require('lodash').assign;
3var VISITOR_KEYS = require('./visitor-keys');
4
5var keys = assign({}, VISITOR_KEYS, estraverse.VisitorKeys, {
6 // These are deprecated, need to remove it in the future
7 XJSIdentifier: [],
8 XJSNamespacedName: ['namespace', 'name'],
9 XJSMemberExpression: ['object', 'property'],
10 XJSEmptyExpression: [],
11 XJSExpressionContainer: ['expression'],
12 XJSElement: ['openingElement', 'closingElement', 'children'],
13 XJSClosingElement: ['name'],
14 XJSOpeningElement: ['name', 'attributes'],
15 XJSAttribute: ['name', 'value'],
16 XJSSpreadAttribute: ['argument'],
17 XJSText: null,
18
19 // babel-core extends the estraverse "VisitorKeys" property with old TryStatement path
20 // We need to revert it back
21 TryStatement: ['block', 'handler', 'finalizer']
22});
23
24module.exports.iterate = function iterate(node, cb) {
25 if ('type' in node) {
26 estraverse.traverse(node, {
27 enter: function(node, parent) {
28 var parentCollection = [];
29
30 // parentCollection support
31 var path = this.path();
32 if (path) {
33 var collectionKey;
34 while (path.length > 0) {
35 var pathElement = path.pop();
36 if (typeof pathElement === 'string') {
37 collectionKey = pathElement;
38 break;
39 }
40 }
41
42 parentCollection = parent[collectionKey];
43 if (!Array.isArray(parentCollection)) {
44 parentCollection = [parentCollection];
45 }
46 }
47
48 if (cb(node, parent, parentCollection) === false) {
49 return estraverse.VisitorOption.Skip;
50 }
51 },
52 keys: keys
53 });
54 }
55};