@@ -13,6 +13,7 @@ const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFun
13
13
function parseOptions ( options : string | Config | null ) : Required < Config > {
14
14
let functions = true ;
15
15
let classes = true ;
16
+ let enums = true ;
16
17
let variables = true ;
17
18
let typedefs = true ;
18
19
@@ -21,11 +22,12 @@ function parseOptions(options: string | Config | null): Required<Config> {
21
22
} else if ( typeof options === 'object' && options !== null ) {
22
23
functions = options . functions !== false ;
23
24
classes = options . classes !== false ;
25
+ enums = options . enums !== false ;
24
26
variables = options . variables !== false ;
25
27
typedefs = options . typedefs !== false ;
26
28
}
27
29
28
- return { functions, classes, variables, typedefs } ;
30
+ return { functions, classes, enums , variables, typedefs } ;
29
31
}
30
32
31
33
/**
@@ -35,6 +37,23 @@ function isTopLevelScope(scope: TSESLint.Scope.Scope): boolean {
35
37
return scope . type === 'module' || scope . type === 'global' ;
36
38
}
37
39
40
+ /**
41
+ * Checks whether or not a given variable declaration in an upper scope.
42
+ */
43
+ function isOuterScope (
44
+ variable : TSESLint . Scope . Variable ,
45
+ reference : TSESLint . Scope . Reference ,
46
+ ) : boolean {
47
+ if ( variable . scope . variableScope === reference . from . variableScope ) {
48
+ // allow the same scope only if it's the top level global/module scope
49
+ if ( ! isTopLevelScope ( variable . scope . variableScope ) ) {
50
+ return false ;
51
+ }
52
+ }
53
+
54
+ return true ;
55
+ }
56
+
38
57
/**
39
58
* Checks whether or not a given variable is a function declaration.
40
59
*/
@@ -43,24 +62,30 @@ function isFunction(variable: TSESLint.Scope.Variable): boolean {
43
62
}
44
63
45
64
/**
46
- * Checks whether or not a given variable is a class declaration in an upper function scope.
65
+ * Checks whether or not a given variable is a enum declaration in an upper function scope.
47
66
*/
48
- function isOuterClass (
67
+ function isOuterEnum (
49
68
variable : TSESLint . Scope . Variable ,
50
69
reference : TSESLint . Scope . Reference ,
51
70
) : boolean {
52
- if ( variable . defs [ 0 ] . type !== 'ClassName' ) {
53
- return false ;
54
- }
71
+ const node = variable . defs [ 0 ] . node as TSESTree . Node ;
55
72
56
- if ( variable . scope . variableScope === reference . from . variableScope ) {
57
- // allow the same scope only if it's the top level global/module scope
58
- if ( ! isTopLevelScope ( variable . scope . variableScope ) ) {
59
- return false ;
60
- }
61
- }
73
+ return (
74
+ node . type === AST_NODE_TYPES . TSEnumDeclaration &&
75
+ isOuterScope ( variable , reference )
76
+ ) ;
77
+ }
62
78
63
- return true ;
79
+ /**
80
+ * Checks whether or not a given variable is a class declaration in an upper function scope.
81
+ */
82
+ function isOuterClass (
83
+ variable : TSESLint . Scope . Variable ,
84
+ reference : TSESLint . Scope . Reference ,
85
+ ) : boolean {
86
+ return (
87
+ variable . defs [ 0 ] . type === 'ClassName' && isOuterScope ( variable , reference )
88
+ ) ;
64
89
}
65
90
66
91
/**
@@ -70,18 +95,9 @@ function isOuterVariable(
70
95
variable : TSESLint . Scope . Variable ,
71
96
reference : TSESLint . Scope . Reference ,
72
97
) : boolean {
73
- if ( variable . defs [ 0 ] . type !== 'Variable' ) {
74
- return false ;
75
- }
76
-
77
- if ( variable . scope . variableScope === reference . from . variableScope ) {
78
- // allow the same scope only if it's the top level global/module scope
79
- if ( ! isTopLevelScope ( variable . scope . variableScope ) ) {
80
- return false ;
81
- }
82
- }
83
-
84
- return true ;
98
+ return (
99
+ variable . defs [ 0 ] . type === 'Variable' && isOuterScope ( variable , reference )
100
+ ) ;
85
101
}
86
102
87
103
/**
@@ -147,6 +163,7 @@ function isInInitializer(
147
163
interface Config {
148
164
functions ?: boolean ;
149
165
classes ?: boolean ;
166
+ enums ?: boolean ;
150
167
variables ?: boolean ;
151
168
typedefs ?: boolean ;
152
169
}
@@ -176,6 +193,7 @@ export default util.createRule<Options, MessageIds>({
176
193
properties : {
177
194
functions : { type : 'boolean' } ,
178
195
classes : { type : 'boolean' } ,
196
+ enums : { type : 'boolean' } ,
179
197
variables : { type : 'boolean' } ,
180
198
typedefs : { type : 'boolean' } ,
181
199
} ,
@@ -189,6 +207,7 @@ export default util.createRule<Options, MessageIds>({
189
207
{
190
208
functions : true ,
191
209
classes : true ,
210
+ enums : true ,
192
211
variables : true ,
193
212
typedefs : true ,
194
213
} ,
@@ -214,6 +233,10 @@ export default util.createRule<Options, MessageIds>({
214
233
if ( isOuterVariable ( variable , reference ) ) {
215
234
return ! ! options . variables ;
216
235
}
236
+ if ( isOuterEnum ( variable , reference ) ) {
237
+ return ! ! options . enums ;
238
+ }
239
+
217
240
return true ;
218
241
}
219
242
0 commit comments