context.js
3.58 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
152
153
154
155
156
"use strict";
exports.__esModule = true;
// 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 }; }
// istanbul ignore next
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _path = require("./path");
var _path2 = _interopRequireDefault(_path);
var _types = require("../types");
var t = _interopRequireWildcard(_types);
/**
* [Please add a description.]
*/
var TraversalContext = (function () {
function TraversalContext(scope, opts, state, parentPath) {
_classCallCheck(this, TraversalContext);
this.queue = null;
this.parentPath = parentPath;
this.scope = scope;
this.state = state;
this.opts = opts;
}
/**
* [Please add a description.]
*/
TraversalContext.prototype.shouldVisit = function shouldVisit(node) {
var opts = this.opts;
if (opts.enter || opts.exit) return true;
if (opts[node.type]) return true;
var keys = t.VISITOR_KEYS[node.type];
if (!keys || !keys.length) return false;
var _arr = keys;
for (var _i = 0; _i < _arr.length; _i++) {
var key = _arr[_i];
if (node[key]) return true;
}
return false;
};
/**
* [Please add a description.]
*/
TraversalContext.prototype.create = function create(node, obj, key, listKey) {
var path = _path2["default"].get({
parentPath: this.parentPath,
parent: node,
container: obj,
key: key,
listKey: listKey
});
path.unshiftContext(this);
return path;
};
/**
* [Please add a description.]
*/
TraversalContext.prototype.visitMultiple = function visitMultiple(container, parent, listKey) {
// nothing to traverse!
if (container.length === 0) return false;
var visited = [];
var queue = this.queue = [];
var stop = false;
// build up initial queue
for (var key = 0; key < container.length; key++) {
var self = container[key];
if (self && this.shouldVisit(self)) {
queue.push(this.create(parent, container, key, listKey));
}
}
// visit the queue
var _arr2 = queue;
for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
var path = _arr2[_i2];
path.resync();
if (visited.indexOf(path.node) >= 0) continue;
visited.push(path.node);
if (path.visit()) {
stop = true;
break;
}
}
var _arr3 = queue;
for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
var path = _arr3[_i3];
path.shiftContext();
}
this.queue = null;
return stop;
};
/**
* [Please add a description.]
*/
TraversalContext.prototype.visitSingle = function visitSingle(node, key) {
if (this.shouldVisit(node[key])) {
var path = this.create(node, node, key);
path.visit();
path.shiftContext();
}
};
/**
* [Please add a description.]
*/
TraversalContext.prototype.visit = function visit(node, key) {
var nodes = node[key];
if (!nodes) return;
if (Array.isArray(nodes)) {
return this.visitMultiple(nodes, node, key);
} else {
return this.visitSingle(node, key);
}
};
return TraversalContext;
})();
exports["default"] = TraversalContext;
module.exports = exports["default"];