]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/complexity.js
b2355556af9d24f824bd82de5211fa317c61675f
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, switch/case,
4 * @author Patrick Brosset
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const astUtils
= require("./utils/ast-utils");
14 const { upperCaseFirst
} = require("../shared/string-utils");
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
20 /** @type {import('../shared/types').Rule} */
26 description
: "enforce a maximum cyclomatic complexity allowed in a program",
28 url
: "https://eslint.org/docs/rules/complexity"
50 additionalProperties
: false
57 complex
: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}."
62 const option
= context
.options
[0];
66 typeof option
=== "object" &&
67 (Object
.prototype.hasOwnProperty
.call(option
, "maximum") || Object
.prototype.hasOwnProperty
.call(option
, "max"))
69 THRESHOLD
= option
.maximum
|| option
.max
;
70 } else if (typeof option
=== "number") {
74 //--------------------------------------------------------------------------
76 //--------------------------------------------------------------------------
78 // Using a stack to store complexity per code path
79 const complexities
= [];
82 * Increase the complexity of the code path in context
86 function increaseComplexity() {
87 complexities
[complexities
.length
- 1]++;
90 //--------------------------------------------------------------------------
92 //--------------------------------------------------------------------------
98 // The initial complexity is 1, representing one execution path in the CodePath
102 // Each branching in the code adds 1 to the complexity
103 CatchClause
: increaseComplexity
,
104 ConditionalExpression
: increaseComplexity
,
105 LogicalExpression
: increaseComplexity
,
106 ForStatement
: increaseComplexity
,
107 ForInStatement
: increaseComplexity
,
108 ForOfStatement
: increaseComplexity
,
109 IfStatement
: increaseComplexity
,
110 WhileStatement
: increaseComplexity
,
111 DoWhileStatement
: increaseComplexity
,
114 "SwitchCase[test]": increaseComplexity
,
116 // Logical assignment operators have short-circuiting behavior
117 AssignmentExpression(node
) {
118 if (astUtils
.isLogicalAssignmentOperator(node
.operator
)) {
119 increaseComplexity();
123 onCodePathEnd(codePath
, node
) {
124 const complexity
= complexities
.pop();
127 * This rule only evaluates complexity of functions, so "program" is excluded.
128 * Class field initializers and class static blocks are implicit functions. Therefore,
129 * they shouldn't contribute to the enclosing function's complexity, but their
130 * own complexity should be evaluated.
133 codePath
.origin
!== "function" &&
134 codePath
.origin
!== "class-field-initializer" &&
135 codePath
.origin
!== "class-static-block"
140 if (complexity
> THRESHOLD
) {
143 if (codePath
.origin
=== "class-field-initializer") {
144 name
= "class field initializer";
145 } else if (codePath
.origin
=== "class-static-block") {
146 name
= "class static block";
148 name
= astUtils
.getFunctionNameWithKind(node
);
153 messageId
: "complex",
155 name
: upperCaseFirst(name
),