.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noBooleanLiteralCompareRule.js
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 ||
65         !utils.isTypeFlagSet(checker.getTypeAtLocation(cmp.expression), ts.TypeFlags.Boolean)
66         ? undefined
67         : cmp;
68 }
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());
74     if (!negate) {
75         return deleted;
76     }
77     else if (needsParenthesesForNegate(expression)) {
78         return [
79             deleted,
80             Lint.Replacement.appendText(node.getStart(), "!("),
81             Lint.Replacement.appendText(node.getEnd(), ")"),
82         ];
83     }
84     else {
85         return [deleted, Lint.Replacement.appendText(node.getStart(), "!")];
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;