-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
248 lines (221 loc) · 6.83 KB
/
index.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
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
var Promise = require("bluebird");
var glob = Promise.promisify(require("glob"));
var readFile = Promise.promisify(require('fs').readFile);
var writeFile = Promise.promisify(require('fs').writeFile);
var babylon = require("babylon");
var sloc = require('sloc');
var _ = require('lodash');
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
}
function getASTNodeList(ast) {
return getObjects(ast, 'type', '')
.map(obj => obj.type)
.filter(type => !(/Comment/).test(type));
}
function findPattern(pattern, ast) {
try {
if(pattern(ast)) { return ast; }
} catch(e) {}
for (var i in ast) {
if (!ast.hasOwnProperty(i)) continue;
if (typeof ast[i] == 'object') {
var node = findPattern(pattern, ast[i]);
if(node) { return node; }
}
}
}
function getSloc(filePath) {
return readFile(filePath, 'utf8')
.then(function(source) {
var stats = sloc(source, "js");
return stats.source;
});
}
function parseSource(source) {
return babylon.parse(source, {
sourceType: "module",
plugins: [
"estree",
"jsx",
"flow",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport"
]
}).program;
}
function getAllASTNodes(filePath, pattern) {
return readFile(filePath, 'utf8')
.then(parseSource)
.then(findPattern.bind(null, pattern || () => true))
.then(function(tree) {
return getASTNodeList(tree);
});
}
function getResults(spec) {
return glob(spec.globPattern)
.map(filePath => {
return new Promise(resolve => {
Promise.join(
getSloc(filePath),
getAllASTNodes(filePath).then(list => list.length),
(sloc, astNodes) => resolve({filePath, sloc, astNodes})
);
});
});
}
// signals plain
var SIGNALS_PLAIN = {
name: "signals plain",
globPattern: "projects/programming-signals-plain/transform/*.template.js"
};
// signals plain detection
var SIGNALS_PLAIN_DETECTION = {
name: "signals plain detection",
globPattern: "projects/programming-signals-plain-detection/transform/*.template.js"
};
// constraints plain
var CONSTRAINTS_PLAIN = {
name: "constraints plain",
globPattern: "projects/programming-constraints-plain/{cassowary,babel-plugin-cassowary-transform}.js"
};
// constraints plain detection
var CONSTRAINTS_PLAIN_DETECTION = {
name: "constraints plain detection",
globPattern: "projects/programming-constraints-plain-detection/{cassowary,babel-plugin-cassowary-transform}.js"
};
// roq plain
var ROQ_PLAIN = {
name: "roq plain",
globPattern: "projects/programming-roq-plain/src/**/*.js"
};
// roq plain reaction
var ROQ_PLAIN_REACTION = {
name: "roq plain reaction",
globPattern: "projects/programming-roq-plain-reaction/src/**/*.js"
};
// ila plain
var ILA_PLAIN = {
name: "ila plain",
globPattern: "projects/programming-contextjs-plain/src/Layers.js"
};
// signals aexpr
var SIGNALS_AEXPR = {
name: "signals aexpr",
globPattern: "projects/programming-signals-aexpr/transform/*.extracted.js"
};
// signals aexpr detection
var SIGNALS_AEXPR_DETECTION = {
name: "signals aexpr detection",
globPattern: "projects/programming-signals-aexpr-detection/transform/*.extracted.js"
};
// constraint aexpr
var CONSTRAINTS_AEXPR = {
name: "constraints aexpr",
globPattern: "projects/babel-plugin-always-constraint/index.js"
};
// constraint aexpr
var CONSTRAINTS_AEXPR_DETECTION = {
name: "constraints aexpr detection",
globPattern: "projects/babel-plugin-always-constraint-detection/index.js"
};
// roq aexpr
var ROQ_AEXPR = {
name: "roq aexpr",
globPattern: "projects/reactive-object-queries/src/*.js"
};
// roq aexpr reaction
var ROQ_AEXPR_REACTION = {
name: "roq aexpr reaction",
globPattern: "projects/reactive-object-queries-reaction/src/*.js"
};
// ila aexpr
var ILA_AEXPR = {
name: "ila aexpr",
globPattern: "projects/programming-contextjs-aexpr/src/Layers.js"
};
// ila aexpr reaction
var ILA_AEXPR_REACTION = {
name: "ila aexpr reaction",
globPattern: "projects/programming-contextjs-aexpr-reaction/src/Layers.js"
};
// ila aexpr
var CONTEXTJS = {
name: "ContextJS",
globPattern: "projects/ContextJS/src/Layers.js"
};
Promise.resolve([
SIGNALS_PLAIN,
SIGNALS_PLAIN_DETECTION,
CONSTRAINTS_PLAIN,
CONSTRAINTS_PLAIN_DETECTION,
ROQ_PLAIN,
ROQ_PLAIN_REACTION,
ILA_PLAIN,
SIGNALS_AEXPR,
SIGNALS_AEXPR_DETECTION,
CONSTRAINTS_AEXPR,
CONSTRAINTS_AEXPR_DETECTION,
ROQ_AEXPR,
ROQ_AEXPR_REACTION,
ILA_AEXPR,
ILA_AEXPR_REACTION,
CONTEXTJS
])
.map(spec => {
return getResults(spec).then(results => {
var fullSloc = _(results).map('sloc').sum();
var fullAstNodes = _(results).map('astNodes').sum();
return {
name: spec.name,
fullAstNodes,
fullSloc
};
});
})
.then(results => {
results.forEach(result => {
console.log(result.name + ' ' + result.fullAstNodes + '[' + result.fullSloc + ']');
});
})
.then(() => {
// roq plain detection
// roq aexpr detection #ast-nodes
return readFile('projects/reactive-object-queries/src/select.js', 'utf8')
.then(parseSource)
.then(findPattern.bind(null, node => node.type === "ImportDeclaration" &&
node.specifiers[0].local.name === 'trigger'
))
.tap(ast => {
var list = getASTNodeList(ast);
console.log(list, list.length);
})
.then(ast => {
writeFile('res.ast', JSON.stringify(ast, null, 2));
//projects/reactive-object-queries/src/*.js
});
});