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");
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);
26 return _super !== null && _super.apply(this, arguments) || this;
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") + ".";
32 Rule.prototype.applyWithProgram = function (sourceFile, program) {
33 return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
35 /* tslint:disable:object-literal-sort-keys */
37 ruleName: "no-boolean-literal-compare",
38 description: "Warns on comparison to a boolean literal, as in `x === true`.",
40 optionsDescription: "Not configurable.",
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 "]))),
46 requiresTypeInfo: true,
49 }(Lint.Rules.TypedRule));
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));
59 return ts.forEachChild(node, cb);
62 function getBooleanComparison(node, checker) {
63 var cmp = deconstructComparison(node);
64 return cmp === undefined ||
65 !utils.isTypeFlagSet(checker.getTypeAtLocation(cmp.expression), ts.TypeFlags.Boolean)
69 function fix(node, _a) {
70 var negate = _a.negate, expression = _a.expression;
71 var deleted = node.left === expression
72 ? Lint.Replacement.deleteFromTo(node.left.end, node.end)
73 : Lint.Replacement.deleteFromTo(node.getStart(), node.right.getStart());
77 else if (needsParenthesesForNegate(expression)) {
80 Lint.Replacement.appendText(node.getStart(), "!("),
81 Lint.Replacement.appendText(node.getEnd(), ")"),
85 return [deleted, Lint.Replacement.appendText(node.getStart(), "!")];
88 function needsParenthesesForNegate(node) {
90 case ts.SyntaxKind.AsExpression:
91 case ts.SyntaxKind.BinaryExpression:
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) {
103 var leftValue = booleanFromExpression(left);
104 if (leftValue !== undefined) {
105 return { negate: leftValue !== eq.isPositive, expression: right };
107 var rightValue = booleanFromExpression(right);
108 if (rightValue !== undefined) {
109 return { negate: rightValue !== eq.isPositive, expression: left };
113 function booleanFromExpression(node) {
115 case ts.SyntaxKind.TrueKeyword:
117 case ts.SyntaxKind.FalseKeyword:
123 var templateObject_1;