.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / binaryExpressionOperandOrderRule.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 tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var utils_1 = require("../language/utils");
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     /* tslint:disable:object-literal-sort-keys */
33     Rule.metadata = {
34         ruleName: "binary-expression-operand-order",
35         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            In a binary expression, a literal should always be on the right-hand side if possible.\n            For example, prefer 'x + 1' over '1 + x'."], ["\n            In a binary expression, a literal should always be on the right-hand side if possible.\n            For example, prefer 'x + 1' over '1 + x'."]))),
36         optionsDescription: "Not configurable.",
37         options: null,
38         optionExamples: [true],
39         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            Expressions like `1 + x` are sometimes referred to as \"Yoda\" expressions because they read\n            opposite to how we would normally speak the expression.\n\n            Sticking to a consistent grammar for conditions helps keep code readable and understandable.\n        "], ["\n            Expressions like \\`1 + x\\` are sometimes referred to as \"Yoda\" expressions because they read\n            opposite to how we would normally speak the expression.\n\n            Sticking to a consistent grammar for conditions helps keep code readable and understandable.\n        "]))),
40         type: "style",
41         typescriptOnly: false,
42     };
43     /* tslint:enable:object-literal-sort-keys */
44     Rule.FAILURE_STRING = "Literal expression should be on the right-hand side of a binary expression.";
45     return Rule;
46 }(Lint.Rules.AbstractRule));
47 exports.Rule = Rule;
48 function walk(ctx) {
49     ts.forEachChild(ctx.sourceFile, function cb(node) {
50         if (tsutils_1.isBinaryExpression(node) &&
51             isLiteral(node.left) &&
52             !isLiteral(node.right) &&
53             !isAllowedOrderedOperator(node)) {
54             ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
55         }
56         ts.forEachChild(node, cb);
57     });
58 }
59 /** Allows certain inherently ordered operators that can't easily be written with the literal on the right. */
60 function isAllowedOrderedOperator(node) {
61     switch (node.operatorToken.kind) {
62         case ts.SyntaxKind.PlusToken:
63             // Allow `"foo" + x` but not `1 + x`.
64             return node.left.kind === ts.SyntaxKind.StringLiteral;
65         case ts.SyntaxKind.MinusToken:
66         case ts.SyntaxKind.SlashToken:
67         case ts.SyntaxKind.PercentToken:
68         case ts.SyntaxKind.LessThanLessThanToken:
69         case ts.SyntaxKind.GreaterThanGreaterThanToken:
70         case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
71         case ts.SyntaxKind.AsteriskAsteriskToken:
72         case ts.SyntaxKind.InKeyword:
73         case ts.SyntaxKind.CommaToken:
74             return true;
75         default:
76             return false;
77     }
78 }
79 function isLiteral(node) {
80     switch (node.kind) {
81         case ts.SyntaxKind.StringLiteral:
82         case ts.SyntaxKind.NumericLiteral:
83         case ts.SyntaxKind.TrueKeyword:
84         case ts.SyntaxKind.FalseKeyword:
85         case ts.SyntaxKind.NullKeyword:
86             return true;
87         case ts.SyntaxKind.Identifier:
88             return node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword;
89         case ts.SyntaxKind.PrefixUnaryExpression:
90             return utils_1.isNegativeNumberLiteral(node);
91         case ts.SyntaxKind.ParenthesizedExpression:
92             return isLiteral(node.expression);
93         default:
94             return false;
95     }
96 }
97 var templateObject_1, templateObject_2;