1 |
|
2 | var visitorKeys = {
|
3 | VashProgram: ['body'],
|
4 | VashBlock: ['head', 'values', 'tail'],
|
5 | VashExplicitExpression: ['values'],
|
6 | VashExpression: ['values'],
|
7 | VashIndexExpression: ['values'],
|
8 | VashMarkup: ['attributes', 'values'],
|
9 | VashMarkupAttribute: ['left', 'right'],
|
10 | VashText: []
|
11 | }
|
12 |
|
13 | module.exports = function traverse(node, callbacks) {
|
14 |
|
15 | (function walk(node, parent) {
|
16 |
|
17 | if (!node.parent) {
|
18 | node.parent = parent;
|
19 | }
|
20 |
|
21 | var ret = callbacks.enter(node);
|
22 |
|
23 | var candidateKeys = visitorKeys[node.type];
|
24 | if (candidateKeys) {
|
25 | candidateKeys.forEach(function(key) {
|
26 | var valueNodes = node[key];
|
27 | if (valueNodes && valueNodes.length) {
|
28 | valueNodes.forEach(function(child) {
|
29 | walk(child, node);
|
30 | })
|
31 | }
|
32 | })
|
33 | }
|
34 |
|
35 | ret = callbacks.leave(node);
|
36 | }(node, null));
|
37 | } |
\ | No newline at end of file |