restrictPlusOperandsRule.js 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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 Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  29. return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "restrict-plus-operands",
  34. description: "When adding two variables, operands must both be of type number or of type string.",
  35. optionsDescription: "Not configurable.",
  36. options: null,
  37. optionExamples: [true],
  38. type: "functionality",
  39. typescriptOnly: false,
  40. requiresTypeInfo: true,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.INVALID_TYPES_ERROR = "Operands of '+' operation must either be both strings or both numbers";
  44. Rule.SUGGEST_TEMPLATE_LITERALS = ", consider using template literals";
  45. return Rule;
  46. }(Lint.Rules.TypedRule));
  47. exports.Rule = Rule;
  48. function walk(ctx, tc) {
  49. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  50. if (tsutils_1.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken) {
  51. var leftType = getBaseTypeOfLiteralType(tc.getTypeAtLocation(node.left));
  52. var rightType = getBaseTypeOfLiteralType(tc.getTypeAtLocation(node.right));
  53. if (leftType === "invalid" || rightType === "invalid" || leftType !== rightType) {
  54. if (leftType === "string" || rightType === "string") {
  55. return ctx.addFailureAtNode(node, Rule.INVALID_TYPES_ERROR + Rule.SUGGEST_TEMPLATE_LITERALS);
  56. }
  57. else {
  58. return ctx.addFailureAtNode(node, Rule.INVALID_TYPES_ERROR);
  59. }
  60. }
  61. }
  62. return ts.forEachChild(node, cb);
  63. });
  64. }
  65. function getBaseTypeOfLiteralType(type) {
  66. if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.StringLiteral) || tsutils_1.isTypeFlagSet(type, ts.TypeFlags.String)) {
  67. return "string";
  68. }
  69. else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.NumberLiteral) || tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Number)) {
  70. return "number";
  71. }
  72. else if (tsutils_1.isUnionType(type) && !tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Enum)) {
  73. var types = type.types.map(getBaseTypeOfLiteralType);
  74. return allSame(types) ? types[0] : "invalid";
  75. }
  76. else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.EnumLiteral)) {
  77. // Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
  78. getBaseTypeOfLiteralType(type.baseType);
  79. }
  80. return "invalid";
  81. }
  82. function allSame(array) {
  83. return array.every(function (value) { return value === array[0]; });
  84. }