2017-05-28 00:38:50 +02:00
"use strict" ;
/ * *
* @ license
* Copyright 2013 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" ) ;
2017-08-14 05:01:11 +02:00
var error _1 = require ( "../error" ) ;
2017-05-28 00:38:50 +02:00
var Lint = require ( "../index" ) ;
var OPTION _NO _PUBLIC = "no-public" ;
var OPTION _CHECK _ACCESSOR = "check-accessor" ;
var OPTION _CHECK _CONSTRUCTOR = "check-constructor" ;
2017-12-10 21:51:33 +01:00
var OPTION _CHECK _PARAMETER _PROPERTY = "check-parameter-property" ;
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 ;
}
Rule . FAILURE _STRING _FACTORY = function ( memberType , memberName ) {
memberName = memberName === undefined ? "" : " '" + memberName + "'" ;
return "The " + memberType + memberName + " must be marked either 'private', 'public', or 'protected'" ;
} ;
Rule . prototype . apply = function ( sourceFile ) {
var options = this . ruleArguments ;
var noPublic = options . indexOf ( OPTION _NO _PUBLIC ) !== - 1 ;
var checkAccessor = options . indexOf ( OPTION _CHECK _ACCESSOR ) !== - 1 ;
var checkConstructor = options . indexOf ( OPTION _CHECK _CONSTRUCTOR ) !== - 1 ;
2017-12-10 21:51:33 +01:00
var checkParameterProperty = options . indexOf ( OPTION _CHECK _PARAMETER _PROPERTY ) !== - 1 ;
2017-05-28 00:38:50 +02:00
if ( noPublic ) {
2017-12-10 21:51:33 +01:00
if ( checkAccessor || checkConstructor || checkParameterProperty ) {
2017-08-14 05:01:11 +02:00
error _1 . showWarningOnce ( "Warning: " + this . ruleName + " - If 'no-public' is present, it should be the only option." ) ;
return [ ] ;
2017-05-28 00:38:50 +02:00
}
2017-12-10 21:51:33 +01:00
checkAccessor = checkConstructor = checkParameterProperty = true ;
2017-05-28 00:38:50 +02:00
}
2017-08-14 05:01:11 +02:00
return this . applyWithFunction ( sourceFile , walk , {
checkAccessor : checkAccessor ,
checkConstructor : checkConstructor ,
2017-12-10 21:51:33 +01:00
checkParameterProperty : checkParameterProperty ,
2017-08-14 05:01:11 +02:00
noPublic : noPublic ,
} ) ;
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 : "member-access" ,
description : "Requires explicit visibility declarations for class members." ,
rationale : "Explicit visibility declarations can make code more readable and accessible for those new to TS." ,
2017-12-10 21:51:33 +01:00
optionsDescription : ( _a = [ "\n These arguments may be optionally provided:\n\n * `\"no-public\"` forbids public accessibility to be specified, because this is the default.\n * `\"check-accessor\"` enforces explicit visibility on get/set accessors\n * `\"check-constructor\"` enforces explicit visibility on constructors\n * `\"check-parameter-property\"` enforces explicit visibility on parameter properties" ] , _a . raw = [ "\n These arguments may be optionally provided:\n\n * \\`\"no-public\"\\` forbids public accessibility to be specified, because this is the default.\n * \\`\"check-accessor\"\\` enforces explicit visibility on get/set accessors\n * \\`\"check-constructor\"\\` enforces explicit visibility on constructors\n * \\`\"check-parameter-property\"\\` enforces explicit visibility on parameter properties" ] , Lint . Utils . dedent ( _a ) ) ,
2017-08-14 05:01:11 +02:00
options : {
type : "array" ,
items : {
type : "string" ,
2017-12-10 21:51:33 +01:00
enum : [ OPTION _NO _PUBLIC , OPTION _CHECK _ACCESSOR , OPTION _CHECK _CONSTRUCTOR , OPTION _CHECK _PARAMETER _PROPERTY ] ,
2017-08-14 05:01:11 +02:00
} ,
minLength : 0 ,
2017-12-10 21:51:33 +01:00
maxLength : 4 ,
2017-08-14 05:01:11 +02:00
} ,
optionExamples : [ true , [ true , OPTION _NO _PUBLIC ] , [ true , OPTION _CHECK _ACCESSOR ] ] ,
type : "typescript" ,
typescriptOnly : true ,
hasFix : true ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING _NO _PUBLIC = "'public' is implicit." ;
2017-05-28 00:38:50 +02:00
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
2017-08-14 05:01:11 +02:00
function walk ( ctx ) {
2017-12-10 21:51:33 +01:00
var _a = ctx . options , noPublic = _a . noPublic , checkAccessor = _a . checkAccessor , checkConstructor = _a . checkConstructor , checkParameterProperty = _a . checkParameterProperty ;
2017-05-28 00:38:50 +02:00
return ts . forEachChild ( ctx . sourceFile , function recur ( node ) {
if ( tsutils _1 . isClassLikeDeclaration ( node ) ) {
for ( var _i = 0 , _a = node . members ; _i < _a . length ; _i ++ ) {
var child = _a [ _i ] ;
if ( shouldCheck ( child ) ) {
check ( child ) ;
}
2017-12-10 21:51:33 +01:00
if ( checkParameterProperty && tsutils _1 . isConstructorDeclaration ( child ) && child . body !== undefined ) {
for ( var _b = 0 , _c = child . parameters ; _b < _c . length ; _b ++ ) {
var param = _c [ _b ] ;
if ( tsutils _1 . isParameterProperty ( param ) ) {
check ( param ) ;
}
}
}
2017-05-28 00:38:50 +02:00
}
}
return ts . forEachChild ( node , recur ) ;
} ) ;
function shouldCheck ( node ) {
switch ( node . kind ) {
case ts . SyntaxKind . Constructor :
return checkConstructor ;
case ts . SyntaxKind . GetAccessor :
case ts . SyntaxKind . SetAccessor :
return checkAccessor ;
case ts . SyntaxKind . MethodDeclaration :
case ts . SyntaxKind . PropertyDeclaration :
return true ;
default :
return false ;
}
}
function check ( node ) {
if ( Lint . hasModifier ( node . modifiers , ts . SyntaxKind . ProtectedKeyword , ts . SyntaxKind . PrivateKeyword ) ) {
return ;
}
2017-08-14 05:01:11 +02:00
var publicKeyword = tsutils _1 . getModifier ( node , ts . SyntaxKind . PublicKeyword ) ;
if ( noPublic && publicKeyword !== undefined ) {
2017-12-10 21:51:33 +01:00
var readonlyKeyword = tsutils _1 . getModifier ( node , ts . SyntaxKind . ReadonlyKeyword ) ;
// public is not optional for parameter property without the readonly modifier
var isPublicOptional = node . kind !== ts . SyntaxKind . Parameter || readonlyKeyword !== undefined ;
if ( isPublicOptional ) {
var start = publicKeyword . end - "public" . length ;
ctx . addFailure ( start , publicKeyword . end , Rule . FAILURE _STRING _NO _PUBLIC , Lint . Replacement . deleteFromTo ( start , tsutils _1 . getNextToken ( publicKeyword , ctx . sourceFile ) . getStart ( ctx . sourceFile ) ) ) ;
}
2017-05-28 00:38:50 +02:00
}
2017-08-14 05:01:11 +02:00
if ( ! noPublic && publicKeyword === undefined ) {
var nameNode = node . kind === ts . SyntaxKind . Constructor
? tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . ConstructorKeyword , ctx . sourceFile )
2017-05-28 00:38:50 +02:00
: node . name !== undefined ? node . name : node ;
2017-08-14 05:01:11 +02:00
var memberName = node . name !== undefined && node . name . kind === ts . SyntaxKind . Identifier ? node . name . text : undefined ;
2017-10-14 18:40:54 +02:00
ctx . addFailureAtNode ( nameNode , Rule . FAILURE _STRING _FACTORY ( typeToString ( node ) , memberName ) , Lint . Replacement . appendText ( getInsertionPosition ( node , ctx . sourceFile ) , "public " ) ) ;
2017-05-28 00:38:50 +02:00
}
}
}
2017-10-14 18:40:54 +02:00
function getInsertionPosition ( member , sourceFile ) {
var node = member . decorators === undefined ? member : tsutils _1 . getTokenAtPosition ( member , member . decorators . end , sourceFile ) ;
return node . getStart ( sourceFile ) ;
}
2017-08-14 05:01:11 +02:00
function typeToString ( node ) {
2017-05-28 00:38:50 +02:00
switch ( node . kind ) {
case ts . SyntaxKind . MethodDeclaration :
return "class method" ;
case ts . SyntaxKind . PropertyDeclaration :
return "class property" ;
case ts . SyntaxKind . Constructor :
return "class constructor" ;
case ts . SyntaxKind . GetAccessor :
return "get property accessor" ;
case ts . SyntaxKind . SetAccessor :
return "set property accessor" ;
2017-12-10 21:51:33 +01:00
case ts . SyntaxKind . Parameter :
return "parameter property" ;
2017-05-28 00:38:50 +02:00
default :
throw new Error ( "unhandled node type " + ts . SyntaxKind [ node . kind ] ) ;
}
}
var _a ;