preferSwitchRule.js 5.5KB

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