1 | "use strict";
|
2 |
|
3 | const is = require("simple-is");
|
4 |
|
5 | function traverse(root, options) {
|
6 | options = options || {};
|
7 | const pre = options.pre;
|
8 | const post = options.post;
|
9 | const cleanup = options.cleanup;
|
10 |
|
11 | function visit(node, parent) {
|
12 | if (!node || !is.string(node.type)) {
|
13 | return;
|
14 | }
|
15 |
|
16 | if (!cleanup && !is.own(node, "$parent")) {
|
17 | node.$parent = parent;
|
18 | }
|
19 |
|
20 | let res = undefined;
|
21 | if (pre) {
|
22 | res = pre(node);
|
23 | }
|
24 |
|
25 | if (res !== false) {
|
26 | for (let prop in node) {
|
27 | if (prop[0] === "$") {
|
28 | if (cleanup) {
|
29 | delete node[prop];
|
30 | }
|
31 | continue;
|
32 | }
|
33 |
|
34 | const child = node[prop];
|
35 |
|
36 | if (Array.isArray(child)) {
|
37 | for (let i = 0; i < child.length; i++) {
|
38 | visit(child[i], node);
|
39 | }
|
40 | } else {
|
41 | visit(child, node);
|
42 | }
|
43 | }
|
44 | }
|
45 |
|
46 | if (post) {
|
47 | post(node);
|
48 | }
|
49 | }
|
50 |
|
51 | visit(root, null);
|
52 | };
|
53 | module.exports = traverse;
|