noBooleanLiteralCompareRule.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. /* tslint:enable:object-literal-sort-keys */
  29. Rule.FAILURE_STRING = function (negate) {
  30. return "This expression is unnecessarily compared to a boolean. Just " + (negate ? "negate it" : "use it directly") + ".";
  31. };
  32. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  33. return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
  34. };
  35. /* tslint:disable:object-literal-sort-keys */
  36. Rule.metadata = {
  37. ruleName: "no-boolean-literal-compare",
  38. description: "Warns on comparison to a boolean literal, as in `x === true`.",
  39. hasFix: true,
  40. optionsDescription: "Not configurable.",
  41. options: null,
  42. optionExamples: [true],
  43. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Comparing boolean values to boolean literals is unnecessary, as those expressions will result in booleans too.\n Just use the boolean values directly or negate them.\n "], ["\n Comparing boolean values to boolean literals is unnecessary, as those expressions will result in booleans too.\n Just use the boolean values directly or negate them.\n "]))),
  44. type: "style",
  45. typescriptOnly: true,
  46. requiresTypeInfo: true,
  47. };
  48. return Rule;
  49. }(Lint.Rules.TypedRule));
  50. exports.Rule = Rule;
  51. function walk(ctx, checker) {
  52. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  53. if (utils.isBinaryExpression(node)) {
  54. var cmp = getBooleanComparison(node, checker);
  55. if (cmp !== undefined) {
  56. ctx.addFailureAtNode(cmp.expression, Rule.FAILURE_STRING(cmp.negate), fix(node, cmp));
  57. }
  58. }
  59. return ts.forEachChild(node, cb);
  60. });
  61. }
  62. function getBooleanComparison(node, checker) {
  63. var cmp = deconstructComparison(node);
  64. return cmp === undefined || !utils.isTypeFlagSet(checker.getTypeAtLocation(cmp.expression), ts.TypeFlags.Boolean) ? undefined : cmp;
  65. }
  66. function fix(node, _a) {
  67. var negate = _a.negate, expression = _a.expression;
  68. var deleted = node.left === expression
  69. ? Lint.Replacement.deleteFromTo(node.left.end, node.end)
  70. : Lint.Replacement.deleteFromTo(node.getStart(), node.right.getStart());
  71. if (!negate) {
  72. return deleted;
  73. }
  74. else if (needsParenthesesForNegate(expression)) {
  75. return [
  76. deleted,
  77. Lint.Replacement.appendText(node.getStart(), "!("),
  78. Lint.Replacement.appendText(node.getEnd(), ")"),
  79. ];
  80. }
  81. else {
  82. return [
  83. deleted,
  84. Lint.Replacement.appendText(node.getStart(), "!"),
  85. ];
  86. }
  87. }
  88. function needsParenthesesForNegate(node) {
  89. switch (node.kind) {
  90. case ts.SyntaxKind.AsExpression:
  91. case ts.SyntaxKind.BinaryExpression:
  92. return true;
  93. default:
  94. return false;
  95. }
  96. }
  97. function deconstructComparison(node) {
  98. var left = node.left, operatorToken = node.operatorToken, right = node.right;
  99. var eq = Lint.getEqualsKind(operatorToken);
  100. if (eq === undefined) {
  101. return undefined;
  102. }
  103. var leftValue = booleanFromExpression(left);
  104. if (leftValue !== undefined) {
  105. return { negate: leftValue !== eq.isPositive, expression: right };
  106. }
  107. var rightValue = booleanFromExpression(right);
  108. if (rightValue !== undefined) {
  109. return { negate: rightValue !== eq.isPositive, expression: left };
  110. }
  111. return undefined;
  112. }
  113. function booleanFromExpression(node) {
  114. switch (node.kind) {
  115. case ts.SyntaxKind.TrueKeyword:
  116. return true;
  117. case ts.SyntaxKind.FalseKeyword:
  118. return false;
  119. default:
  120. return undefined;
  121. }
  122. }
  123. var templateObject_1;