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 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 20 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 21 | module.exports = { |
| 22 | meta: { |
| 23 | type: "suggestion", |
| 24 | |
| 25 | docs: { |
| 26 | description: "enforce a maximum cyclomatic complexity allowed in a program", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 27 | recommended: false, |
| 28 | url: "https://eslint.org/docs/rules/complexity" |
| 29 | }, |
| 30 | |
| 31 | schema: [ |
| 32 | { |
| 33 | oneOf: [ |
| 34 | { |
| 35 | type: "integer", |
| 36 | minimum: 0 |
| 37 | }, |
| 38 | { |
| 39 | type: "object", |
| 40 | properties: { |
| 41 | maximum: { |
| 42 | type: "integer", |
| 43 | minimum: 0 |
| 44 | }, |
| 45 | max: { |
| 46 | type: "integer", |
| 47 | minimum: 0 |
| 48 | } |
| 49 | }, |
| 50 | additionalProperties: false |
| 51 | } |
| 52 | ] |
| 53 | } |
| 54 | ], |
| 55 | |
| 56 | messages: { |
| 57 | complex: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}." |
| 58 | } |
| 59 | }, |
| 60 | |
| 61 | create(context) { |
| 62 | const option = context.options[0]; |
| 63 | let THRESHOLD = 20; |
| 64 | |
| 65 | if ( |
| 66 | typeof option === "object" && |
| 67 | (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) |
| 68 | ) { |
| 69 | THRESHOLD = option.maximum || option.max; |
| 70 | } else if (typeof option === "number") { |
| 71 | THRESHOLD = option; |
| 72 | } |
| 73 | |
| 74 | //-------------------------------------------------------------------------- |
| 75 | // Helpers |
| 76 | //-------------------------------------------------------------------------- |
| 77 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 78 | // Using a stack to store complexity per code path |
| 79 | const complexities = []; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 82 | * Increase the complexity of the code path in context |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 83 | * @returns {void} |
| 84 | * @private |
| 85 | */ |
| 86 | function increaseComplexity() { |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 87 | complexities[complexities.length - 1]++; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | //-------------------------------------------------------------------------- |
| 91 | // Public API |
| 92 | //-------------------------------------------------------------------------- |
| 93 | |
| 94 | return { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 95 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 96 | onCodePathStart() { |
| 97 | |
| 98 | // The initial complexity is 1, representing one execution path in the CodePath |
| 99 | complexities.push(1); |
| 100 | }, |
| 101 | |
| 102 | // Each branching in the code adds 1 to the complexity |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 103 | CatchClause: increaseComplexity, |
| 104 | ConditionalExpression: increaseComplexity, |
| 105 | LogicalExpression: increaseComplexity, |
| 106 | ForStatement: increaseComplexity, |
| 107 | ForInStatement: increaseComplexity, |
| 108 | ForOfStatement: increaseComplexity, |
| 109 | IfStatement: increaseComplexity, |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 110 | WhileStatement: increaseComplexity, |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 111 | DoWhileStatement: increaseComplexity, |
| 112 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 113 | // Avoid `default` |
| 114 | "SwitchCase[test]": increaseComplexity, |
| 115 | |
| 116 | // Logical assignment operators have short-circuiting behavior |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 117 | AssignmentExpression(node) { |
| 118 | if (astUtils.isLogicalAssignmentOperator(node.operator)) { |
| 119 | increaseComplexity(); |
| 120 | } |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 121 | }, |
| 122 | |
| 123 | onCodePathEnd(codePath, node) { |
| 124 | const complexity = complexities.pop(); |
| 125 | |
| 126 | /* |
| 127 | * 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] | 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. |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 131 | */ |
| 132 | if ( |
| 133 | codePath.origin !== "function" && |
Tim van der Lippe | 0124c68 | 2021-11-23 15:12:10 +0000 | [diff] [blame] | 134 | codePath.origin !== "class-field-initializer" && |
| 135 | codePath.origin !== "class-static-block" |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 136 | ) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | if (complexity > THRESHOLD) { |
Tim van der Lippe | 0124c68 | 2021-11-23 15:12:10 +0000 | [diff] [blame] | 141 | let name; |
| 142 | |
| 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"; |
| 147 | } else { |
| 148 | name = astUtils.getFunctionNameWithKind(node); |
| 149 | } |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 150 | |
| 151 | context.report({ |
| 152 | node, |
| 153 | messageId: "complex", |
| 154 | data: { |
| 155 | name: upperCaseFirst(name), |
| 156 | complexity, |
| 157 | max: THRESHOLD |
| 158 | } |
| 159 | }); |
| 160 | } |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 161 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | } |
| 165 | }; |