UNPKG

3.54 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = traverse;
5// istanbul ignore next
6
7function _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// istanbul ignore next
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
12
13var _context = require("./context");
14
15var _context2 = _interopRequireDefault(_context);
16
17var _visitors = require("./visitors");
18
19var visitors = _interopRequireWildcard(_visitors);
20
21var _messages = require("../messages");
22
23var messages = _interopRequireWildcard(_messages);
24
25var _lodashCollectionIncludes = require("lodash/collection/includes");
26
27var _lodashCollectionIncludes2 = _interopRequireDefault(_lodashCollectionIncludes);
28
29var _types = require("../types");
30
31var t = _interopRequireWildcard(_types);
32
33/**
34 * [Please add a description.]
35 */
36
37function 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 // array of nodes
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
59traverse.visitors = visitors;
60traverse.verify = visitors.verify;
61traverse.explode = visitors.explode;
62
63/**
64 * [Please add a description.]
65 */
66
67traverse.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 * [Please add a description.]
82 */
83
84var CLEAR_KEYS = t.COMMENT_KEYS.concat(["_scopeInfo", "_paths", "tokens", "comments", "start", "end", "loc", "raw", "rawValue"]);
85
86/**
87 * [Please add a description.]
88 */
89
90traverse.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 * [Please add a description.]
99 */
100
101var clearVisitor = {
102 noScope: true,
103 exit: traverse.clearNode
104};
105
106/**
107 * [Please add a description.]
108 */
109
110traverse.removeProperties = function (tree) {
111 traverse(tree, clearVisitor);
112 traverse.clearNode(tree);
113
114 return tree;
115};
116
117/**
118 * [Please add a description.]
119 */
120
121function hasBlacklistedType(node, parent, scope, state) {
122 if (node.type === state.type) {
123 state.has = true;
124 this.skip();
125 }
126}
127
128/**
129 * [Please add a description.]
130 */
131
132traverse.hasType = function (tree, scope, type, blacklistTypes) {
133 // the node we're searching in is blacklisted
134 if (_lodashCollectionIncludes2["default"](blacklistTypes, tree.type)) return false;
135
136 // the type we're looking for is the same as the passed node
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};
151module.exports = exports["default"];
\No newline at end of file