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