Skip to content

Commit 977b1f8

Browse files
ljqxariya
authored andcommitted
Move complexity check to ESLint
1 parent 2f5cfd7 commit 977b1f8

File tree

6 files changed

+68
-116
lines changed

6 files changed

+68
-116
lines changed

.eslintrc.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"parserOptions": {
1313
"sourceType": "script"
1414
},
15+
"plugins": ["esprima-internal"],
1516
"rules": {
17+
"esprima-internal/esprima-complexity": "error",
1618
"no-case-declarations": "off",
1719
"no-constant-condition": ["error", { "checkLoops": false }]
1820
},

eslint/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @fileoverview Counts the cyclomatic complexity of each function of the script. See http://en.wikipedia.org/wiki/Cyclomatic_complexity.
3+
* Counts the number of if, conditional, for, while, try, but not logical, switch/case,
4+
*/
5+
6+
"use strict";
7+
8+
const MAX = 24;
9+
10+
module.exports = {
11+
rules: {
12+
"esprima-complexity": {
13+
create(context) {
14+
const fns = [];
15+
16+
function startFunction() {
17+
fns.push(1);
18+
}
19+
20+
function endFunction(node) {
21+
const name = node.id ? node.id.name : `:${node.loc.start.line}`;
22+
const complexity = fns.pop();
23+
24+
if (complexity > MAX) {
25+
context.report(node, `${name} has a Cyclomatic complexity of ${complexity}, treshold of ${MAX} is exceeded!`);
26+
}
27+
}
28+
29+
function increaseComplexity() {
30+
if (fns.length) {
31+
fns[fns.length - 1]++;
32+
}
33+
}
34+
35+
return {
36+
FunctionDeclaration: startFunction,
37+
FunctionExpression: startFunction,
38+
ArrowFunctionExpression: startFunction,
39+
"FunctionDeclaration:exit": endFunction,
40+
"FunctionExpression:exit": endFunction,
41+
"ArrowFunctionExpression:exit": endFunction,
42+
43+
CatchClause: increaseComplexity,
44+
ConditionalExpression: increaseComplexity,
45+
ForStatement: increaseComplexity,
46+
ForInStatement: increaseComplexity,
47+
ForOfStatement: increaseComplexity,
48+
IfStatement: increaseComplexity,
49+
WhileStatement: increaseComplexity,
50+
DoWhileStatement: increaseComplexity
51+
};
52+
}
53+
}
54+
}
55+
};

eslint/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "eslint-plugin-esprima-internal",
3+
"version": "0.0.1",
4+
"main": "index.js"
5+
}

package-lock.json

Lines changed: 4 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
"@typescript-eslint/eslint-plugin": "^2.14.0",
3939
"@typescript-eslint/parser": "^2.14.0",
4040
"codecov.io": "~0.1.6",
41-
"escomplex-js": "1.2.0",
4241
"eslint": "^6.8.0",
42+
"eslint-plugin-esprima-internal": "file:eslint",
4343
"everything.js": "~1.0.3",
4444
"glob": "~7.1.0",
4545
"istanbul": "~0.4.0",
@@ -68,8 +68,7 @@
6868
"lint-code": "eslint src/*.ts",
6969
"code-style": "tsfmt --verify src/*.ts && tsfmt --verify test/*.js",
7070
"format-code": "tsfmt -r src/*.ts && tsfmt -r test/*.js",
71-
"complexity": "node test/check-complexity.js",
72-
"static-analysis": "npm run check-version && npm run lint-code && npm run code-style && npm run complexity",
71+
"static-analysis": "npm run check-version && npm run lint-code && npm run code-style",
7372
"hostile-env-tests": "node test/hostile-environment-tests.js",
7473
"unit-tests": "node test/unit-tests.js",
7574
"api-tests": "mocha -R dot test/api-tests.js",

test/check-complexity.js

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

0 commit comments

Comments
 (0)