| 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, switch/case,
|
|---|
| 4 | * @author Patrick Brosset
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | "use strict";
|
|---|
| 8 |
|
|---|
| 9 | //------------------------------------------------------------------------------
|
|---|
| 10 | // Requirements
|
|---|
| 11 | //------------------------------------------------------------------------------
|
|---|
| 12 |
|
|---|
| 13 | const astUtils = require("./utils/ast-utils");
|
|---|
| 14 | const { upperCaseFirst } = require("../shared/string-utils");
|
|---|
| 15 |
|
|---|
| 16 | //------------------------------------------------------------------------------
|
|---|
| 17 | // Rule Definition
|
|---|
| 18 | //------------------------------------------------------------------------------
|
|---|
| 19 |
|
|---|
| 20 | /** @type {import('../shared/types').Rule} */
|
|---|
| 21 | module.exports = {
|
|---|
| 22 | meta: {
|
|---|
| 23 | type: "suggestion",
|
|---|
| 24 |
|
|---|
| 25 | docs: {
|
|---|
| 26 | description: "Enforce a maximum cyclomatic complexity allowed in a program",
|
|---|
| 27 | recommended: false,
|
|---|
| 28 | url: "https://eslint.org/docs/latest/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 |
|
|---|
| 78 | // Using a stack to store complexity per code path
|
|---|
| 79 | const complexities = [];
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Increase the complexity of the code path in context
|
|---|
| 83 | * @returns {void}
|
|---|
| 84 | * @private
|
|---|
| 85 | */
|
|---|
| 86 | function increaseComplexity() {
|
|---|
| 87 | complexities[complexities.length - 1]++;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | //--------------------------------------------------------------------------
|
|---|
| 91 | // Public API
|
|---|
| 92 | //--------------------------------------------------------------------------
|
|---|
| 93 |
|
|---|
| 94 | return {
|
|---|
| 95 |
|
|---|
| 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
|
|---|
| 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,
|
|---|
| 112 |
|
|---|
| 113 | // Avoid `default`
|
|---|
| 114 | "SwitchCase[test]": increaseComplexity,
|
|---|
| 115 |
|
|---|
| 116 | // Logical assignment operators have short-circuiting behavior
|
|---|
| 117 | AssignmentExpression(node) {
|
|---|
| 118 | if (astUtils.isLogicalAssignmentOperator(node.operator)) {
|
|---|
| 119 | increaseComplexity();
|
|---|
| 120 | }
|
|---|
| 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.
|
|---|
| 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.
|
|---|
| 131 | */
|
|---|
| 132 | if (
|
|---|
| 133 | codePath.origin !== "function" &&
|
|---|
| 134 | codePath.origin !== "class-field-initializer" &&
|
|---|
| 135 | codePath.origin !== "class-static-block"
|
|---|
| 136 | ) {
|
|---|
| 137 | return;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | if (complexity > THRESHOLD) {
|
|---|
| 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 | }
|
|---|
| 150 |
|
|---|
| 151 | context.report({
|
|---|
| 152 | node,
|
|---|
| 153 | messageId: "complex",
|
|---|
| 154 | data: {
|
|---|
| 155 | name: upperCaseFirst(name),
|
|---|
| 156 | complexity,
|
|---|
| 157 | max: THRESHOLD
|
|---|
| 158 | }
|
|---|
| 159 | });
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | };
|
|---|
| 163 |
|
|---|
| 164 | }
|
|---|
| 165 | };
|
|---|