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" ) ;
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 */
Rule . FAILURE _STRING _FACTORY = function ( name ) {
2017-08-14 05:01:11 +02:00
return "Shadowed name: '" + name + "'" ;
2017-05-28 00:38:50 +02:00
} ;
Rule . prototype . apply = function ( sourceFile ) {
2017-08-14 05:01:11 +02:00
return this . applyWithWalker ( new NoShadowedVariableWalker ( sourceFile , this . ruleName , parseOptions ( this . ruleArguments [ 0 ] ) ) ) ;
} ;
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "no-shadowed-variable" ,
description : "Disallows shadowing variable declarations." ,
2018-09-20 02:56:13 +02:00
rationale : Lint . Utils . dedent ( templateObject _1 || ( templateObject _1 = tslib _1 . _ _makeTemplateObject ( [ "\n Shadowing a variable masks access to it and obscures to what value an identifier actually refers.\n For example, in the following code, it can be confusing why the filter is likely never true:\n\n ```\n const findNeighborsWithin = (instance: MyClass, instances: MyClass[]): MyClass[] => {\n return instances.filter((instance) => instance.neighbors.includes(instance));\n };\n ```\n " ] , [ "\n Shadowing a variable masks access to it and obscures to what value an identifier actually refers.\n For example, in the following code, it can be confusing why the filter is likely never true:\n\n \\`\\`\\`\n const findNeighborsWithin = (instance: MyClass, instances: MyClass[]): MyClass[] => {\n return instances.filter((instance) => instance.neighbors.includes(instance));\n };\n \\`\\`\\`\n " ] ) ) ) ,
optionsDescription : Lint . Utils . dedent ( templateObject _2 || ( templateObject _2 = tslib _1 . _ _makeTemplateObject ( [ "\n You can optionally pass an object to disable checking for certain kinds of declarations.\n Possible keys are `\"class\"`, `\"enum\"`, `\"function\"`, `\"import\"`, `\"interface\"`, `\"namespace\"`, `\"typeAlias\"`\n and `\"typeParameter\"`. Just set the value to `false` for the check you want to disable.\n All checks default to `true`, i.e. are enabled by default.\n Note that you cannot disable variables and parameters.\n\n The option `\"temporalDeadZone\"` defaults to `true` which shows errors when shadowing block scoped declarations in their\n temporal dead zone. When set to `false` parameters, classes, enums and variables declared\n with `let` or `const` are not considered shadowed if the shadowing occurs within their\n [temporal dead zone](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified).\n\n The following example shows how the `\"temporalDeadZone\"` option changes the linting result:\n\n ```ts\n function fn(value) {\n if (value) {\n const tmp = value; // no error on this line if \"temporalDeadZone\" is false\n return tmp;\n }\n let tmp = undefined;\n if (!value) {\n const tmp = value; // this line always contains an error\n return tmp;\n }\n }\n ```\n " ] , [ "\n You can optionally pass an object to disable checking for certain kinds of declarations.\n Possible keys are \\`\"class\"\\`, \\`\"enum\"\\`, \\`\"function\"\\`, \\`\"import\"\\`, \\`\"interface\"\\`, \\`\"namespace\"\\`, \\`\"typeAlias\"\\`\n and \\`\"typeParameter\"\\`. Just set the value to \\`false\\` for the check you want to disable.\n All checks default to \\`true\\`, i.e. are enabled by default.\n Note that you cannot disable variables and parameters.\n\n The option \\`\"temporalDeadZone\"\\` defaults to \\`true\\` which shows errors when shadowing block scoped declarations in their\n temporal dead zone. When set to \\`false\\` parameters, classes, enums and variables declared\n with \\`let\\` or \\`const\\` are not considered shadowed if the shadowing occurs within their\n [temporal dead zone](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified).\n\n The following example shows how the \\`\"temporalDeadZone\"\\` option changes the linting result:\n\n \\`\\`\\`ts\n function fn(value) {\n if (value) {\n const tmp = value; // no error on this line if \"temporalDeadZone\" is false\n return tmp;\n }\n let tmp = undefined;\n if (!value) {\n const tmp = value; // this line always contains an error\n return tmp;\n }\n }\n \\`\\`\\`\n " ] ) ) ) ,
2017-08-14 05:01:11 +02:00
options : {
type : "object" ,
properties : {
class : { type : "boolean" } ,
enum : { type : "boolean" } ,
function : { type : "boolean" } ,
import : { type : "boolean" } ,
interface : { type : "boolean" } ,
namespace : { type : "boolean" } ,
typeAlias : { type : "boolean" } ,
typeParameter : { type : "boolean" } ,
2018-09-20 02:56:13 +02:00
temporalDeadZone : { type : "boolean" } ,
2017-08-14 05:01:11 +02:00
} ,
} ,
optionExamples : [
true ,
[ true , { class : true , enum : true , function : true , interface : false , namespace : true , typeAlias : false , typeParameter : false } ] ,
] ,
type : "functionality" ,
typescriptOnly : false ,
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 parseOptions ( option ) {
2018-09-20 02:56:13 +02:00
return tslib _1 . _ _assign ( { class : true , enum : true , function : true , import : true , interface : true , namespace : true , temporalDeadZone : true , typeAlias : true , typeParameter : true } , option ) ;
2017-08-14 05:01:11 +02:00
}
2017-10-14 18:40:54 +02:00
var Scope = /** @class */ ( function ( ) {
2017-08-14 05:01:11 +02:00
function Scope ( functionScope ) {
this . variables = new Map ( ) ;
this . variablesSeen = new Map ( ) ;
this . reassigned = new Set ( ) ;
// if no functionScope is provided we are in the process of creating a new function scope, which for consistency links to itself
this . functionScope = functionScope !== undefined ? functionScope : this ;
}
2018-09-20 02:56:13 +02:00
Scope . prototype . addVariable = function ( identifier , blockScoped , tdz ) {
2017-08-14 05:01:11 +02:00
if ( blockScoped === void 0 ) { blockScoped = true ; }
2018-09-20 02:56:13 +02:00
if ( tdz === void 0 ) { tdz = false ; }
2017-08-14 05:01:11 +02:00
// block scoped variables go to the block scope, function scoped variables to the containing function scope
var scope = blockScoped ? this : this . functionScope ;
var list = scope . variables . get ( identifier . text ) ;
2018-09-20 02:56:13 +02:00
var variableInfo = {
identifier : identifier ,
tdz : tdz ,
} ;
2017-08-14 05:01:11 +02:00
if ( list === undefined ) {
2018-09-20 02:56:13 +02:00
scope . variables . set ( identifier . text , [ variableInfo ] ) ;
2017-08-14 05:01:11 +02:00
}
else {
2018-09-20 02:56:13 +02:00
list . push ( variableInfo ) ;
2017-08-14 05:01:11 +02:00
}
} ;
return Scope ;
} ( ) ) ;
2017-10-14 18:40:54 +02:00
var NoShadowedVariableWalker = /** @class */ ( function ( _super ) {
2017-05-28 00:38:50 +02:00
tslib _1 . _ _extends ( NoShadowedVariableWalker , _super ) ;
function NoShadowedVariableWalker ( ) {
2018-09-20 02:56:13 +02:00
var _this = _super !== null && _super . apply ( this , arguments ) || this ;
_this . scope = new Scope ( ) ;
return _this ;
2017-05-28 00:38:50 +02:00
}
2017-08-14 05:01:11 +02:00
NoShadowedVariableWalker . prototype . walk = function ( sourceFile ) {
var _this = this ;
2018-09-20 02:56:13 +02:00
if ( sourceFile . isDeclarationFile ) {
return ;
}
2017-08-14 05:01:11 +02:00
this . scope = new Scope ( ) ;
var cb = function ( node ) {
var parentScope = _this . scope ;
if ( ( _this . options . function && tsutils _1 . isFunctionExpression ( node ) || _this . options . class && tsutils _1 . isClassExpression ( node ) ) &&
node . name !== undefined ) {
/ * s p e c i a l h a n d l i n g f o r n a m e d f u n c t i o n a n d c l a s s e x p r e s s i o n s :
technically the name of the function is only visible inside of it ,
but variables with the same name declared inside don ' t cause compiler errors .
Therefore we add an additional function scope only for the function name to avoid merging with other declarations * /
var functionScope = new Scope ( ) ;
functionScope . addVariable ( node . name , false ) ;
_this . scope = new Scope ( ) ;
if ( tsutils _1 . isClassExpression ( node ) ) {
_this . visitClassLikeDeclaration ( node , functionScope , cb ) ;
}
else {
ts . forEachChild ( node , cb ) ;
}
_this . onScopeEnd ( functionScope ) ;
_this . scope = functionScope ;
_this . onScopeEnd ( parentScope ) ;
_this . scope = parentScope ;
return ;
}
/ * V i s i t d e c o r a t o r s b e f o r e e n t e r i n g a f u n c t i o n s c o p e .
In the AST decorators are children of the declaration they decorate , but we don ' t want to warn for the following code :
@ decorator ( ( param ) => param )
function foo ( param ) { }
* /
if ( node . decorators !== undefined ) {
for ( var _i = 0 , _a = node . decorators ; _i < _a . length ; _i ++ ) {
var decorator = _a [ _i ] ;
ts . forEachChild ( decorator , cb ) ;
}
}
var boundary = tsutils _1 . isScopeBoundary ( node ) ;
if ( boundary === 2 /* Block */ ) {
_this . scope = new Scope ( parentScope . functionScope ) ;
}
else if ( boundary === 1 /* Function */ ) {
_this . scope = new Scope ( ) ;
}
switch ( node . kind ) {
case ts . SyntaxKind . Decorator :
return ; // handled above
case ts . SyntaxKind . VariableDeclarationList :
_this . handleVariableDeclarationList ( node ) ;
break ;
case ts . SyntaxKind . TypeParameter :
if ( _this . options . typeParameter ) {
_this . scope . addVariable ( node . name ) ;
}
break ;
case ts . SyntaxKind . FunctionDeclaration :
if ( _this . options . function && node . name !== undefined ) {
parentScope . addVariable ( node . name , false ) ;
}
break ;
case ts . SyntaxKind . ClassDeclaration :
if ( _this . options . class && node . name !== undefined ) {
2018-09-20 02:56:13 +02:00
parentScope . addVariable ( node . name , true , true ) ;
2017-08-14 05:01:11 +02:00
}
// falls through
case ts . SyntaxKind . ClassExpression :
_this . visitClassLikeDeclaration ( node , parentScope , cb ) ;
_this . onScopeEnd ( parentScope ) ;
_this . scope = parentScope ;
return ;
case ts . SyntaxKind . TypeAliasDeclaration :
if ( _this . options . typeAlias ) {
parentScope . addVariable ( node . name ) ;
}
break ;
case ts . SyntaxKind . EnumDeclaration :
if ( _this . options . enum ) {
2018-09-20 02:56:13 +02:00
parentScope . addVariable ( node . name , true , true ) ;
2017-08-14 05:01:11 +02:00
}
break ;
case ts . SyntaxKind . InterfaceDeclaration :
if ( _this . options . interface ) {
parentScope . addVariable ( node . name ) ;
}
break ;
case ts . SyntaxKind . Parameter :
if ( node . parent . kind !== ts . SyntaxKind . IndexSignature &&
! tsutils _1 . isThisParameter ( node ) &&
tsutils _1 . isFunctionWithBody ( node . parent ) ) {
2018-09-20 02:56:13 +02:00
_this . handleBindingName ( node . name , false , true ) ;
2017-08-14 05:01:11 +02:00
}
break ;
case ts . SyntaxKind . ModuleDeclaration :
if ( _this . options . namespace &&
node . parent . kind !== ts . SyntaxKind . ModuleDeclaration &&
2018-09-20 02:56:13 +02:00
node . name . kind === ts . SyntaxKind . Identifier &&
! tsutils _1 . isNodeFlagSet ( node , ts . NodeFlags . GlobalAugmentation ) ) {
2017-08-14 05:01:11 +02:00
parentScope . addVariable ( node . name , false ) ;
}
2018-09-20 02:56:13 +02:00
if ( tsutils _1 . hasModifier ( node . modifiers , ts . SyntaxKind . DeclareKeyword ) ) {
_this . onScopeEnd ( parentScope ) ;
_this . scope = parentScope ;
return ; // don't check any ambient declaration blocks
}
2017-08-14 05:01:11 +02:00
break ;
case ts . SyntaxKind . ImportClause :
if ( _this . options . import && node . name !== undefined ) {
_this . scope . addVariable ( node . name , false ) ;
}
break ;
case ts . SyntaxKind . NamespaceImport :
case ts . SyntaxKind . ImportSpecifier :
case ts . SyntaxKind . ImportEqualsDeclaration :
if ( _this . options . import ) {
_this . scope . addVariable ( node . name , false ) ;
}
}
if ( boundary !== 0 /* None */ ) {
ts . forEachChild ( node , cb ) ;
_this . onScopeEnd ( parentScope ) ;
_this . scope = parentScope ;
}
else {
return ts . forEachChild ( node , cb ) ;
}
} ;
ts . forEachChild ( sourceFile , cb ) ;
this . onScopeEnd ( ) ;
2017-05-28 00:38:50 +02:00
} ;
2017-08-14 05:01:11 +02:00
NoShadowedVariableWalker . prototype . visitClassLikeDeclaration = function ( declaration , parentScope , cb ) {
var _this = this ;
var currentScope = this . scope ;
ts . forEachChild ( declaration , function ( node ) {
if ( ! tsutils _1 . hasModifier ( node . modifiers , ts . SyntaxKind . StaticKeyword ) ) {
return cb ( node ) ;
}
/ * D o n ' t t r e a t s t a t i c m e m b e r s a s c h i l d r e n o f t h e c l a s s ' s c o p e . T h a t a v o i d s h a d o w e d t y p e p a r a m e t e r w a r n i n g s o n s t a t i c m e m b e r s .
class C < T > {
static method < T > ( ) { }
}
* /
_this . scope = parentScope ;
cb ( node ) ;
_this . scope = currentScope ;
} ) ;
2017-05-28 00:38:50 +02:00
} ;
2017-08-14 05:01:11 +02:00
NoShadowedVariableWalker . prototype . handleVariableDeclarationList = function ( node ) {
var blockScoped = tsutils _1 . isBlockScopedVariableDeclarationList ( node ) ;
for ( var _i = 0 , _a = node . declarations ; _i < _a . length ; _i ++ ) {
var variable = _a [ _i ] ;
this . handleBindingName ( variable . name , blockScoped ) ;
2017-05-28 00:38:50 +02:00
}
} ;
2018-09-20 02:56:13 +02:00
NoShadowedVariableWalker . prototype . handleBindingName = function ( node , blockScoped , tdz ) {
if ( tdz === void 0 ) { tdz = blockScoped ; }
2017-08-14 05:01:11 +02:00
if ( node . kind === ts . SyntaxKind . Identifier ) {
2018-09-20 02:56:13 +02:00
this . scope . addVariable ( node , blockScoped , tdz ) ;
2017-05-28 00:38:50 +02:00
}
2017-08-14 05:01:11 +02:00
else {
for ( var _i = 0 , _a = node . elements ; _i < _a . length ; _i ++ ) {
var element = _a [ _i ] ;
if ( element . kind !== ts . SyntaxKind . OmittedExpression ) {
2018-09-20 02:56:13 +02:00
this . handleBindingName ( element . name , blockScoped , tdz ) ;
2017-08-14 05:01:11 +02:00
}
}
2017-05-28 00:38:50 +02:00
}
} ;
2017-08-14 05:01:11 +02:00
NoShadowedVariableWalker . prototype . onScopeEnd = function ( parent ) {
2017-05-28 00:38:50 +02:00
var _this = this ;
2017-08-14 05:01:11 +02:00
var _a = this . scope , variables = _a . variables , variablesSeen = _a . variablesSeen ;
variablesSeen . forEach ( function ( identifiers , name ) {
2018-09-20 02:56:13 +02:00
var declarationsInScope = variables . get ( name ) ;
var _loop _1 = function ( identifier ) {
if ( declarationsInScope !== undefined &&
( _this . options . temporalDeadZone ||
// check if any of the declaration either has no temporal dead zone or is declared before the identifier
declarationsInScope . some ( function ( declaration ) { return ! declaration . tdz || declaration . identifier . pos < identifier . pos ; } ) ) ) {
2017-08-14 05:01:11 +02:00
_this . addFailureAtNode ( identifier , Rule . FAILURE _STRING _FACTORY ( name ) ) ;
}
2018-09-20 02:56:13 +02:00
else if ( parent !== undefined ) {
addOneToList ( parent . variablesSeen , name , identifier ) ;
}
} ;
for ( var _i = 0 , identifiers _1 = identifiers ; _i < identifiers _1 . length ; _i ++ ) {
var identifier = identifiers _1 [ _i ] ;
_loop _1 ( identifier ) ;
2017-08-14 05:01:11 +02:00
}
2017-05-28 00:38:50 +02:00
} ) ;
2017-08-14 05:01:11 +02:00
if ( parent !== undefined ) {
variables . forEach ( function ( identifiers , name ) {
addToList ( parent . variablesSeen , name , identifiers ) ;
} ) ;
}
2017-05-28 00:38:50 +02:00
} ;
return NoShadowedVariableWalker ;
2017-08-14 05:01:11 +02:00
} ( Lint . AbstractWalker ) ) ;
2018-09-20 02:56:13 +02:00
function addToList ( map , name , variables ) {
var list = map . get ( name ) ;
if ( list === undefined ) {
list = [ ] ;
map . set ( name , list ) ;
}
for ( var _i = 0 , variables _1 = variables ; _i < variables _1 . length ; _i ++ ) {
var variable = variables _1 [ _i ] ;
list . push ( variable . identifier ) ;
}
}
function addOneToList ( map , name , identifier ) {
2017-08-14 05:01:11 +02:00
var list = map . get ( name ) ;
if ( list === undefined ) {
2018-09-20 02:56:13 +02:00
map . set ( name , [ identifier ] ) ;
2017-08-14 05:01:11 +02:00
}
else {
2018-09-20 02:56:13 +02:00
list . push ( identifier ) ;
2017-08-14 05:01:11 +02:00
}
}
2018-09-20 02:56:13 +02:00
var templateObject _1 , templateObject _2 ;