1 | var estraverse = require('estraverse');
|
2 | var assign = require('lodash').assign;
|
3 | var VISITOR_KEYS = require('./visitor-keys');
|
4 |
|
5 | var keys = assign({}, VISITOR_KEYS, estraverse.VisitorKeys, {
|
6 |
|
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 |
|
20 |
|
21 | TryStatement: ['block', 'handler', 'finalizer']
|
22 | });
|
23 |
|
24 | module.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 |
|
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 | };
|