adjacentOverloadSignaturesRule.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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 utils = 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. /* tslint:enable:object-literal-sort-keys */
  29. Rule.FAILURE_STRING = function (name) {
  30. return "All '" + name + "' signatures should be adjacent";
  31. };
  32. Rule.prototype.apply = function (sourceFile) {
  33. return this.applyWithFunction(sourceFile, walk);
  34. };
  35. /* tslint:disable:object-literal-sort-keys */
  36. Rule.metadata = {
  37. ruleName: "adjacent-overload-signatures",
  38. description: "Enforces function overloads to be consecutive.",
  39. optionsDescription: "Not configurable.",
  40. options: null,
  41. optionExamples: [true],
  42. rationale: "Improves readability and organization by grouping naturally related items together.",
  43. type: "typescript",
  44. typescriptOnly: true,
  45. };
  46. return Rule;
  47. }(Lint.Rules.AbstractRule));
  48. exports.Rule = Rule;
  49. function walk(ctx) {
  50. var sourceFile = ctx.sourceFile;
  51. visitStatements(sourceFile.statements);
  52. return ts.forEachChild(sourceFile, function cb(node) {
  53. switch (node.kind) {
  54. case ts.SyntaxKind.ModuleBlock:
  55. visitStatements(node.statements);
  56. break;
  57. case ts.SyntaxKind.InterfaceDeclaration:
  58. case ts.SyntaxKind.ClassDeclaration:
  59. case ts.SyntaxKind.TypeLiteral: {
  60. var members = node.members;
  61. addFailures(getMisplacedOverloads(members, function (member) {
  62. return utils.isSignatureDeclaration(member) ? getOverloadKey(member) : undefined;
  63. }));
  64. }
  65. }
  66. return ts.forEachChild(node, cb);
  67. });
  68. function visitStatements(statements) {
  69. addFailures(getMisplacedOverloads(statements, function (statement) {
  70. return utils.isFunctionDeclaration(statement) && statement.name !== undefined ? statement.name.text : undefined;
  71. }));
  72. }
  73. function addFailures(misplacedOverloads) {
  74. for (var _i = 0, misplacedOverloads_1 = misplacedOverloads; _i < misplacedOverloads_1.length; _i++) {
  75. var node = misplacedOverloads_1[_i];
  76. ctx.addFailureAtNode(node, Rule.FAILURE_STRING(printOverload(node)));
  77. }
  78. }
  79. }
  80. /** 'getOverloadName' may return undefined for nodes that cannot be overloads, e.g. a `const` declaration. */
  81. function getMisplacedOverloads(overloads, getKey) {
  82. var result = [];
  83. var lastKey;
  84. var seen = new Set();
  85. for (var _i = 0, overloads_1 = overloads; _i < overloads_1.length; _i++) {
  86. var node = overloads_1[_i];
  87. if (node.kind === ts.SyntaxKind.SemicolonClassElement) {
  88. continue;
  89. }
  90. var key = getKey(node);
  91. if (key !== undefined) {
  92. if (seen.has(key) && lastKey !== key) {
  93. result.push(node);
  94. }
  95. seen.add(key);
  96. lastKey = key;
  97. }
  98. else {
  99. lastKey = undefined;
  100. }
  101. }
  102. return result;
  103. }
  104. function printOverload(node) {
  105. var info = getOverloadInfo(node);
  106. return typeof info === "string" ? info : info === undefined ? "<unknown>" : info.name;
  107. }
  108. function getOverloadKey(node) {
  109. var info = getOverloadInfo(node);
  110. if (info === undefined) {
  111. return undefined;
  112. }
  113. var _a = typeof info === "string" ? [false, info] : [info.computed, info.name], computed = _a[0], name = _a[1];
  114. var isStatic = utils.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword);
  115. return (computed ? "0" : "1") + (isStatic ? "0" : "1") + name;
  116. }
  117. exports.getOverloadKey = getOverloadKey;
  118. function getOverloadInfo(node) {
  119. switch (node.kind) {
  120. case ts.SyntaxKind.ConstructSignature:
  121. case ts.SyntaxKind.Constructor:
  122. return "constructor";
  123. case ts.SyntaxKind.CallSignature:
  124. return "()";
  125. default: {
  126. var name = node.name;
  127. if (name === undefined) {
  128. return undefined;
  129. }
  130. switch (name.kind) {
  131. case ts.SyntaxKind.Identifier:
  132. return name.text;
  133. case ts.SyntaxKind.ComputedPropertyName:
  134. var expression = name.expression;
  135. return utils.isLiteralExpression(expression) ? expression.text : { name: expression.getText(), computed: true };
  136. default:
  137. return utils.isLiteralExpression(name) ? name.text : undefined;
  138. }
  139. }
  140. }
  141. }