1 | var estraverse = require('estraverse');
|
2 | var VISITOR_KEYS = require('cst').visitorKeys;
|
3 |
|
4 | module.exports.iterate = function iterate(node, cb) {
|
5 | if ('type' in node) {
|
6 | estraverse.traverse(node, {
|
7 | enter: function(node, parent) {
|
8 | var parentCollection = [];
|
9 |
|
10 |
|
11 | var path = this.path();
|
12 | if (path) {
|
13 | var collectionKey;
|
14 | while (path.length > 0) {
|
15 | var pathElement = path.pop();
|
16 | if (typeof pathElement === 'string') {
|
17 | collectionKey = pathElement;
|
18 | break;
|
19 | }
|
20 | }
|
21 |
|
22 | parentCollection = parent[collectionKey];
|
23 | if (!Array.isArray(parentCollection)) {
|
24 | parentCollection = [parentCollection];
|
25 | }
|
26 | }
|
27 |
|
28 | if (cb(node, parent, parentCollection) === false) {
|
29 | return estraverse.VisitorOption.Skip;
|
30 | }
|
31 | },
|
32 | keys: VISITOR_KEYS
|
33 | });
|
34 | }
|
35 | };
|