maxClassesPerFileRule.js 4.3KB

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 OPTION_EXCLUDE_CLASS_EXPRESSIONS = "exclude-class-expressions";
  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. /* tslint:enable:object-literal-sort-keys */
  30. Rule.FAILURE_STRING = function (maxCount) {
  31. var maxClassWord = maxCount === 1 ? "class per file is" : "classes per file are";
  32. return "A maximum of " + maxCount + " " + maxClassWord + " allowed.";
  33. };
  34. Rule.prototype.apply = function (sourceFile) {
  35. var argument = this.ruleArguments[0];
  36. var maxClasses = isNaN(argument) || argument > 0 ? argument : 1;
  37. return this.applyWithFunction(sourceFile, walk, {
  38. excludeClassExpressions: this.ruleArguments.indexOf(OPTION_EXCLUDE_CLASS_EXPRESSIONS) !== -1,
  39. maxClasses: maxClasses,
  40. });
  41. };
  42. /* tslint:disable:object-literal-sort-keys */
  43. Rule.metadata = {
  44. ruleName: "max-classes-per-file",
  45. description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n A file may not contain more than the specified number of classes"], ["\n A file may not contain more than the specified number of classes"]))),
  46. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Ensures that files have a single responsibility so that that classes each exist in their own files"], ["\n Ensures that files have a single responsibility so that that classes each exist in their own files"]))),
  47. optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n The one required argument is an integer indicating the maximum number of classes that can appear in a\n file. An optional argument `\"exclude-class-expressions\"` can be provided to exclude class expressions\n from the overall class count."], ["\n The one required argument is an integer indicating the maximum number of classes that can appear in a\n file. An optional argument \\`\"exclude-class-expressions\"\\` can be provided to exclude class expressions\n from the overall class count."]))),
  48. options: {
  49. type: "array",
  50. items: [
  51. {
  52. type: "number",
  53. minimum: 1,
  54. },
  55. {
  56. type: "string",
  57. enum: [OPTION_EXCLUDE_CLASS_EXPRESSIONS],
  58. },
  59. ],
  60. additionalItems: false,
  61. minLength: 1,
  62. maxLength: 2,
  63. },
  64. optionExamples: [[true, 1], [true, 5, OPTION_EXCLUDE_CLASS_EXPRESSIONS]],
  65. type: "maintainability",
  66. typescriptOnly: false,
  67. };
  68. return Rule;
  69. }(Lint.Rules.AbstractRule));
  70. exports.Rule = Rule;
  71. function walk(ctx) {
  72. var sourceFile = ctx.sourceFile, _a = ctx.options, maxClasses = _a.maxClasses, excludeClassExpressions = _a.excludeClassExpressions;
  73. var classes = 0;
  74. return ts.forEachChild(sourceFile, function cb(node) {
  75. if (tsutils_1.isClassDeclaration(node) || (!excludeClassExpressions && tsutils_1.isClassExpression(node))) {
  76. classes++;
  77. if (classes > maxClasses) {
  78. ctx.addFailureAtNode(node, Rule.FAILURE_STRING(maxClasses));
  79. }
  80. }
  81. return ts.forEachChild(node, cb);
  82. });
  83. }
  84. var templateObject_1, templateObject_2, templateObject_3;