1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = traverse;
|
5 |
|
6 |
|
7 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }
|
8 |
|
9 |
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
12 |
|
13 | var _context = require("./context");
|
14 |
|
15 | var _context2 = _interopRequireDefault(_context);
|
16 |
|
17 | var _visitors = require("./visitors");
|
18 |
|
19 | var visitors = _interopRequireWildcard(_visitors);
|
20 |
|
21 | var _messages = require("../messages");
|
22 |
|
23 | var messages = _interopRequireWildcard(_messages);
|
24 |
|
25 | var _lodashCollectionIncludes = require("lodash/collection/includes");
|
26 |
|
27 | var _lodashCollectionIncludes2 = _interopRequireDefault(_lodashCollectionIncludes);
|
28 |
|
29 | var _types = require("../types");
|
30 |
|
31 | var t = _interopRequireWildcard(_types);
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function traverse(parent, opts, scope, state, parentPath) {
|
38 | if (!parent) return;
|
39 | if (!opts) opts = {};
|
40 |
|
41 | if (!opts.noScope && !scope) {
|
42 | if (parent.type !== "Program" && parent.type !== "File") {
|
43 | throw new Error(messages.get("traverseNeedsParent", parent.type));
|
44 | }
|
45 | }
|
46 |
|
47 | visitors.explode(opts);
|
48 |
|
49 |
|
50 | if (Array.isArray(parent)) {
|
51 | for (var i = 0; i < parent.length; i++) {
|
52 | traverse.node(parent[i], opts, scope, state, parentPath);
|
53 | }
|
54 | } else {
|
55 | traverse.node(parent, opts, scope, state, parentPath);
|
56 | }
|
57 | }
|
58 |
|
59 | traverse.visitors = visitors;
|
60 | traverse.verify = visitors.verify;
|
61 | traverse.explode = visitors.explode;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
|
68 | var keys = t.VISITOR_KEYS[node.type];
|
69 | if (!keys) return;
|
70 |
|
71 | var context = new _context2["default"](scope, opts, state, parentPath);
|
72 | var _arr = keys;
|
73 | for (var _i = 0; _i < _arr.length; _i++) {
|
74 | var key = _arr[_i];
|
75 | if (skipKeys && skipKeys[key]) continue;
|
76 | if (context.visit(node, key)) return;
|
77 | }
|
78 | };
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | var CLEAR_KEYS = t.COMMENT_KEYS.concat(["_scopeInfo", "_paths", "tokens", "comments", "start", "end", "loc", "raw", "rawValue"]);
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | traverse.clearNode = function (node) {
|
91 | for (var i = 0; i < CLEAR_KEYS.length; i++) {
|
92 | var key = CLEAR_KEYS[i];
|
93 | if (node[key] != null) node[key] = undefined;
|
94 | }
|
95 | };
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | var clearVisitor = {
|
102 | noScope: true,
|
103 | exit: traverse.clearNode
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | traverse.removeProperties = function (tree) {
|
111 | traverse(tree, clearVisitor);
|
112 | traverse.clearNode(tree);
|
113 |
|
114 | return tree;
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function hasBlacklistedType(node, parent, scope, state) {
|
122 | if (node.type === state.type) {
|
123 | state.has = true;
|
124 | this.skip();
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | traverse.hasType = function (tree, scope, type, blacklistTypes) {
|
133 |
|
134 | if (_lodashCollectionIncludes2["default"](blacklistTypes, tree.type)) return false;
|
135 |
|
136 |
|
137 | if (tree.type === type) return true;
|
138 |
|
139 | var state = {
|
140 | has: false,
|
141 | type: type
|
142 | };
|
143 |
|
144 | traverse(tree, {
|
145 | blacklist: blacklistTypes,
|
146 | enter: hasBlacklistedType
|
147 | }, scope, state);
|
148 |
|
149 | return state.has;
|
150 | };
|
151 | module.exports = exports["default"]; |
\ | No newline at end of file |