.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noTautologyExpressionRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2019 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 tsutils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var TAUTOLOGY_DISCOVERED_ERROR_STRING = "Both sides of this equality comparison are the same, so the expression is either a tautology or a contradiction.";
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         return this.applyWithFunction(sourceFile, walk);
31     };
32     Rule.metadata = {
33         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n        Enforces that relational/equality binary operators does not take two equal variables/literals as operands.\n        Expression like 3 === 3, someVar === someVar, \"1\" > \"1\" are either a tautology or contradiction, and will produce an error.\n        "], ["\n        Enforces that relational/equality binary operators does not take two equal variables/literals as operands.\n        Expression like 3 === 3, someVar === someVar, \"1\" > \"1\" are either a tautology or contradiction, and will produce an error.\n        "]))),
34         optionExamples: [true],
35         options: null,
36         optionsDescription: "Not configurable.",
37         rationale: "Clean redundant code and unnecessary comparison of objects and literals.",
38         ruleName: "no-tautology-expression",
39         type: "functionality",
40         typescriptOnly: false,
41     };
42     return Rule;
43 }(Lint.Rules.AbstractRule));
44 exports.Rule = Rule;
45 function walk(context) {
46     var cb = function (node) {
47         if (tsutils.isBinaryExpression(node) && isRelationalOrLogicalOperator(node.operatorToken)) {
48             if ((tsutils.isStringLiteral(node.left) && tsutils.isStringLiteral(node.right)) ||
49                 (tsutils.isNumericLiteral(node.left) && tsutils.isNumericLiteral(node.right))) {
50                 if (node.left.text === node.right.text) {
51                     context.addFailureAtNode(node, TAUTOLOGY_DISCOVERED_ERROR_STRING);
52                 }
53             }
54             else if (tsutils.isIdentifier(node.left) && tsutils.isIdentifier(node.right)) {
55                 if (node.left.text === node.right.text) {
56                     context.addFailureAtNode(node, TAUTOLOGY_DISCOVERED_ERROR_STRING);
57                 }
58             }
59             else if (tsutils.isPropertyAccessExpression(node.left) &&
60                 tsutils.isPropertyAccessExpression(node.right)) {
61                 if (node.left.expression.getText() === node.right.expression.getText()) {
62                     if (node.left.name.text === node.right.name.text) {
63                         context.addFailureAtNode(node, TAUTOLOGY_DISCOVERED_ERROR_STRING);
64                     }
65                 }
66             }
67             else if ((isBooleanLiteral(node.left) && isBooleanLiteral(node.right)) ||
68                 (isNullLiteral(node.left) && isNullLiteral(node.right))) {
69                 context.addFailureAtNode(node, TAUTOLOGY_DISCOVERED_ERROR_STRING);
70             }
71         }
72         return ts.forEachChild(node, cb);
73     };
74     return ts.forEachChild(context.sourceFile, cb);
75 }
76 function isNullLiteral(node) {
77     return node.kind === ts.SyntaxKind.NullKeyword;
78 }
79 function isBooleanLiteral(node) {
80     return node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword;
81 }
82 function isRelationalOrLogicalOperator(operator) {
83     return new Set([
84         ts.SyntaxKind.LessThanToken,
85         ts.SyntaxKind.GreaterThanToken,
86         ts.SyntaxKind.LessThanEqualsToken,
87         ts.SyntaxKind.GreaterThanEqualsToken,
88         ts.SyntaxKind.EqualsEqualsToken,
89         ts.SyntaxKind.EqualsEqualsEqualsToken,
90         ts.SyntaxKind.ExclamationEqualsToken,
91         ts.SyntaxKind.ExclamationEqualsEqualsToken,
92         ts.SyntaxKind.AmpersandAmpersandToken,
93         ts.SyntaxKind.BarBarToken,
94     ]).has(operator.kind);
95 }
96 var templateObject_1;