-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathanalysis.js
278 lines (245 loc) · 6.9 KB
/
analysis.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
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
var esprima = require("esprima");
var options = {tokens:true, tolerant: true, loc: true, range: true };
var fs = require("fs");
function main()
{
var args = process.argv.slice(2);
if( args.length == 0 )
{
args = ["analysis.js"];
}
var filePath = args[0];
complexity(filePath);
// Report
for( var node in builders )
{
var builder = builders[node];
builder.report();
}
}
var builders = {};
// Represent a reusable "class" following the Builder pattern.
function FunctionBuilder()
{
this.StartLine = 0;
this.FunctionName = "";
// The number of parameters for functions
this.ParameterCount = 0,
// Number of if statements/loops + 1
this.SimpleCyclomaticComplexity = 0;
// The max depth of scopes (nested ifs, loops, etc)
this.MaxNestingDepth = 0;
// The max number of conditions if one decision statement.
this.MaxConditions = 0;
this.report = function()
{
console.log(
(
"{0}(): {1}\n" +
"============\n" +
"SimpleCyclomaticComplexity: {2}\t" +
"MaxNestingDepth: {3}\t" +
"MaxConditions: {4}\t" +
"Parameters: {5}\n\n"
)
.format(this.FunctionName, this.StartLine,
this.SimpleCyclomaticComplexity, this.MaxNestingDepth,
this.MaxConditions, this.ParameterCount)
);
}
};
// A builder for storing file level information.
function FileBuilder()
{
this.FileName = "";
// Number of strings in a file.
this.Strings = 0;
// Number of imports in a file.
this.ImportCount = 0;
this.report = function()
{
console.log (
( "{0}\n" +
"~~~~~~~~~~~~\n"+
"ImportCount {1}\t" +
"Strings {2}\n"
).format( this.FileName, this.ImportCount, this.Strings ));
}
}
// A function following the Visitor pattern.
// Annotates nodes with parent objects.
function traverseWithParents(object, visitor)
{
var key, child;
visitor.call(null, object);
for (key in object) {
if (object.hasOwnProperty(key)) {
child = object[key];
if (typeof child === 'object' && child !== null && key != 'parent')
{
child.parent = object;
traverseWithParents(child, visitor);
}
}
}
}
function complexity(filePath)
{
var buf = fs.readFileSync(filePath, "utf8");
var ast = esprima.parse(buf, options);
var i = 0;
// A file level-builder:
var fileBuilder = new FileBuilder();
fileBuilder.FileName = filePath;
fileBuilder.ImportCount = 0;
builders[filePath] = fileBuilder;
// Tranverse program with a function visitor.
traverseWithParents(ast, function (node)
{
if (node.type === 'FunctionDeclaration')
{
var builder = new FunctionBuilder();
builder.FunctionName = functionName(node);
builder.StartLine = node.loc.start.line;
builders[builder.FunctionName] = builder;
}
});
}
// Helper function for counting children of node.
function childrenLength(node)
{
var key, child;
var count = 0;
for (key in node)
{
if (node.hasOwnProperty(key))
{
child = node[key];
if (typeof child === 'object' && child !== null && key != 'parent')
{
count++;
}
}
}
return count;
}
// Helper function for checking if a node is a "decision type node"
function isDecision(node)
{
if( node.type == 'IfStatement' || node.type == 'ForStatement' || node.type == 'WhileStatement' ||
node.type == 'ForInStatement' || node.type == 'DoWhileStatement')
{
return true;
}
return false;
}
// Helper function for printing out function name.
function functionName( node )
{
if( node.id )
{
return node.id.name;
}
return "anon function @" + node.loc.start.line;
}
// Helper function for allowing parameterized formatting of strings.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
main();
function Crazy (argument)
{
var date_bits = element.value.match(/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/);
var new_date = null;
if(date_bits && date_bits.length == 4 && parseInt(date_bits[2]) > 0 && parseInt(date_bits[3]) > 0)
new_date = new Date(parseInt(date_bits[1]), parseInt(date_bits[2]) - 1, parseInt(date_bits[3]));
var secs = bytes / 3500;
if ( secs < 59 )
{
return secs.toString().split(".")[0] + " seconds";
}
else if ( secs > 59 && secs < 3600 )
{
var mints = secs / 60;
var remainder = parseInt(secs.toString().split(".")[0]) -
(parseInt(mints.toString().split(".")[0]) * 60);
var szmin;
if ( mints > 1 )
{
szmin = "minutes";
}
else
{
szmin = "minute";
}
return mints.toString().split(".")[0] + " " + szmin + " " +
remainder.toString() + " seconds";
}
else
{
var mints = secs / 60;
var hours = mints / 60;
var remainders = parseInt(secs.toString().split(".")[0]) -
(parseInt(mints.toString().split(".")[0]) * 60);
var remainderm = parseInt(mints.toString().split(".")[0]) -
(parseInt(hours.toString().split(".")[0]) * 60);
var szmin;
if ( remainderm > 1 )
{
szmin = "minutes";
}
else
{
szmin = "minute";
}
var szhr;
if ( remainderm > 1 )
{
szhr = "hours";
}
else
{
szhr = "hour";
for ( i = 0 ; i < cfield.value.length ; i++)
{
var n = cfield.value.substr(i,1);
if ( n != 'a' && n != 'b' && n != 'c' && n != 'd'
&& n != 'e' && n != 'f' && n != 'g' && n != 'h'
&& n != 'i' && n != 'j' && n != 'k' && n != 'l'
&& n != 'm' && n != 'n' && n != 'o' && n != 'p'
&& n != 'q' && n != 'r' && n != 's' && n != 't'
&& n != 'u' && n != 'v' && n != 'w' && n != 'x'
&& n != 'y' && n != 'z'
&& n != 'A' && n != 'B' && n != 'C' && n != 'D'
&& n != 'E' && n != 'F' && n != 'G' && n != 'H'
&& n != 'I' && n != 'J' && n != 'K' && n != 'L'
&& n != 'M' && n != 'N' && n != 'O' && n != 'P'
&& n != 'Q' && n != 'R' && n != 'S' && n != 'T'
&& n != 'U' && n != 'V' && n != 'W' && n != 'X'
&& n != 'Y' && n != 'Z'
&& n != '0' && n != '1' && n != '2' && n != '3'
&& n != '4' && n != '5' && n != '6' && n != '7'
&& n != '8' && n != '9'
&& n != '_' && n != '@' && n != '-' && n != '.' )
{
window.alert("Only Alphanumeric are allowed.\nPlease re-enter the value.");
cfield.value = '⠀';
cfield.focus();
}
cfield.value = cfield.value.toUpperCase();
}
return;
}
return hours.toString().split(".")[0] + " " + szhr + " " +
mints.toString().split(".")[0] + " " + szmin;
}
}
exports.complexity = complexity;