2017-12-10 21:51:33 +01:00
"use strict" ;
/ * *
* @ license
* Copyright 2017 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 ts = require ( "typescript" ) ;
var Lint = require ( "../index" ) ;
var tsutils _1 = require ( "tsutils" ) ;
var OPTION _ _ALLOW _CONSTRUCTOR _ONLY = "allow-constructor-only" ;
var OPTION _ _ALLOW _EMPTY _CLASS = "allow-empty-class" ;
var OPTION _ _ALLOW _STATIC _ONLY = "allow-static-only" ;
function parseOptions ( options ) {
return {
allowConstructorOnly : options . indexOf ( OPTION _ _ALLOW _CONSTRUCTOR _ONLY ) !== - 1 ,
allowEmptyClass : options . indexOf ( OPTION _ _ALLOW _EMPTY _CLASS ) !== - 1 ,
allowStaticOnly : options . indexOf ( OPTION _ _ALLOW _STATIC _ONLY ) !== - 1 ,
} ;
}
var Rule = /** @class */ ( function ( _super ) {
tslib _1 . _ _extends ( Rule , _super ) ;
function Rule ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
Rule . prototype . apply = function ( sourceFile ) {
return this . applyWithWalker ( new NoUnnecessaryClassWalker ( sourceFile , this . ruleName , parseOptions ( this . ruleArguments ) ) ) ;
} ;
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "no-unnecessary-class" ,
2018-09-20 02:56:13 +02:00
description : Lint . Utils . dedent ( templateObject _1 || ( templateObject _1 = tslib _1 . _ _makeTemplateObject ( [ "\n Disallows classes that are not strictly necessary." ] , [ "\n Disallows classes that are not strictly necessary." ] ) ) ) ,
rationale : Lint . Utils . dedent ( templateObject _2 || ( templateObject _2 = tslib _1 . _ _makeTemplateObject ( [ "\n Users who come from a Java-style OO language may wrap\n their utility functions in an extra class, instead of\n putting them at the top level." ] , [ "\n Users who come from a Java-style OO language may wrap\n their utility functions in an extra class, instead of\n putting them at the top level." ] ) ) ) ,
optionsDescription : Lint . Utils . dedent ( templateObject _3 || ( templateObject _3 = tslib _1 . _ _makeTemplateObject ( [ "\n Three arguments may be optionally provided:\n\n * `\"allow-constructor-only\"` ignores classes whose members are constructors.\n * `\"allow-empty-class\"` ignores `class DemoClass {}`.\n * `\"allow-static-only\"` ignores classes whose members are static." ] , [ "\n Three arguments may be optionally provided:\n\n * \\`\"allow-constructor-only\"\\` ignores classes whose members are constructors.\n * \\`\"allow-empty-class\"\\` ignores \\`class DemoClass {}\\`.\n * \\`\"allow-static-only\"\\` ignores classes whose members are static." ] ) ) ) ,
2017-12-10 21:51:33 +01:00
options : {
type : "array" ,
items : {
type : "string" ,
} ,
minLength : 0 ,
maxLength : 3 ,
} ,
optionExamples : [ true , [ "allow-empty-class" , "allow-constructor-only" ] ] ,
type : "functionality" ,
typescriptOnly : false ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _CONSTRUCTOR _ONLY = "Every member of this class is a constructor. Use functions instead." ;
Rule . FAILURE _STATIC _ONLY = "Every member of this class is static. Use namespaces or plain objects instead." ;
Rule . FAILURE _EMPTY _CLASS = "This class has no members." ;
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
var NoUnnecessaryClassWalker = /** @class */ ( function ( _super ) {
tslib _1 . _ _extends ( NoUnnecessaryClassWalker , _super ) ;
function NoUnnecessaryClassWalker ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
NoUnnecessaryClassWalker . prototype . walk = function ( sourceFile ) {
var _this = this ;
var checkIfUnnecessaryClass = function ( node ) {
if ( tsutils _1 . isClassDeclaration ( node ) && ! hasExtendsClause ( node ) ) {
_this . checkMembers ( node ) ;
}
return ts . forEachChild ( node , checkIfUnnecessaryClass ) ;
} ;
ts . forEachChild ( sourceFile , checkIfUnnecessaryClass ) ;
} ;
NoUnnecessaryClassWalker . prototype . checkMembers = function ( node ) {
if ( node . members . length === 0 ) {
if ( ! this . options . allowEmptyClass ) {
this . addFailureAtNode ( tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . ClassKeyword ) , Rule . FAILURE _EMPTY _CLASS ) ;
}
return ;
}
var allMembersAreConstructors = node . members . every ( tsutils _1 . isConstructorDeclaration ) ;
if ( allMembersAreConstructors &&
! this . options . allowConstructorOnly &&
! node . members . some ( isConstructorWithShorthandProps ) ) {
this . addFailureAtNode ( tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . ClassKeyword , this . sourceFile ) , Rule . FAILURE _CONSTRUCTOR _ONLY ) ;
}
if ( ! allMembersAreConstructors &&
! this . options . allowStaticOnly &&
! node . members . some ( isNonStaticMember ) ) {
this . addFailureAtNode ( tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . ClassKeyword , this . sourceFile ) , Rule . FAILURE _STATIC _ONLY ) ;
}
} ;
return NoUnnecessaryClassWalker ;
} ( Lint . AbstractWalker ) ) ;
function isNonStaticMember ( member ) {
return ( isConstructorWithShorthandProps ( member ) ||
( ! tsutils _1 . isConstructorDeclaration ( member ) && ! tsutils _1 . hasModifier ( member . modifiers , ts . SyntaxKind . StaticKeyword ) ) ) ;
}
function hasExtendsClause ( declaration ) {
return ( declaration . heritageClauses !== undefined &&
declaration . heritageClauses [ 0 ] . token === ts . SyntaxKind . ExtendsKeyword ) ;
}
function isConstructorWithShorthandProps ( member ) {
return tsutils _1 . isConstructorDeclaration ( member ) && member . parameters . some ( tsutils _1 . isParameterProperty ) ;
}
2018-09-20 02:56:13 +02:00
var templateObject _1 , templateObject _2 , templateObject _3 ;