-
Notifications
You must be signed in to change notification settings - Fork 732
/
Copy pathglobals.js
96 lines (92 loc) · 2.99 KB
/
globals.js
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
const { assert } = require('chai');
const cheerio = require('cheerio');
const dot = require('dot-object');
const fs = require('fs');
const path = require('path');
const jscs = require('jscodeshift');
const esprima = require('esprima');
const source = fs.readFileSync(path.join(process.cwd(), 'js/gameoflife.js'), 'utf8');
const ast = jscs(source);
jscs.registerMethods({
findConditional: function() {
return this.find(jscs.ConditionalExpression);
},
findCall: function(name) {
return this.find(jscs.CallExpression).filter(path => {
let callee_name = '';
if (path.value.callee.type === 'Identifier') {
callee_name = path.value.callee.name;
} if (path.value.callee.type === 'MemberExpression') {
callee_name = path.value.callee.property.name;
}
return (callee_name === name) ? true : false;
});
},
findAssignment: function(name) {
return this.find(jscs.AssignmentExpression);
},
findIdentifier: function(name) {
return this.find(jscs.Identifier, { name: name });
},
findPropertyAssignment: function(obj, property) {
return this.find(jscs.AssignmentExpression).filter(path => {
if (path.value.left.type === 'MemberExpression' &&
path.value.left.object.name === obj &&
path.value.left.property.name === property) {
return true;
} else {
return false;
}
});
},
findSides: function(operator) {
const element = this.find(jscs.BinaryExpression, { operator: operator });
if (element.length) {
const left_side = element.get().value.left;
const right_side = element.get().value.right;
const operator_expr = element.get().value.operator;
return { left: left_side, right: right_side, operator: operator_expr }
} else {
return false;
}
},
findIdentifierParent: function(name) {
const element = this.find(jscs.Identifier, { name: name });
if (element.length) {
const parent = element.get().parent.value
if (parent.type === 'VariableDeclarator') {
if (parent.init.type === 'FunctionExpression' || parent.init.type === 'ArrowFunctionExpression') {
return { params: parent.init.params, body: parent.init.body, defaults: parent.init.defaults };
}
return parent;
} else if (parent.type === 'FunctionDeclaration') {
return { params: parent.params, body: parent.body, defaults: parent.defaults };
} else {
return parent;
}
} else {
return false;
}
},
returnParent: function(name) {
const element = this.find(jscs.Identifier, { name: name });
return (element.length) ? jscs(element.get().parent) : [];
},
findBinary: function() {
return this.find(jscs.BinaryExpression);
},
findReturn: function() {
return this.find(jscs.ReturnStatement);
},
findIf: function() {
const element = this.find(jscs.IfStatement);
return (element.length) ? element.get().value : [];
},
});
Object.assign(global, {
source,
assert,
ast,
dot,
jscs
});