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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528 | 1
1
1
1
1
93
12
12
1
1
1
80
731606
80
1
1
81
7762
7762
7762
13894
7762
1
1
1
59
90
90
1
13
13
879915
13
1
63
32
63
1
23
5
18
18
1
10
10
33
17
33
7
26
9
9
9
10
10
1
121816
121816
1843
1855
1587
121816
1
29
29
29
29
1
1
1
1
8317
7701
7078
7078
7078
7078
1576
1
3514
1
566
565
565
565
565
204
565
565
44
565
1
137
137
1
28
21
7
7
7
1
1
6
1
1
7
2
7
7
7
1
38
38
38
38
38
38
1
137
101
36
36
36
36
36
36
36
1
8697
8697
8697
8697
14197
14197
14195
14135
14135
14135
14135
8692
5443
2747
3029
3029
2747
2747
2696
2590
3138
3138
8697
8697
1
1
1
4483
1
150
1
1
209
418
209
1
2609
5218
2609
1
159
159
159
159
159
159
1
224
1
158
| var esutils = require("esutils");
var _ = require("lodash");
var t = exports;
t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", "String"];
//
var addAssert = function (type, is) {
t["assert" + type] = function (node, opts) {
opts = opts || {};
Iif (!is(node, opts)) {
throw new Error("Expected type " + JSON.stringify(type) + " with option " + JSON.stringify(opts));
}
};
};
t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body"];
//
t.VISITOR_KEYS = require("./visitor-keys");
_.each(t.VISITOR_KEYS, function (keys, type) {
var is = t["is" + type] = function (node, opts) {
return node && node.type === type && t.shallowEqual(node, opts);
};
addAssert(type, is);
});
//
t.BUILDER_KEYS = _.defaults(require("./builder-keys"), t.VISITOR_KEYS);
_.each(t.BUILDER_KEYS, function (keys, type) {
t[type[0].toLowerCase() + type.slice(1)] = function () {
var args = arguments;
var node = { type: type };
_.each(keys, function (key, i) {
node[key] = args[i];
});
return node;
};
});
//
t.ALIAS_KEYS = require("./alias-keys");
t.FLIPPED_ALIAS_KEYS = {};
_.each(t.ALIAS_KEYS, function (aliases, type) {
_.each(aliases, function (alias) {
var types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [];
types.push(type);
});
});
_.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
t[type.toUpperCase() + "_TYPES"] = types;
var is = t["is" + type] = function (node, opts) {
return node && types.indexOf(node.type) >= 0 && t.shallowEqual(node, opts);
};
addAssert(type, is);
});
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
t.toComputedKey = function (node, key) {
if (!node.computed) {
if (t.isIdentifier(key)) key = t.literal(key.name);
}
return key;
};
/*
* Shallowly checks to see if the passed `node` will evaluate to a
* falsy. This is if `node` is a `Literal` and `value` is falsy or
* `node` is an `Identifier` with a name of `undefiend`.
*
* @param {Object} node
* @returns {Boolean}
*/
t.isFalsyExpression = function (node) {
if (t.isLiteral(node)) {
return !node.value;
} else Eif (t.isIdentifier(node)) {
return node.name === "undefined";
}
return false;
};
/**
* Turn an array of statement `nodes` into a `SequenceExpression`.
*
* Variable declarations are turned into simple assignments and their
* declarations hoisted to the top of the current scope.
*
* Expression statements are just resolved to their standard expression.
*/
t.toSequenceExpression = function (nodes, scope) {
var exprs = [];
_.each(nodes, function (node) {
if (t.isExpression(node)) {
exprs.push(node);
} if (t.isExpressionStatement(node)) {
exprs.push(node.expression);
} else if (t.isVariableDeclaration(node)) {
_.each(node.declarations, function (declar) {
scope.push({
kind: node.kind,
key: declar.id.name,
id: declar.id
});
exprs.push(t.assignmentExpression("=", declar.id, declar.init));
});
}
});
Iif (exprs.length === 1) {
return exprs[0];
} else {
return t.sequenceExpression(exprs);
}
};
//
t.shallowEqual = function (actual, expected) {
var same = true;
if (expected) {
_.each(expected, function (val, key) {
if (actual[key] !== val) {
return same = false;
}
});
}
return same;
};
/**
* Description
*
* @param {Object} member
* @param {Object} append
* @param {Boolean} [computed]
* @returns {Object} member
*/
t.appendToMemberExpression = function (member, append, computed) {
member.object = t.memberExpression(member.object, member.property, member.computed);
member.property = append;
member.computed = !!computed;
return member;
};
/**
* Description
*
* @param {Object} member
* @param {Object} append
* @returns {Object} member
*/
t.prependToMemberExpression = function (member, append) {
member.object = t.memberExpression(append, member.object);
return member;
};
/**
* Description
*
* @param {Object} node
* @param {Object} parent
* @returns {Boolean}
*/
t.isReferenced = function (node, parent) {
// we're a property key and we aren't computed so we aren't referenced
if (t.isProperty(parent) && parent.key === node && !parent.computed) return false;
// we're a variable declarator id so we aren't referenced
if (t.isVariableDeclarator(parent) && parent.id === node) return false;
var isMemberExpression = t.isMemberExpression(parent);
// we're in a member expression and we're the computed property so we're referenced
var isComputedProperty = isMemberExpression && parent.property === node && parent.computed;
// we're in a member expression and we're the object so we're referenced
var isObject = isMemberExpression && parent.object === node;
// we are referenced
if (!isMemberExpression || isComputedProperty || isObject) return true;
return false;
};
/**
* Description
*
* @param {String} name
* @returns {Boolean}
*/
t.isValidIdentifier = function (name) {
return _.isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isReservedWordES6(name, true);
};
/*
* Description
*
* @param {String} name
* @returns {String}
*/
t.toIdentifier = function (name) {
if (t.isIdentifier(name)) return name.name;
name = name + "";
// replace all non-valid identifiers with dashes
name = name.replace(/[^a-zA-Z0-9$_]/g, "-");
// remove all dashes and numbers from start of name
name = name.replace(/^[-0-9]+/, "");
// camel case
name = name.replace(/[-_\s]+(.)?/g, function (match, c) {
return c ? c.toUpperCase() : "";
});
// remove underscores from start of name
name = name.replace(/^\_/, "");
if (!t.isValidIdentifier(name)) {
name = "_" + name;
}
return name || '_';
};
/**
* Description
*
* @param {Object} node
* @param {String} key
*/
t.ensureBlock = function (node, key) {
key = key || "body";
node[key] = t.toBlock(node[key], node);
};
/**
* Description
*
* @param {Object} node
* @param {Boolean} [ignore]
* @returns {Object|Boolean}
*/
t.toStatement = function (node, ignore) {
if (t.isStatement(node)) {
return node;
}
var mustHaveId = false;
var newType;
if (t.isClass(node)) {
mustHaveId = true;
newType = "ClassDeclaration";
} else if (t.isFunction(node)) {
mustHaveId = true;
newType = "FunctionDeclaration";
}
if (mustHaveId && !node.id) {
newType = false;
}
Eif (!newType) {
Eif (ignore) {
return false;
} else {
throw new Error("cannot turn " + node.type + " to a statement");
}
}
node.type = newType;
return node;
};
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
exports.toExpression = function (node) {
Iif (t.isExpressionStatement(node)) {
node = node.expression;
}
Iif (t.isClass(node)) {
node.type = "ClassExpression";
} else Eif (t.isFunction(node)) {
node.type = "FunctionExpression";
}
Eif (t.isExpression(node)) {
return node;
} else {
throw new Error("cannot turn " + node.type + " to an expression");
}
};
/**
* Description
*
* @param {Object} node
* @param {Object} parent
* @returns {Object}
*/
t.toBlock = function (node, parent) {
if (t.isBlockStatement(node)) {
return node;
}
Iif (t.isEmptyStatement(node)) {
node = [];
}
Eif (!Array.isArray(node)) {
Eif (!t.isStatement(node)) {
Eif (t.isFunction(parent)) {
node = t.returnStatement(node);
} else {
node = t.expressionStatement(node);
}
}
node = [node];
}
return t.blockStatement(node);
};
/**
* Description
*
* @param {Object} node
* @param {Boolean} [map]
* @param {Array} [ignoreTypes]
* @returns {Array|Object}
*/
t.getIds = function (node, map, ignoreTypes) {
ignoreTypes = ignoreTypes || [];
var search = [].concat(node);
var ids = {};
while (search.length) {
var id = search.shift();
if (!id) continue;
if (_.contains(ignoreTypes, id.type)) continue;
var nodeKeys = t.getIds.nodes[id.type];
var arrKeys = t.getIds.arrays[id.type];
var i, key;
if (t.isIdentifier(id)) {
ids[id.name] = id;
} else if (nodeKeys) {
for (i in nodeKeys) {
key = nodeKeys[i];
if (id[key]) {
search.push(id[key]);
break;
}
}
} else if (arrKeys) {
for (i in arrKeys) {
key = arrKeys[i];
search = search.concat(id[key] || []);
}
}
}
if (!map) ids = _.keys(ids);
return ids;
};
t.getIds.nodes = {
AssignmentExpression: ["left"],
ImportBatchSpecifier: ["name"],
ImportSpecifier: ["name", "id"],
ExportSpecifier: ["name", "id"],
VariableDeclarator: ["id"],
FunctionDeclaration: ["id"],
ClassDeclaration: ["id"],
MemeberExpression: ["object"],
SpreadElement: ["argument"],
Property: ["value"]
};
t.getIds.arrays = {
ExportDeclaration: ["specifiers", "declaration"],
ImportDeclaration: ["specifiers"],
VariableDeclaration: ["declarations"],
ArrayPattern: ["elements"],
ObjectPattern: ["properties"]
};
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isLet = function (node) {
return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let);
};
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isVar = function (node) {
return t.isVariableDeclaration(node, { kind: "var" }) && !node._let;
};
//
t.COMMENT_KEYS = ["leadingComments", "trailingComments"];
/**
* Description
*
* @param {Object} child
* @returns {Object} child
*/
t.removeComments = function (child) {
_.each(t.COMMENT_KEYS, function (key) {
delete child[key];
});
return child;
};
/**
* Description
*
* @param {Object} child
* @param {Object} parent
* @returns {Object} child
*/
t.inheritsComments = function (child, parent) {
_.each(t.COMMENT_KEYS, function (key) {
child[key] = _.uniq(_.compact([].concat(child[key], parent[key])));
});
return child;
};
/**
* Description
*
* @param {Object} child
* @param {Object} parent
* @returns {Object} child
*/
t.inherits = function (child, parent) {
child.loc = parent.loc;
child.end = parent.end;
child.range = parent.range;
child.start = parent.start;
t.inheritsComments(child, parent);
return child;
};
/**
* Description
*
* @param {Object} specifier
* @returns {String}
*/
t.getSpecifierName = function (specifier) {
return specifier.name || specifier.id;
};
/**
* Description
*
* @param {Object} specifier
* @returns {Boolean}
*/
t.isSpecifierDefault = function (specifier) {
return t.isIdentifier(specifier.id) && specifier.id.name === "default";
};
|