2017-05-28 00:38:50 +02:00
"use strict" ;
/ * *
* @ license
* Copyright 2016 Palantir Technologies , Inc .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
var tslib _1 = require ( "tslib" ) ;
var tsutils _1 = require ( "tsutils" ) ;
var ts = require ( "typescript" ) ;
var Lint = require ( "../index" ) ;
2017-12-10 21:51:33 +01:00
var OPTION _EXCLUDE _CLASS _EXPRESSIONS = "exclude-class-expressions" ;
2017-10-14 18:40:54 +02:00
var Rule = /** @class */ ( function ( _super ) {
2017-05-28 00:38:50 +02:00
tslib _1 . _ _extends ( Rule , _super ) ;
function Rule ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING = function ( maxCount ) {
var maxClassWord = maxCount === 1 ? "class per file is" : "classes per file are" ;
return "A maximum of " + maxCount + " " + maxClassWord + " allowed." ;
} ;
Rule . prototype . apply = function ( sourceFile ) {
var argument = this . ruleArguments [ 0 ] ;
var maxClasses = isNaN ( argument ) || argument > 0 ? argument : 1 ;
2017-12-10 21:51:33 +01:00
return this . applyWithFunction ( sourceFile , walk , {
excludeClassExpressions : this . ruleArguments . indexOf ( OPTION _EXCLUDE _CLASS _EXPRESSIONS ) !== - 1 ,
maxClasses : maxClasses ,
} ) ;
2017-05-28 00:38:50 +02:00
} ;
2017-08-14 05:01:11 +02:00
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "max-classes-per-file" ,
description : ( _a = [ "\n A file may not contain more than the specified number of classes" ] , _a . raw = [ "\n A file may not contain more than the specified number of classes" ] , Lint . Utils . dedent ( _a ) ) ,
rationale : ( _b = [ "\n Ensures that files have a single responsibility so that that classes each exist in their own files" ] , _b . raw = [ "\n Ensures that files have a single responsibility so that that classes each exist in their own files" ] , Lint . Utils . dedent ( _b ) ) ,
2017-12-10 21:51:33 +01:00
optionsDescription : ( _c = [ "\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." ] , _c . raw = [ "\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." ] , Lint . Utils . dedent ( _c ) ) ,
2017-08-14 05:01:11 +02:00
options : {
type : "array" ,
items : [
{
type : "number" ,
minimum : 1 ,
} ,
2017-12-10 21:51:33 +01:00
{
type : "string" ,
enum : [ OPTION _EXCLUDE _CLASS _EXPRESSIONS ] ,
} ,
2017-08-14 05:01:11 +02:00
] ,
additionalItems : false ,
minLength : 1 ,
maxLength : 2 ,
} ,
2017-12-10 21:51:33 +01:00
optionExamples : [ [ true , 1 ] , [ true , 5 , OPTION _EXCLUDE _CLASS _EXPRESSIONS ] ] ,
2017-08-14 05:01:11 +02:00
type : "maintainability" ,
typescriptOnly : false ,
} ;
2017-05-28 00:38:50 +02:00
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
function walk ( ctx ) {
2017-12-10 21:51:33 +01:00
var sourceFile = ctx . sourceFile , _a = ctx . options , maxClasses = _a . maxClasses , excludeClassExpressions = _a . excludeClassExpressions ;
2017-05-28 00:38:50 +02:00
var classes = 0 ;
return ts . forEachChild ( sourceFile , function cb ( node ) {
2017-12-10 21:51:33 +01:00
if ( tsutils _1 . isClassDeclaration ( node ) || ( ! excludeClassExpressions && tsutils _1 . isClassExpression ( node ) ) ) {
2017-05-28 00:38:50 +02:00
classes ++ ;
if ( classes > maxClasses ) {
ctx . addFailureAtNode ( node , Rule . FAILURE _STRING ( maxClasses ) ) ;
}
}
return ts . forEachChild ( node , cb ) ;
} ) ;
}
var _a , _b , _c ;