4 * Copyright 2018 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
27 return _super !== null && _super.apply(this, arguments) || this;
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.";
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,
42 /* tslint:disable:object-literal-sort-keys */
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."]))),
57 enum: [OPTION_EXCLUDE_CLASS_EXPRESSIONS],
60 additionalItems: false,
64 optionExamples: [[true, 1], [true, 5, OPTION_EXCLUDE_CLASS_EXPRESSIONS]],
65 type: "maintainability",
66 typescriptOnly: false,
69 }(Lint.Rules.AbstractRule));
72 var sourceFile = ctx.sourceFile, _a = ctx.options, maxClasses = _a.maxClasses, excludeClassExpressions = _a.excludeClassExpressions;
74 return ts.forEachChild(sourceFile, function cb(node) {
75 if (tsutils_1.isClassDeclaration(node) || (!excludeClassExpressions && tsutils_1.isClassExpression(node))) {
77 if (classes > maxClasses) {
78 ctx.addFailureAtNode(node, Rule.FAILURE_STRING(maxClasses));
81 return ts.forEachChild(node, cb);
84 var templateObject_1, templateObject_2, templateObject_3;