noBooleanLiteralCompareRule.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. type: "style",
  44. typescriptOnly: true,
  45. requiresTypeInfo: true,
  46. };
  47. return Rule;
  48. }(Lint.Rules.TypedRule));
  49. exports.Rule = Rule;
  50. function walk(ctx, checker) {
  51. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  52. if (utils.isBinaryExpression(node)) {
  53. var cmp = getBooleanComparison(node, checker);
  54. if (cmp !== undefined) {
  55. ctx.addFailureAtNode(cmp.expression, Rule.FAILURE_STRING(cmp.negate), fix(node, cmp));
  56. }
  57. }
  58. return ts.forEachChild(node, cb);
  59. });
  60. }
  61. function getBooleanComparison(node, checker) {
  62. var cmp = deconstructComparison(node);
  63. return cmp === undefined || !utils.isTypeFlagSet(checker.getTypeAtLocation(cmp.expression), ts.TypeFlags.Boolean) ? undefined : cmp;
  64. }
  65. function fix(node, _a) {
  66. var negate = _a.negate, expression = _a.expression;
  67. var deleted = node.left === expression
  68. ? Lint.Replacement.deleteFromTo(node.left.end, node.end)
  69. : Lint.Replacement.deleteFromTo(node.getStart(), node.right.getStart());
  70. if (!negate) {
  71. return deleted;
  72. }
  73. else if (needsParenthesesForNegate(expression)) {
  74. return [
  75. deleted,
  76. Lint.Replacement.appendText(node.getStart(), "!("),
  77. Lint.Replacement.appendText(node.getEnd(), ")"),
  78. ];
  79. }
  80. else {
  81. return [
  82. deleted,
  83. Lint.Replacement.appendText(node.getStart(), "!"),
  84. ];
  85. }
  86. }
  87. function needsParenthesesForNegate(node) {
  88. switch (node.kind) {
  89. case ts.SyntaxKind.AsExpression:
  90. case ts.SyntaxKind.BinaryExpression:
  91. return true;
  92. default:
  93. return false;
  94. }
  95. }
  96. function deconstructComparison(node) {
  97. var left = node.left, operatorToken = node.operatorToken, right = node.right;
  98. var eq = Lint.getEqualsKind(operatorToken);
  99. if (eq === undefined) {
  100. return undefined;
  101. }
  102. var leftValue = booleanFromExpression(left);
  103. if (leftValue !== undefined) {
  104. return { negate: leftValue !== eq.isPositive, expression: right };
  105. }
  106. var rightValue = booleanFromExpression(right);
  107. if (rightValue !== undefined) {
  108. return { negate: rightValue !== eq.isPositive, expression: left };
  109. }
  110. return undefined;
  111. }
  112. function booleanFromExpression(node) {
  113. switch (node.kind) {
  114. case ts.SyntaxKind.TrueKeyword:
  115. return true;
  116. case ts.SyntaxKind.FalseKeyword:
  117. return false;
  118. default:
  119. return undefined;
  120. }
  121. }