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 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);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk);
32 /* tslint:disable:object-literal-sort-keys */
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.",
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 "]))),
41 typescriptOnly: false,
43 /* tslint:enable:object-literal-sort-keys */
44 Rule.FAILURE_STRING = "Literal expression should be on the right-hand side of a binary expression.";
46 }(Lint.Rules.AbstractRule));
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);
56 ts.forEachChild(node, cb);
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:
79 function isLiteral(node) {
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:
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);
97 var templateObject_1, templateObject_2;