Skip to content

Commit cc973b6

Browse files
committed
fix: clean up
1 parent 5cf812b commit cc973b6

File tree

10 files changed

+28
-146
lines changed

10 files changed

+28
-146
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Cyclomatic JS
2+
3+
Cyclomatic Complexity was developed by a Computer Scientist at IBM named Thomas McCabe in an article called ["A Complexity Measurement"](https://ieeexplore.ieee.org/document/1702388). The approach uses Graph Theory to creates a representation of the number of linear paths possible through the execution of an algorithm. It will produce a simple integer value as an assessment. For example, the following code has an objective Complexity of 2:
4+
5+
```javascript
6+
function main(a) {
7+
if (a > 0) {
8+
return "is positive"
9+
}
10+
11+
return "is negative"
12+
}
13+
```
14+
15+
As code grows, and I'm sure we've all seen it, algorithms can take on a life of their own and grow to contain numerous linear paths that all have to be maintained and amount to the cognitive overhead of a given algorithm.
16+
17+
This library provides any JS library the ability to analyze the Cyclomatic Complexity of their functions with a dependency solely on the [abstract-syntax-tree](https://www.npmjs.com/package/abstract-syntax-tree)

default-export-example.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

example.cjs

Lines changed: 0 additions & 18 deletions
This file was deleted.

example_export_function.cjs

Lines changed: 0 additions & 18 deletions
This file was deleted.

named-export-example.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "forecast-action-js",
3-
"version": "1.0.0",
3+
"version": "0.0.0",
44
"main": "index.js",
55
"type": "module",
66
"license": "MIT",

index.js renamed to src/index.js

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ const typesAddingComplexity = [
5555
"ForInStatement",
5656
"ForOfStatement",
5757
"ForStatement",
58-
"SwitchStatement",
5958
"BinaryExpression",
60-
// "SwitchCase",
6159
"WhileStatement",
6260
"ConditionalExpression",
6361
];
@@ -67,6 +65,10 @@ function increasesComplexity(node) {
6765
return [true, 1];
6866
}
6967

68+
if (node.type === "SwitchCase" && node.consequent?.length > 0) {
69+
return [true, 1];
70+
}
71+
7072
if (
7173
node.type === "LogicalExpression" &&
7274
(node.operator === "||" || node.operator === "&&" || node.operator === "??")
@@ -82,10 +84,11 @@ const resolveBody = {
8284
TryStatement: (node) => [node?.block?.body, node.handler.body.body],
8385
TryStatementHandler: (node) => [],
8486
LogicalExpression: (node) => [[node.left], [node.right]],
85-
ForStatement: (node) => [],
86-
ForOfStatement: (node) => [],
87-
ForInStatement: (node) => [],
88-
SwitchStatement: (node) => [],
87+
ForStatement: (node) => [node.body],
88+
ForOfStatement: (node) => [node.body],
89+
ForInStatement: (node) => [node.body],
90+
SwitchStatement: (node) => [node.cases],
91+
SwitchCase: (node) => [node.body],
8992
WhileStatement: (node) => [],
9093
BinaryExpression: (node) => [],
9194
IfStatement: (node) => [
@@ -110,15 +113,14 @@ function determineLogicalComplexity(bodyInput) {
110113
for (const node of body) {
111114
if (node.type === "ExportNamedDeclaration") {
112115
complexity = 1; // reset clock on each function
113-
console.log(node);
114-
console.log(`export name: ${node.id.lk}`);
115116
if (node.declaration.type === "FunctionDeclaration") {
116117
processNodes([node.declaration.body]);
117118
} else {
118119
findDeclarations(node.declaration);
119120
}
120121
}
121122
const resolvedBody = resolveBody[node.type];
123+
console.log(node)
122124
if (!resolvedBody) continue;
123125
const [shouldIncrease] = increasesComplexity(node);
124126
if (shouldIncrease) {
@@ -149,46 +151,14 @@ function determineLogicalComplexity(bodyInput) {
149151
}
150152
}
151153
processNodes(bodyInput);
152-
// if (node.type === "SwitchCase" || node.type === "SwitchStatement") {
153-
// console.log(JSON.stringify(node, undefined, 2));
154-
// }
155-
156-
// if (node.handler?.type === "CatchClause") {
157-
// complexity++;
158-
// determineLogicalComplexity(node.handler.body.body, complexity);
159-
// }
160-
161-
// if (node.type === "TryStatement") {
162-
// determineLogicalComplexity(node.block.body, complexity);
163-
// determineLogicalComplexity([node.handler?.body?.body]);
164-
// }
165-
166-
// if (node.type === "LogicalExpression") {
167-
// determineLogicalComplexity([node.left], complexity);
168-
// determineLogicalComplexity([node.right], complexity);
169-
170-
// if (node.operator === "||" || node.operator === "&&") {
171-
// complexity++;
172-
// }
173-
// }
174154

175-
// if (node.type === "IfStatement") {
176-
// determineLogicalComplexity([node.test], complexity);
177-
// // consequent and alternate are BlockStatement nodes
178-
// determineLogicalComplexity(node.consequent.body, complexity);
179-
// if (node?.alternate?.body) {
180-
// complexity++;
181-
// determineLogicalComplexity(node.alternate.body, complexity);
182-
// }
183-
// }
184155
return complexity;
185156
}
186157

187158
function calculateComplexity(tree) {
188159
const complexity = determineLogicalComplexity(tree.body);
189160
console.log(complexity);
190161
}
191-
console.log(process.argv[2]);
192162
const file = await readFile(process.argv[2], "utf-8");
193163
const tree = ast.parse(file);
194164
calculateComplexity(tree);
File renamed without changes.

switch-case.js

Whitespace-only changes.

testing-logical-operations.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)