4 * Copyright 2017 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");
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);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
32 if (this.ruleArguments.length !== 0) {
33 var obj = this.ruleArguments[0];
34 minCases = obj[OPTION_MIN_CASES];
36 return this.applyWithFunction(sourceFile, walk, minCases);
38 /* tslint:disable:object-literal-sort-keys */
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),
46 _a[OPTION_MIN_CASES] = { type: "number" },
49 optionExamples: [true, [true, (_b = {}, _b[OPTION_MIN_CASES] = 2, _b)]],
51 typescriptOnly: false,
53 /* tslint:enable:object-literal-sort-keys */
54 Rule.FAILURE_STRING = "Use a switch statement instead of using multiple '===' checks.";
56 }(Lint.Rules.AbstractRule));
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);
72 return ts.forEachChild(node, cb);
76 function check(node, sourceFile, minCases) {
79 var couldBeSwitch = everyCase(node, function (expr) {
81 if (switchVariable !== undefined) {
82 return nodeEquals(expr, switchVariable, sourceFile);
85 switchVariable = expr;
89 return couldBeSwitch && casesSeen >= minCases;
91 function everyCase(_a, test) {
92 var expression = _a.expression, elseStatement = _a.elseStatement;
93 if (!everyCondition(expression, test)) {
96 return (elseStatement === undefined ||
97 !utils.isIfStatement(elseStatement) ||
98 everyCase(elseStatement, test));
100 function everyCondition(node, test) {
101 if (!utils.isBinaryExpression(node)) {
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);
114 function nodeEquals(a, b, sourceFile) {
115 return a.getText(sourceFile) === b.getText(sourceFile);
117 function isSimple(node) {
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:
127 return isSimple(node.operand);
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:
142 var templateObject_1;