-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute.js
107 lines (72 loc) · 2.21 KB
/
compute.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
let ops = require("./ops.js");
function analyse(ast) {
let results = {cyclomaticComplexity: 1};
function branchPaths(paths1, paths2, isBranch=0) {
results.cyclomaticComplexity += (1*isBranch);
return ops.mergePaths(paths1, paths2);
}
function max (a, b) {
return a > b ? a : b;
}
function compute(program){
let paths = [0];
let nestDepth = 0;
let computed = {};
let incremented = false;
if (program.body.statements === undefined) console.log (program);
program.body.statements.forEach(statement => {
paths = branchPaths(paths, [1]);
if (statement.type=="expression_statement") {
if (statement.expression.right.type=="function_call"){
let calledFunction =
statement.expression.right.
identifier.specifier.identifier;
let calledProgram = ops.findFunction(ast, calledFunction);
if ( calledProgram !== null)
{
computed = compute(calledProgram);
nestDepth = max(computed.nestDepth, nestDepth);
paths = branchPaths(paths, computed.paths);
}
}
}
if (statement.type =="if_statement"){
computed = compute(statement);
nestDepth = max(computed.nestDepth, nestDepth);
if (!incremented) {
nestDepth++;
incremented = true;
}
let ifPaths = branchPaths(paths, computed.paths, 1);
let elsePaths = paths;
if (statement.else !== undefined){
if (statement.else[1].type === "compound_statement")
{
computed =compute({type: "else_statement", body: statement.else[1]});
nestDepth = max(computed.nestDepth, nestDepth);
elsePaths = branchPaths(paths, computed.paths );
}
else {
const elseIfProgram = {
type: "else_if_stmt",
body: {
statements: [statement.else[1]]
}
};
computed = compute(elseIfProgram);
nestDepth = max(computed.nestDepth, nestDepth);
elsePaths = branchPaths(paths, computed.paths);
}
}
paths = paths.concat(ifPaths, elsePaths);
}
});
return {paths, nestDepth};
}
let program = ops.findFunction(ast, "main");
let computed = compute(program);
results.depthOfNesting= computed.nestDepth;
results.paths = computed.paths;
return results;
}
module.exports = { analyse };