binaryExpressionOperandOrderRule.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. type: "style",
  40. typescriptOnly: false,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.FAILURE_STRING = "Literal expression should be on the right-hand side of a binary expression.";
  44. return Rule;
  45. }(Lint.Rules.AbstractRule));
  46. exports.Rule = Rule;
  47. function walk(ctx) {
  48. ts.forEachChild(ctx.sourceFile, function cb(node) {
  49. if (tsutils_1.isBinaryExpression(node) && isLiteral(node.left) && !isLiteral(node.right) && !isAllowedOrderedOperator(node)) {
  50. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  51. }
  52. ts.forEachChild(node, cb);
  53. });
  54. }
  55. /** Allows certain inherently ordered operators that can't easily be written with the literal on the right. */
  56. function isAllowedOrderedOperator(node) {
  57. switch (node.operatorToken.kind) {
  58. case ts.SyntaxKind.PlusToken:
  59. // Allow `"foo" + x` but not `1 + x`.
  60. return node.left.kind === ts.SyntaxKind.StringLiteral;
  61. case ts.SyntaxKind.MinusToken:
  62. case ts.SyntaxKind.SlashToken:
  63. case ts.SyntaxKind.PercentToken:
  64. case ts.SyntaxKind.LessThanLessThanToken:
  65. case ts.SyntaxKind.GreaterThanGreaterThanToken:
  66. case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
  67. case ts.SyntaxKind.AsteriskAsteriskToken:
  68. case ts.SyntaxKind.InKeyword:
  69. case ts.SyntaxKind.CommaToken:
  70. return true;
  71. default:
  72. return false;
  73. }
  74. }
  75. function isLiteral(node) {
  76. switch (node.kind) {
  77. case ts.SyntaxKind.StringLiteral:
  78. case ts.SyntaxKind.NumericLiteral:
  79. case ts.SyntaxKind.TrueKeyword:
  80. case ts.SyntaxKind.FalseKeyword:
  81. case ts.SyntaxKind.NullKeyword:
  82. return true;
  83. case ts.SyntaxKind.Identifier:
  84. return node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword;
  85. case ts.SyntaxKind.PrefixUnaryExpression:
  86. return utils_1.isNegativeNumberLiteral(node);
  87. case ts.SyntaxKind.ParenthesizedExpression:
  88. return isLiteral(node.expression);
  89. default:
  90. return false;
  91. }
  92. }
  93. var templateObject_1;