Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Counts the cyclomatic complexity of each function of the script. See http://en.wikipedia.org/wiki/Cyclomatic_complexity. |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 3 | * Counts the number of if, conditional, for, while, try, switch/case, |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 4 | * @author Patrick Brosset |
| 5 | */ |
| 6 | |
| 7 | "use strict"; |
| 8 | |
| 9 | //------------------------------------------------------------------------------ |
| 10 | // Requirements |
| 11 | //------------------------------------------------------------------------------ |
| 12 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 13 | const astUtils = require("./utils/ast-utils"); |
Simon Zünd | 52e2020 | 2021-06-16 08:34:28 +0200 | [diff] [blame] | 14 | const { upperCaseFirst } = require("../shared/string-utils"); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 15 | |
| 16 | //------------------------------------------------------------------------------ |
| 17 | // Rule Definition |
| 18 | //------------------------------------------------------------------------------ |
| 19 | |
| 20 | module.exports = { |
| 21 | meta: { |
| 22 | type: "suggestion", |
| 23 | |
| 24 | docs: { |
| 25 | description: "enforce a maximum cyclomatic complexity allowed in a program", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 26 | recommended: false, |
| 27 | url: "https://eslint.org/docs/rules/complexity" |
| 28 | }, |
| 29 | |
| 30 | schema: [ |
| 31 | { |
| 32 | oneOf: [ |
| 33 | { |
| 34 | type: "integer", |
| 35 | minimum: 0 |
| 36 | }, |
| 37 | { |
| 38 | type: "object", |
| 39 | properties: { |
| 40 | maximum: { |
| 41 | type: "integer", |
| 42 | minimum: 0 |
| 43 | }, |
| 44 | max: { |
| 45 | type: "integer", |
| 46 | minimum: 0 |
| 47 | } |
| 48 | }, |
| 49 | additionalProperties: false |
| 50 | } |
| 51 | ] |
| 52 | } |
| 53 | ], |
| 54 | |
| 55 | messages: { |
| 56 | complex: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}." |
| 57 | } |
| 58 | }, |
| 59 | |
| 60 | create(context) { |
| 61 | const option = context.options[0]; |
| 62 | let THRESHOLD = 20; |
| 63 | |
| 64 | if ( |
| 65 | typeof option === "object" && |
| 66 | (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) |
| 67 | ) { |
| 68 | THRESHOLD = option.maximum || option.max; |
| 69 | } else if (typeof option === "number") { |
| 70 | THRESHOLD = option; |
| 71 | } |
| 72 | |
| 73 | //-------------------------------------------------------------------------- |
| 74 | // Helpers |
| 75 | //-------------------------------------------------------------------------- |
| 76 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 77 | // Using a stack to store complexity per code path |
| 78 | const complexities = []; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 79 | |
| 80 | /** |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 81 | * Increase the complexity of the code path in context |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 82 | * @returns {void} |
| 83 | * @private |
| 84 | */ |
| 85 | function increaseComplexity() { |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 86 | complexities[complexities.length - 1]++; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | //-------------------------------------------------------------------------- |
| 90 | // Public API |
| 91 | //-------------------------------------------------------------------------- |
| 92 | |
| 93 | return { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 94 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 95 | onCodePathStart() { |
| 96 | |
| 97 | // The initial complexity is 1, representing one execution path in the CodePath |
| 98 | complexities.push(1); |
| 99 | }, |
| 100 | |
| 101 | // Each branching in the code adds 1 to the complexity |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 102 | CatchClause: increaseComplexity, |
| 103 | ConditionalExpression: increaseComplexity, |
| 104 | LogicalExpression: increaseComplexity, |
| 105 | ForStatement: increaseComplexity, |
| 106 | ForInStatement: increaseComplexity, |
| 107 | ForOfStatement: increaseComplexity, |
| 108 | IfStatement: increaseComplexity, |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 109 | WhileStatement: increaseComplexity, |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 110 | DoWhileStatement: increaseComplexity, |
| 111 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 112 | // Avoid `default` |
| 113 | "SwitchCase[test]": increaseComplexity, |
| 114 | |
| 115 | // Logical assignment operators have short-circuiting behavior |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 116 | AssignmentExpression(node) { |
| 117 | if (astUtils.isLogicalAssignmentOperator(node.operator)) { |
| 118 | increaseComplexity(); |
| 119 | } |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 120 | }, |
| 121 | |
| 122 | onCodePathEnd(codePath, node) { |
| 123 | const complexity = complexities.pop(); |
| 124 | |
| 125 | /* |
| 126 | * This rule only evaluates complexity of functions, so "program" is excluded. |
Tim van der Lippe | 0124c68 | 2021-11-23 15:12:10 +0000 | [diff] [blame] | 127 | * Class field initializers and class static blocks are implicit functions. Therefore, |
| 128 | * they shouldn't contribute to the enclosing function's complexity, but their |
| 129 | * own complexity should be evaluated. |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 130 | */ |
| 131 | if ( |
| 132 | codePath.origin !== "function" && |
Tim van der Lippe | 0124c68 | 2021-11-23 15:12:10 +0000 | [diff] [blame] | 133 | codePath.origin !== "class-field-initializer" && |
| 134 | codePath.origin !== "class-static-block" |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 135 | ) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (complexity > THRESHOLD) { |
Tim van der Lippe | 0124c68 | 2021-11-23 15:12:10 +0000 | [diff] [blame] | 140 | let name; |
| 141 | |
| 142 | if (codePath.origin === "class-field-initializer") { |
| 143 | name = "class field initializer"; |
| 144 | } else if (codePath.origin === "class-static-block") { |
| 145 | name = "class static block"; |
| 146 | } else { |
| 147 | name = astUtils.getFunctionNameWithKind(node); |
| 148 | } |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 149 | |
| 150 | context.report({ |
| 151 | node, |
| 152 | messageId: "complex", |
| 153 | data: { |
| 154 | name: upperCaseFirst(name), |
| 155 | complexity, |
| 156 | max: THRESHOLD |
| 157 | } |
| 158 | }); |
| 159 | } |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 160 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } |
| 164 | }; |