@@ -11,7 +11,7 @@ export type Options = [
11
11
{
12
12
allow ?: string [ ] ;
13
13
builtinGlobals ?: boolean ;
14
- hoist ?: 'all' | 'functions' | 'never' ;
14
+ hoist ?: 'all' | 'functions' | 'functions-and-types' | ' never' | 'types ';
15
15
ignoreFunctionTypeParameterNameValueShadow ?: boolean ;
16
16
ignoreOnInitialization ?: boolean ;
17
17
ignoreTypeValueShadow ?: boolean ;
@@ -28,6 +28,13 @@ const allowedFunctionVariableDefTypes = new Set([
28
28
AST_NODE_TYPES . TSConstructorType ,
29
29
] ) ;
30
30
31
+ const functionsHoistedNodes = new Set ( [ AST_NODE_TYPES . FunctionDeclaration ] ) ;
32
+
33
+ const typesHoistedNodes = new Set ( [
34
+ AST_NODE_TYPES . TSInterfaceDeclaration ,
35
+ AST_NODE_TYPES . TSTypeAliasDeclaration ,
36
+ ] ) ;
37
+
31
38
export default createRule < Options , MessageIds > ( {
32
39
name : 'no-shadow' ,
33
40
meta : {
@@ -63,7 +70,7 @@ export default createRule<Options, MessageIds>({
63
70
type : 'string' ,
64
71
description :
65
72
'Whether to report shadowing before outer functions or variables are defined.' ,
66
- enum : [ 'all' , 'functions' , 'never' ] ,
73
+ enum : [ 'all' , 'functions' , 'functions-and-types' , ' never' , 'types '] ,
67
74
} ,
68
75
ignoreFunctionTypeParameterNameValueShadow : {
69
76
type : 'boolean' ,
@@ -88,7 +95,7 @@ export default createRule<Options, MessageIds>({
88
95
{
89
96
allow : [ ] ,
90
97
builtinGlobals : false ,
91
- hoist : 'functions' ,
98
+ hoist : 'functions-and-types ' ,
92
99
ignoreFunctionTypeParameterNameValueShadow : true ,
93
100
ignoreOnInitialization : false ,
94
101
ignoreTypeValueShadow : true ,
@@ -513,15 +520,30 @@ export default createRule<Options, MessageIds>({
513
520
const inner = getNameRange ( variable ) ;
514
521
const outer = getNameRange ( scopeVar ) ;
515
522
516
- return ! ! (
517
- inner &&
518
- outer &&
519
- inner [ 1 ] < outer [ 0 ] &&
520
- // Excepts FunctionDeclaration if is {"hoist":"function"}.
521
- ( options . hoist !== 'functions' ||
522
- ! outerDef ||
523
- outerDef . node . type !== AST_NODE_TYPES . FunctionDeclaration )
524
- ) ;
523
+ if ( ! inner || ! outer || inner [ 1 ] >= outer [ 0 ] ) {
524
+ return false ;
525
+ }
526
+
527
+ if ( ! outerDef ) {
528
+ return true ;
529
+ }
530
+
531
+ if ( options . hoist === 'functions' ) {
532
+ return ! functionsHoistedNodes . has ( outerDef . node . type ) ;
533
+ }
534
+
535
+ if ( options . hoist === 'types' ) {
536
+ return ! typesHoistedNodes . has ( outerDef . node . type ) ;
537
+ }
538
+
539
+ if ( options . hoist === 'functions-and-types' ) {
540
+ return (
541
+ ! functionsHoistedNodes . has ( outerDef . node . type ) &&
542
+ ! typesHoistedNodes . has ( outerDef . node . type )
543
+ ) ;
544
+ }
545
+
546
+ return true ;
525
547
}
526
548
527
549
/**
0 commit comments