Skip to content

Commit

Permalink
Move complexity check to ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
ljqx authored and ariya committed May 8, 2021
1 parent 2f5cfd7 commit 977b1f8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"parserOptions": {
"sourceType": "script"
},
"plugins": ["esprima-internal"],
"rules": {
"esprima-internal/esprima-complexity": "error",
"no-case-declarations": "off",
"no-constant-condition": ["error", { "checkLoops": false }]
},
Expand Down
55 changes: 55 additions & 0 deletions eslint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @fileoverview Counts the cyclomatic complexity of each function of the script. See http://en.wikipedia.org/wiki/Cyclomatic_complexity.
* Counts the number of if, conditional, for, while, try, but not logical, switch/case,
*/

"use strict";

const MAX = 24;

module.exports = {
rules: {
"esprima-complexity": {
create(context) {
const fns = [];

function startFunction() {
fns.push(1);
}

function endFunction(node) {
const name = node.id ? node.id.name : `:${node.loc.start.line}`;
const complexity = fns.pop();

if (complexity > MAX) {
context.report(node, `${name} has a Cyclomatic complexity of ${complexity}, treshold of ${MAX} is exceeded!`);
}
}

function increaseComplexity() {
if (fns.length) {
fns[fns.length - 1]++;
}
}

return {
FunctionDeclaration: startFunction,
FunctionExpression: startFunction,
ArrowFunctionExpression: startFunction,
"FunctionDeclaration:exit": endFunction,
"FunctionExpression:exit": endFunction,
"ArrowFunctionExpression:exit": endFunction,

CatchClause: increaseComplexity,
ConditionalExpression: increaseComplexity,
ForStatement: increaseComplexity,
ForInStatement: increaseComplexity,
ForOfStatement: increaseComplexity,
IfStatement: increaseComplexity,
WhileStatement: increaseComplexity,
DoWhileStatement: increaseComplexity
};
}
}
}
};
5 changes: 5 additions & 0 deletions eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "eslint-plugin-esprima-internal",
"version": "0.0.1",
"main": "index.js"
}
60 changes: 4 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"@typescript-eslint/eslint-plugin": "^2.14.0",
"@typescript-eslint/parser": "^2.14.0",
"codecov.io": "~0.1.6",
"escomplex-js": "1.2.0",
"eslint": "^6.8.0",
"eslint-plugin-esprima-internal": "file:eslint",
"everything.js": "~1.0.3",
"glob": "~7.1.0",
"istanbul": "~0.4.0",
Expand Down Expand Up @@ -68,8 +68,7 @@
"lint-code": "eslint src/*.ts",
"code-style": "tsfmt --verify src/*.ts && tsfmt --verify test/*.js",
"format-code": "tsfmt -r src/*.ts && tsfmt -r test/*.js",
"complexity": "node test/check-complexity.js",
"static-analysis": "npm run check-version && npm run lint-code && npm run code-style && npm run complexity",
"static-analysis": "npm run check-version && npm run lint-code && npm run code-style",
"hostile-env-tests": "node test/hostile-environment-tests.js",
"unit-tests": "node test/unit-tests.js",
"api-tests": "mocha -R dot test/api-tests.js",
Expand Down
57 changes: 0 additions & 57 deletions test/check-complexity.js

This file was deleted.

0 comments on commit 977b1f8

Please sign in to comment.