.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / cyclomaticComplexityRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
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));
33     };
34     Rule.prototype.apply = function (sourceFile) {
35         return this.applyWithFunction(sourceFile, walk, { threshold: this.threshold });
36     };
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;
41     };
42     Object.defineProperty(Rule.prototype, "threshold", {
43         get: function () {
44             if (this.ruleArguments[0] !== undefined) {
45                 return this.ruleArguments[0];
46             }
47             return Rule.DEFAULT_THRESHOLD;
48         },
49         enumerable: true,
50         configurable: true
51     });
52     Rule.DEFAULT_THRESHOLD = 20;
53     Rule.MINIMUM_THRESHOLD = 2;
54     /* tslint:disable:object-literal-sort-keys */
55     Rule.metadata = {
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),
61         options: {
62             type: "number",
63             minimum: Rule.MINIMUM_THRESHOLD,
64         },
65         optionExamples: [true, [true, 20]],
66         type: "maintainability",
67         typescriptOnly: false,
68     };
69     return Rule;
70 }(Lint.Rules.AbstractRule));
71 exports.Rule = Rule;
72 function walk(ctx) {
73     var threshold = ctx.options.threshold;
74     var complexity = 0;
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
80             var old = complexity;
81             complexity = 1;
82             ts.forEachChild(node, cb);
83             if (complexity > threshold) {
84                 var name = node.name;
85                 var nameStr = name !== undefined && tsutils_1.isIdentifier(name) ? name.text : undefined;
86                 ctx.addFailureAtNode(node, Rule.FAILURE_STRING(threshold, complexity, nameStr));
87             }
88             complexity = old;
89         }
90         else {
91             if (increasesComplexity(node)) {
92                 complexity++;
93             }
94             return ts.forEachChild(node, cb);
95         }
96     });
97 }
98 function increasesComplexity(node) {
99     switch (node.kind) {
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:
110             return true;
111         case ts.SyntaxKind.BinaryExpression:
112             switch (node.operatorToken.kind) {
113                 case ts.SyntaxKind.BarBarToken:
114                 case ts.SyntaxKind.AmpersandAmpersandToken:
115                     return true;
116                 default:
117                     return false;
118             }
119         default:
120             return false;
121     }
122 }
123 var templateObject_1, templateObject_2, templateObject_3;