1
- import { ScopeManager } from 'eslint- scope' ;
1
+ import { ScopeManager } from './ scope/scope-manager ' ;
2
2
import { Definition , ParameterDefinition } from 'eslint-scope/lib/definition' ;
3
3
import OriginalPatternVisitor from 'eslint-scope/lib/pattern-visitor' ;
4
4
import Reference from 'eslint-scope/lib/reference' ;
5
5
import OriginalReferencer from 'eslint-scope/lib/referencer' ;
6
- import { Scope } from 'eslint-scope/lib/scope' ;
7
6
import { getKeys as fallback } from 'eslint-visitor-keys' ;
8
7
import { ParserOptions } from './parser-options' ;
9
8
import { visitorKeys as childVisitorKeys } from './visitor-keys' ;
@@ -30,18 +29,6 @@ function overrideDefine(define: any) {
30
29
} ;
31
30
}
32
31
33
- /** The scope class for enum. */
34
- class EnumScope extends Scope {
35
- constructor (
36
- scopeManager : ScopeManager ,
37
- upperScope : Scope ,
38
- block : TSESTree . Node | null
39
- ) {
40
- // @ts -ignore
41
- super ( scopeManager , 'enum' , upperScope , block , false ) ;
42
- }
43
- }
44
-
45
32
class PatternVisitor extends OriginalPatternVisitor {
46
33
constructor (
47
34
options : PatternVisitorOptions ,
@@ -99,7 +86,7 @@ class PatternVisitor extends OriginalPatternVisitor {
99
86
}
100
87
}
101
88
102
- class Referencer extends OriginalReferencer {
89
+ class Referencer extends OriginalReferencer < ScopeManager > {
103
90
protected typeMode : boolean ;
104
91
105
92
constructor ( options : any , scopeManager : ScopeManager ) {
@@ -176,13 +163,13 @@ class Referencer extends OriginalReferencer {
176
163
scopeManager . __nestFunctionExpressionNameScope ( node ) ;
177
164
}
178
165
179
- // Process the type parameters
180
- this . visit ( typeParameters ) ;
181
-
182
166
// Open the function scope.
183
167
scopeManager . __nestFunctionScope ( node , this . isInnerMethodDefinition ) ;
184
168
const innerScope = this . currentScope ( ) ;
185
169
170
+ // Process the type parameters
171
+ this . visit ( typeParameters ) ;
172
+
186
173
// Process parameter declarations.
187
174
for ( let i = 0 ; i < params . length ; ++ i ) {
188
175
this . visitPattern (
@@ -344,29 +331,56 @@ class Referencer extends OriginalReferencer {
344
331
* @param node The TSDeclareFunction node to visit.
345
332
*/
346
333
TSDeclareFunction ( node : TSESTree . TSDeclareFunction ) : void {
347
- const upperTypeMode = this . typeMode ;
348
- const scope = this . currentScope ( ) ;
334
+ const scopeManager = this . scopeManager ;
335
+ const upperScope = this . currentScope ( ) ;
349
336
const { id, typeParameters, params, returnType } = node ;
350
337
351
338
// Ignore this if other overloadings have already existed.
352
339
if ( id ) {
353
- const variable = scope . set . get ( id . name ) ;
340
+ const variable = upperScope . set . get ( id . name ) ;
354
341
const defs = variable && variable . defs ;
355
342
const existed = defs && defs . some ( d => d . type === 'FunctionName' ) ;
356
343
if ( ! existed ) {
357
- scope . __define (
344
+ upperScope . __define (
358
345
id ,
359
346
new Definition ( 'FunctionName' , id , node , null , null , null )
360
347
) ;
361
348
}
362
349
}
363
350
364
- // Find `typeof` expressions.
365
- this . typeMode = true ;
351
+ // Open the function scope.
352
+ scopeManager . __nestEmptyFunctionScope ( node ) ;
353
+ const innerScope = this . currentScope ( ) ;
354
+
355
+ // Process the type parameters
366
356
this . visit ( typeParameters ) ;
367
- params . forEach ( this . visit , this ) ;
357
+
358
+ // Process parameter declarations.
359
+ for ( let i = 0 ; i < params . length ; ++ i ) {
360
+ this . visitPattern (
361
+ params [ i ] ,
362
+ { processRightHandNodes : true } ,
363
+ ( pattern , info ) => {
364
+ innerScope . __define (
365
+ pattern ,
366
+ new ParameterDefinition ( pattern , node , i , info . rest )
367
+ ) ;
368
+
369
+ // Set `variable.eslintUsed` to tell ESLint that the variable is used.
370
+ const variable = innerScope . set . get ( pattern . name ) ;
371
+ if ( variable ) {
372
+ variable . eslintUsed = true ;
373
+ }
374
+ this . referencingDefaultValue ( pattern , info . assignments , null , true ) ;
375
+ }
376
+ ) ;
377
+ }
378
+
379
+ // Process the return type.
368
380
this . visit ( returnType ) ;
369
- this . typeMode = upperTypeMode ;
381
+
382
+ // Close the function scope.
383
+ this . close ( node ) ;
370
384
}
371
385
372
386
/**
@@ -645,7 +659,7 @@ class Referencer extends OriginalReferencer {
645
659
scope . __define ( id , new Definition ( 'EnumName' , id , node ) ) ;
646
660
}
647
661
648
- scopeManager . __nestScope ( new EnumScope ( scopeManager , scope , node ) ) ;
662
+ scopeManager . __nestEnumScope ( node ) ;
649
663
for ( const member of members ) {
650
664
this . visit ( member ) ;
651
665
}
0 commit comments