binaryExpressionOperandOrderRule.js 4.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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) && isLiteral(node.left) && !isLiteral(node.right) && !isAllowedOrderedOperator(node)) {
  51. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  52. }
  53. ts.forEachChild(node, cb);
  54. });
  55. }
  56. /** Allows certain inherently ordered operators that can't easily be written with the literal on the right. */
  57. function isAllowedOrderedOperator(node) {
  58. switch (node.operatorToken.kind) {
  59. case ts.SyntaxKind.PlusToken:
  60. // Allow `"foo" + x` but not `1 + x`.
  61. return node.left.kind === ts.SyntaxKind.StringLiteral;
  62. case ts.SyntaxKind.MinusToken:
  63. case ts.SyntaxKind.SlashToken:
  64. case ts.SyntaxKind.PercentToken:
  65. case ts.SyntaxKind.LessThanLessThanToken:
  66. case ts.SyntaxKind.GreaterThanGreaterThanToken:
  67. case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
  68. case ts.SyntaxKind.AsteriskAsteriskToken:
  69. case ts.SyntaxKind.InKeyword:
  70. case ts.SyntaxKind.CommaToken:
  71. return true;
  72. default:
  73. return false;
  74. }
  75. }
  76. function isLiteral(node) {
  77. switch (node.kind) {
  78. case ts.SyntaxKind.StringLiteral:
  79. case ts.SyntaxKind.NumericLiteral:
  80. case ts.SyntaxKind.TrueKeyword:
  81. case ts.SyntaxKind.FalseKeyword:
  82. case ts.SyntaxKind.NullKeyword:
  83. return true;
  84. case ts.SyntaxKind.Identifier:
  85. return node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword;
  86. case ts.SyntaxKind.PrefixUnaryExpression:
  87. return utils_1.isNegativeNumberLiteral(node);
  88. case ts.SyntaxKind.ParenthesizedExpression:
  89. return isLiteral(node.expression);
  90. default:
  91. return false;
  92. }
  93. }
  94. var templateObject_1, templateObject_2;