.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / preferSwitchRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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 _a, _b;
21 var utils = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var OPTION_MIN_CASES = "min-cases";
25 var Rule = /** @class */ (function (_super) {
26     tslib_1.__extends(Rule, _super);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     Rule.prototype.apply = function (sourceFile) {
31         var minCases = 3;
32         if (this.ruleArguments.length !== 0) {
33             var obj = this.ruleArguments[0];
34             minCases = obj[OPTION_MIN_CASES];
35         }
36         return this.applyWithFunction(sourceFile, walk, minCases);
37     };
38     /* tslint:disable:object-literal-sort-keys */
39     Rule.metadata = {
40         ruleName: "prefer-switch",
41         description: "Prefer a `switch` statement to an `if` statement with simple `===` comparisons.",
42         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            An optional object with the property '", "'.\n            This is the number cases needed before a switch statement is recommended.\n            Defaults to 3."], ["\n            An optional object with the property '", "'.\n            This is the number cases needed before a switch statement is recommended.\n            Defaults to 3."])), OPTION_MIN_CASES),
43         options: {
44             type: "object",
45             properties: (_a = {},
46                 _a[OPTION_MIN_CASES] = { type: "number" },
47                 _a),
48         },
49         optionExamples: [true, [true, (_b = {}, _b[OPTION_MIN_CASES] = 2, _b)]],
50         type: "style",
51         typescriptOnly: false,
52     };
53     /* tslint:enable:object-literal-sort-keys */
54     Rule.FAILURE_STRING = "Use a switch statement instead of using multiple '===' checks.";
55     return Rule;
56 }(Lint.Rules.AbstractRule));
57 exports.Rule = Rule;
58 function walk(ctx) {
59     var minCases = ctx.options, sourceFile = ctx.sourceFile;
60     return ts.forEachChild(sourceFile, function cb(node) {
61         if (utils.isIfStatement(node) && check(node, sourceFile, minCases)) {
62             var expression = node.expression, thenStatement = node.thenStatement, elseStatement = node.elseStatement;
63             ctx.addFailureAtNode(expression, Rule.FAILURE_STRING);
64             // Be careful not to fail again for the "else if"
65             ts.forEachChild(expression, cb);
66             ts.forEachChild(thenStatement, cb);
67             if (elseStatement !== undefined) {
68                 ts.forEachChild(elseStatement, cb);
69             }
70         }
71         else {
72             return ts.forEachChild(node, cb);
73         }
74     });
75 }
76 function check(node, sourceFile, minCases) {
77     var switchVariable;
78     var casesSeen = 0;
79     var couldBeSwitch = everyCase(node, function (expr) {
80         casesSeen++;
81         if (switchVariable !== undefined) {
82             return nodeEquals(expr, switchVariable, sourceFile);
83         }
84         else {
85             switchVariable = expr;
86             return true;
87         }
88     });
89     return couldBeSwitch && casesSeen >= minCases;
90 }
91 function everyCase(_a, test) {
92     var expression = _a.expression, elseStatement = _a.elseStatement;
93     if (!everyCondition(expression, test)) {
94         return false;
95     }
96     return (elseStatement === undefined ||
97         !utils.isIfStatement(elseStatement) ||
98         everyCase(elseStatement, test));
99 }
100 function everyCondition(node, test) {
101     if (!utils.isBinaryExpression(node)) {
102         return false;
103     }
104     var operatorToken = node.operatorToken, left = node.left, right = node.right;
105     switch (operatorToken.kind) {
106         case ts.SyntaxKind.BarBarToken:
107             return everyCondition(left, test) && everyCondition(right, test);
108         case ts.SyntaxKind.EqualsEqualsEqualsToken:
109             return isSimple(left) && isSimple(right) && test(left);
110         default:
111             return false;
112     }
113 }
114 function nodeEquals(a, b, sourceFile) {
115     return a.getText(sourceFile) === b.getText(sourceFile);
116 }
117 function isSimple(node) {
118     switch (node.kind) {
119         case ts.SyntaxKind.PropertyAccessExpression:
120             return isSimple(node.expression);
121         case ts.SyntaxKind.PrefixUnaryExpression:
122             switch (node.operator) {
123                 case ts.SyntaxKind.PlusPlusToken:
124                 case ts.SyntaxKind.MinusMinusToken:
125                     return false;
126                 default:
127                     return isSimple(node.operand);
128             }
129         case ts.SyntaxKind.Identifier:
130         case ts.SyntaxKind.NumericLiteral:
131         case ts.SyntaxKind.StringLiteral:
132         case ts.SyntaxKind.ThisKeyword:
133         case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
134         case ts.SyntaxKind.TrueKeyword:
135         case ts.SyntaxKind.FalseKeyword:
136         case ts.SyntaxKind.NullKeyword:
137             return true;
138         default:
139             return false;
140     }
141 }
142 var templateObject_1;