4 * Copyright 2018 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var utils_1 = require("../utils");
24 var Rule = /** @class */ (function (_super) {
25 tslib_1.__extends(Rule, _super);
27 return _super !== null && _super.apply(this, arguments) || this;
29 /* tslint:enable:object-literal-sort-keys */
30 Rule.FAILURE_STRING = function (expected, actual, name) {
31 return ("The function" + (name === undefined ? "" : " " + name) + " has a cyclomatic complexity of " +
32 (actual + " which is higher than the threshold of " + expected));
34 Rule.prototype.apply = function (sourceFile) {
35 return this.applyWithFunction(sourceFile, walk, { threshold: this.threshold });
37 Rule.prototype.isEnabled = function () {
38 // Disable the rule if the option is provided but non-numeric or less than the minimum.
39 var isThresholdValid = typeof this.threshold === "number" && this.threshold >= Rule.MINIMUM_THRESHOLD;
40 return _super.prototype.isEnabled.call(this) && isThresholdValid;
42 Object.defineProperty(Rule.prototype, "threshold", {
44 if (this.ruleArguments[0] !== undefined) {
45 return this.ruleArguments[0];
47 return Rule.DEFAULT_THRESHOLD;
52 Rule.DEFAULT_THRESHOLD = 20;
53 Rule.MINIMUM_THRESHOLD = 2;
54 /* tslint:disable:object-literal-sort-keys */
56 ruleName: "cyclomatic-complexity",
57 description: "Enforces a threshold of cyclomatic complexity.",
58 descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Cyclomatic complexity is assessed for each function of any type. A starting value of 0\n is assigned and this value is then incremented for every statement which can branch the\n control flow within the function. The following statements and expressions contribute\n to cyclomatic complexity:\n * `catch`\n * `if` and `? :`\n * `||` and `&&` due to short-circuit evaluation\n * `for`, `for in` and `for of` loops\n * `while` and `do while` loops\n * `case` clauses that contain statements"], ["\n Cyclomatic complexity is assessed for each function of any type. A starting value of 0\n is assigned and this value is then incremented for every statement which can branch the\n control flow within the function. The following statements and expressions contribute\n to cyclomatic complexity:\n * \\`catch\\`\n * \\`if\\` and \\`? :\\`\n * \\`||\\` and \\`&&\\` due to short-circuit evaluation\n * \\`for\\`, \\`for in\\` and \\`for of\\` loops\n * \\`while\\` and \\`do while\\` loops\n * \\`case\\` clauses that contain statements"]))),
59 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Cyclomatic complexity is a code metric which indicates the level of complexity in a\n function. High cyclomatic complexity indicates confusing code which may be prone to\n errors or difficult to modify.\n\n It's better to have smaller, single-purpose functions with self-documenting names."], ["\n Cyclomatic complexity is a code metric which indicates the level of complexity in a\n function. High cyclomatic complexity indicates confusing code which may be prone to\n errors or difficult to modify.\n\n It's better to have smaller, single-purpose functions with self-documenting names."]))),
60 optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n An optional upper limit for cyclomatic complexity can be specified. If no limit option\n is provided a default value of ", " will be used."], ["\n An optional upper limit for cyclomatic complexity can be specified. If no limit option\n is provided a default value of ", " will be used."])), Rule.DEFAULT_THRESHOLD),
63 minimum: Rule.MINIMUM_THRESHOLD,
65 optionExamples: [true, [true, 20]],
66 type: "maintainability",
67 typescriptOnly: false,
70 }(Lint.Rules.AbstractRule));
73 var threshold = ctx.options.threshold;
75 return ts.forEachChild(ctx.sourceFile, function cb(node) {
76 // tslint:disable:deprecation This is needed for https://github.com/palantir/tslint/pull/4274 and will be fixed once TSLint
77 // requires tsutils > 3.0.
78 if (utils_1.isFunctionScopeBoundary(node)) {
79 // tslint:enable:deprecation
82 ts.forEachChild(node, cb);
83 if (complexity > threshold) {
85 var nameStr = name !== undefined && tsutils_1.isIdentifier(name) ? name.text : undefined;
86 ctx.addFailureAtNode(node, Rule.FAILURE_STRING(threshold, complexity, nameStr));
91 if (increasesComplexity(node)) {
94 return ts.forEachChild(node, cb);
98 function increasesComplexity(node) {
100 case ts.SyntaxKind.CaseClause:
101 return node.statements.length > 0;
102 case ts.SyntaxKind.CatchClause:
103 case ts.SyntaxKind.ConditionalExpression:
104 case ts.SyntaxKind.DoStatement:
105 case ts.SyntaxKind.ForStatement:
106 case ts.SyntaxKind.ForInStatement:
107 case ts.SyntaxKind.ForOfStatement:
108 case ts.SyntaxKind.IfStatement:
109 case ts.SyntaxKind.WhileStatement:
111 case ts.SyntaxKind.BinaryExpression:
112 switch (node.operatorToken.kind) {
113 case ts.SyntaxKind.BarBarToken:
114 case ts.SyntaxKind.AmpersandAmpersandToken:
123 var templateObject_1, templateObject_2, templateObject_3;