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" ) ;
2017-08-14 05:01:11 +02:00
var tsutils _1 = require ( "tsutils" ) ;
2017-05-28 00:38:50 +02:00
var ts = require ( "typescript" ) ;
var Lint = require ( "../index" ) ;
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 */
2017-08-14 05:01:11 +02:00
Rule . FAILURE _STRING = function ( name , message ) {
return name + " is deprecated" + ( message === "" ? "." : ": " + message . trim ( ) ) ;
} ;
2017-05-28 00:38:50 +02:00
Rule . prototype . applyWithProgram = function ( sourceFile , program ) {
2017-10-14 18:40:54 +02:00
return this . applyWithFunction ( sourceFile , walk , undefined , program . getTypeChecker ( ) ) ;
2017-08-14 05:01:11 +02:00
} ;
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "deprecation" ,
description : "Warns when deprecated APIs are used." ,
descriptionDetails : ( _a = [ "Any usage of an identifier\n with the @deprecated JSDoc annotation will trigger a warning.\n See http://usejsdoc.org/tags-deprecated.html" ] , _a . raw = [ "Any usage of an identifier\n with the @deprecated JSDoc annotation will trigger a warning.\n See http://usejsdoc.org/tags-deprecated.html" ] , Lint . Utils . dedent ( _a ) ) ,
rationale : "Deprecated APIs should be avoided, and usage updated." ,
optionsDescription : "" ,
options : null ,
optionExamples : [ ] ,
type : "maintainability" ,
typescriptOnly : false ,
requiresTypeInfo : true ,
2017-05-28 00:38:50 +02:00
} ;
return Rule ;
} ( Lint . Rules . TypedRule ) ) ;
exports . Rule = Rule ;
2017-08-14 05:01:11 +02:00
function walk ( ctx , tc ) {
return ts . forEachChild ( ctx . sourceFile , function cb ( node ) {
if ( tsutils _1 . isIdentifier ( node ) ) {
if ( ! isDeclaration ( node ) ) {
var deprecation = getDeprecation ( node , tc ) ;
if ( deprecation !== undefined ) {
ctx . addFailureAtNode ( node , Rule . FAILURE _STRING ( node . text , deprecation ) ) ;
}
}
2017-05-28 00:38:50 +02:00
}
2017-08-14 05:01:11 +02:00
else {
2017-10-14 18:40:54 +02:00
switch ( node . kind ) {
case ts . SyntaxKind . ImportDeclaration :
case ts . SyntaxKind . ImportEqualsDeclaration :
case ts . SyntaxKind . ExportDeclaration :
case ts . SyntaxKind . ExportAssignment :
return ;
}
2017-08-14 05:01:11 +02:00
return ts . forEachChild ( node , cb ) ;
2017-05-28 00:38:50 +02:00
}
2017-08-14 05:01:11 +02:00
} ) ;
}
function isDeclaration ( identifier ) {
var parent = identifier . parent ;
switch ( parent . kind ) {
case ts . SyntaxKind . ClassDeclaration :
case ts . SyntaxKind . ClassExpression :
case ts . SyntaxKind . InterfaceDeclaration :
case ts . SyntaxKind . TypeParameter :
case ts . SyntaxKind . FunctionExpression :
case ts . SyntaxKind . FunctionDeclaration :
case ts . SyntaxKind . LabeledStatement :
case ts . SyntaxKind . JsxAttribute :
case ts . SyntaxKind . MethodDeclaration :
case ts . SyntaxKind . MethodSignature :
case ts . SyntaxKind . PropertySignature :
case ts . SyntaxKind . TypeAliasDeclaration :
case ts . SyntaxKind . GetAccessor :
case ts . SyntaxKind . SetAccessor :
case ts . SyntaxKind . EnumDeclaration :
return true ;
case ts . SyntaxKind . VariableDeclaration :
case ts . SyntaxKind . TypeAliasDeclaration :
case ts . SyntaxKind . Parameter :
case ts . SyntaxKind . ModuleDeclaration :
case ts . SyntaxKind . PropertyDeclaration :
case ts . SyntaxKind . PropertyAssignment :
case ts . SyntaxKind . EnumMember :
2017-10-14 18:40:54 +02:00
case ts . SyntaxKind . ImportEqualsDeclaration :
2017-08-14 05:01:11 +02:00
return parent . name === identifier ;
case ts . SyntaxKind . BindingElement :
2017-10-14 18:40:54 +02:00
// return true for `b` in `const {a: b} = obj"`
2017-08-14 05:01:11 +02:00
return parent . name === identifier &&
parent . propertyName !== undefined ;
default :
return false ;
}
}
2017-10-14 18:40:54 +02:00
function getCallExpresion ( node ) {
var parent = node . parent ;
if ( tsutils _1 . isPropertyAccessExpression ( parent ) && parent . name === node ) {
node = parent ;
parent = node . parent ;
}
return tsutils _1 . isTaggedTemplateExpression ( parent ) || tsutils _1 . isCallExpression ( parent ) && parent . expression === node ? parent : undefined ;
}
2017-08-14 05:01:11 +02:00
function getDeprecation ( node , tc ) {
2017-10-14 18:40:54 +02:00
var callExpression = getCallExpresion ( node ) ;
if ( callExpression !== undefined ) {
var result = getSignatureDeprecation ( tc . getResolvedSignature ( callExpression ) ) ;
if ( result !== undefined ) {
return result ;
}
}
2017-08-14 05:01:11 +02:00
var symbol = tc . getSymbolAtLocation ( node ) ;
if ( symbol !== undefined && Lint . isSymbolFlagSet ( symbol , ts . SymbolFlags . Alias ) ) {
symbol = tc . getAliasedSymbol ( symbol ) ;
}
2017-10-14 18:40:54 +02:00
if ( symbol === undefined ||
// if this is a CallExpression and the declaration is a function or method,
// stop here to avoid collecting JsDoc of all overload signatures
callExpression !== undefined && isFunctionOrMethod ( symbol . declarations ) ) {
return undefined ;
}
return getSymbolDeprecation ( symbol ) ;
}
function findDeprecationTag ( tags ) {
for ( var _i = 0 , tags _1 = tags ; _i < tags _1 . length ; _i ++ ) {
var tag = tags _1 [ _i ] ;
if ( tag . name === "deprecated" ) {
return tag . text ;
}
2017-08-14 05:01:11 +02:00
}
return undefined ;
}
2017-10-14 18:40:54 +02:00
function getSymbolDeprecation ( symbol ) {
2017-08-14 05:01:11 +02:00
if ( symbol . getJsDocTags !== undefined ) {
2017-10-14 18:40:54 +02:00
return findDeprecationTag ( symbol . getJsDocTags ( ) ) ;
2017-08-14 05:01:11 +02:00
}
// for compatibility with typescript@<2.3.0
return getDeprecationFromDeclarations ( symbol . declarations ) ;
}
2017-10-14 18:40:54 +02:00
function getSignatureDeprecation ( signature ) {
if ( signature === undefined ) {
return undefined ;
}
if ( signature . getJsDocTags !== undefined ) {
return findDeprecationTag ( signature . getJsDocTags ( ) ) ;
}
// for compatibility with typescript@<2.3.0
return signature . declaration === undefined ? undefined : getDeprecationFromDeclaration ( signature . declaration ) ;
}
2017-08-14 05:01:11 +02:00
function getDeprecationFromDeclarations ( declarations ) {
if ( declarations === undefined ) {
return undefined ;
}
var declaration ;
for ( var _i = 0 , declarations _1 = declarations ; _i < declarations _1 . length ; _i ++ ) {
declaration = declarations _1 [ _i ] ;
if ( tsutils _1 . isBindingElement ( declaration ) ) {
declaration = tsutils _1 . getDeclarationOfBindingElement ( declaration ) ;
}
if ( tsutils _1 . isVariableDeclaration ( declaration ) ) {
declaration = declaration . parent ;
}
if ( tsutils _1 . isVariableDeclarationList ( declaration ) ) {
declaration = declaration . parent ;
}
2017-10-14 18:40:54 +02:00
var result = getDeprecationFromDeclaration ( declaration ) ;
if ( result !== undefined ) {
return result ;
}
}
return undefined ;
}
function getDeprecationFromDeclaration ( declaration ) {
for ( var _i = 0 , _a = declaration . getChildren ( ) ; _i < _a . length ; _i ++ ) {
var child = _a [ _i ] ;
if ( ! tsutils _1 . isJsDoc ( child ) ) {
break ;
}
if ( child . tags === undefined ) {
continue ;
}
for ( var _b = 0 , _c = child . tags ; _b < _c . length ; _b ++ ) {
var tag = _c [ _b ] ;
if ( tag . tagName . text === "deprecated" ) {
return tag . comment === undefined ? "" : tag . comment ;
2017-05-28 00:38:50 +02:00
}
}
2017-08-14 05:01:11 +02:00
}
return undefined ;
}
2017-10-14 18:40:54 +02:00
function isFunctionOrMethod ( declarations ) {
if ( declarations === undefined || declarations . length === 0 ) {
return false ;
}
switch ( declarations [ 0 ] . kind ) {
case ts . SyntaxKind . MethodDeclaration :
case ts . SyntaxKind . FunctionDeclaration :
case ts . SyntaxKind . FunctionExpression :
case ts . SyntaxKind . MethodSignature :
return true ;
default :
return false ;
}
}
2017-08-14 05:01:11 +02:00
var _a ;