source: trip-planner-front/node_modules/@webassemblyjs/ast/lib/traverse.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.traverse = traverse;
7
8var _nodePath = require("./node-path");
9
10var _nodes = require("./nodes");
11
12// recursively walks the AST starting at the given node. The callback is invoked for
13// and object that has a 'type' property.
14function walk(context, callback) {
15 var stop = false;
16
17 function innerWalk(context, callback) {
18 if (stop) {
19 return;
20 }
21
22 var node = context.node;
23
24 if (node === undefined) {
25 console.warn("traversing with an empty context");
26 return;
27 }
28
29 if (node._deleted === true) {
30 return;
31 }
32
33 var path = (0, _nodePath.createPath)(context);
34 callback(node.type, path);
35
36 if (path.shouldStop) {
37 stop = true;
38 return;
39 }
40
41 Object.keys(node).forEach(function (prop) {
42 var value = node[prop];
43
44 if (value === null || value === undefined) {
45 return;
46 }
47
48 var valueAsArray = Array.isArray(value) ? value : [value];
49 valueAsArray.forEach(function (childNode) {
50 if (typeof childNode.type === "string") {
51 var childContext = {
52 node: childNode,
53 parentKey: prop,
54 parentPath: path,
55 shouldStop: false,
56 inList: Array.isArray(value)
57 };
58 innerWalk(childContext, callback);
59 }
60 });
61 });
62 }
63
64 innerWalk(context, callback);
65}
66
67var noop = function noop() {};
68
69function traverse(node, visitors) {
70 var before = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
71 var after = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : noop;
72 Object.keys(visitors).forEach(function (visitor) {
73 if (!_nodes.nodeAndUnionTypes.includes(visitor)) {
74 throw new Error("Unexpected visitor ".concat(visitor));
75 }
76 });
77 var context = {
78 node: node,
79 inList: false,
80 shouldStop: false,
81 parentPath: null,
82 parentKey: null
83 };
84 walk(context, function (type, path) {
85 if (typeof visitors[type] === "function") {
86 before(type, path);
87 visitors[type](path);
88 after(type, path);
89 }
90
91 var unionTypes = _nodes.unionTypesMap[type];
92
93 if (!unionTypes) {
94 throw new Error("Unexpected node type ".concat(type));
95 }
96
97 unionTypes.forEach(function (unionType) {
98 if (typeof visitors[unionType] === "function") {
99 before(unionType, path);
100 visitors[unionType](path);
101 after(unionType, path);
102 }
103 });
104 });
105}
Note: See TracBrowser for help on using the repository browser.