a631fad155a01264899d13b761e87711df05f69c.svn-base
6.61 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.explode = explode;
exports.verify = verify;
exports.merge = merge;
var virtualTypes = _interopRequireWildcard(require("./path/lib/virtual-types"));
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function explode(visitor) {
if (visitor._exploded) return visitor;
visitor._exploded = true;
for (const nodeType of Object.keys(visitor)) {
if (shouldIgnoreKey(nodeType)) continue;
const parts = nodeType.split("|");
if (parts.length === 1) continue;
const fns = visitor[nodeType];
delete visitor[nodeType];
for (const part of parts) {
visitor[part] = fns;
}
}
verify(visitor);
delete visitor.__esModule;
ensureEntranceObjects(visitor);
ensureCallbackArrays(visitor);
for (const nodeType of Object.keys(visitor)) {
if (shouldIgnoreKey(nodeType)) continue;
const wrapper = virtualTypes[nodeType];
if (!wrapper) continue;
const fns = visitor[nodeType];
for (const type of Object.keys(fns)) {
fns[type] = wrapCheck(wrapper, fns[type]);
}
delete visitor[nodeType];
if (wrapper.types) {
for (const type of wrapper.types) {
if (visitor[type]) {
mergePair(visitor[type], fns);
} else {
visitor[type] = fns;
}
}
} else {
mergePair(visitor, fns);
}
}
for (const nodeType of Object.keys(visitor)) {
if (shouldIgnoreKey(nodeType)) continue;
const fns = visitor[nodeType];
let aliases = t.FLIPPED_ALIAS_KEYS[nodeType];
const deprecratedKey = t.DEPRECATED_KEYS[nodeType];
if (deprecratedKey) {
console.trace(`Visitor defined for ${nodeType} but it has been renamed to ${deprecratedKey}`);
aliases = [deprecratedKey];
}
if (!aliases) continue;
delete visitor[nodeType];
for (const alias of aliases) {
const existing = visitor[alias];
if (existing) {
mergePair(existing, fns);
} else {
visitor[alias] = Object.assign({}, fns);
}
}
}
for (const nodeType of Object.keys(visitor)) {
if (shouldIgnoreKey(nodeType)) continue;
ensureCallbackArrays(visitor[nodeType]);
}
return visitor;
}
function verify(visitor) {
if (visitor._verified) return;
if (typeof visitor === "function") {
throw new Error("You passed `traverse()` a function when it expected a visitor object, " + "are you sure you didn't mean `{ enter: Function }`?");
}
for (const nodeType of Object.keys(visitor)) {
if (nodeType === "enter" || nodeType === "exit") {
validateVisitorMethods(nodeType, visitor[nodeType]);
}
if (shouldIgnoreKey(nodeType)) continue;
if (t.TYPES.indexOf(nodeType) < 0) {
throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`);
}
const visitors = visitor[nodeType];
if (typeof visitors === "object") {
for (const visitorKey of Object.keys(visitors)) {
if (visitorKey === "enter" || visitorKey === "exit") {
validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]);
} else {
throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`);
}
}
}
}
visitor._verified = true;
}
function validateVisitorMethods(path, val) {
const fns = [].concat(val);
for (const fn of fns) {
if (typeof fn !== "function") {
throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`);
}
}
}
function merge(visitors, states = [], wrapper) {
const rootVisitor = {};
for (let i = 0; i < visitors.length; i++) {
const visitor = visitors[i];
const state = states[i];
explode(visitor);
for (const type of Object.keys(visitor)) {
let visitorType = visitor[type];
if (state || wrapper) {
visitorType = wrapWithStateOrWrapper(visitorType, state, wrapper);
}
const nodeVisitor = rootVisitor[type] = rootVisitor[type] || {};
mergePair(nodeVisitor, visitorType);
}
}
return rootVisitor;
}
function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
const newVisitor = {};
for (const key of Object.keys(oldVisitor)) {
let fns = oldVisitor[key];
if (!Array.isArray(fns)) continue;
fns = fns.map(function (fn) {
let newFn = fn;
if (state) {
newFn = function (path) {
return fn.call(state, path, state);
};
}
if (wrapper) {
newFn = wrapper(state.key, key, newFn);
}
if (newFn !== fn) {
newFn.toString = () => fn.toString();
}
return newFn;
});
newVisitor[key] = fns;
}
return newVisitor;
}
function ensureEntranceObjects(obj) {
for (const key of Object.keys(obj)) {
if (shouldIgnoreKey(key)) continue;
const fns = obj[key];
if (typeof fns === "function") {
obj[key] = {
enter: fns
};
}
}
}
function ensureCallbackArrays(obj) {
if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
}
function wrapCheck(wrapper, fn) {
const newFn = function (path) {
if (wrapper.checkPath(path)) {
return fn.apply(this, arguments);
}
};
newFn.toString = () => fn.toString();
return newFn;
}
function shouldIgnoreKey(key) {
if (key[0] === "_") return true;
if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
if (key === "blacklist" || key === "noScope" || key === "skipKeys") {
return true;
}
return false;
}
function mergePair(dest, src) {
for (const key of Object.keys(src)) {
dest[key] = [].concat(dest[key] || [], src[key]);
}
}