forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze-scope.ts
892 lines (785 loc) · 23 KB
/
analyze-scope.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
import {
TSESTree,
TSESLintScope,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import { getKeys as fallback } from 'eslint-visitor-keys';
import { ParserOptions } from './parser-options';
import { ScopeManager } from './scope/scope-manager';
import { visitorKeys as childVisitorKeys } from '@typescript-eslint/typescript-estree';
/**
* Define the override function of `Scope#__define` for global augmentation.
* @param {Function} define The original Scope#__define method.
* @returns {Function} The override function.
*/
function overrideDefine(
define: (node: TSESTree.Node, def: TSESLintScope.Definition) => void,
) {
return function (
this: TSESLintScope.Scope,
node: TSESTree.Node,
definition: TSESLintScope.Definition,
): void {
define.call(this, node, definition);
// Set `variable.eslintUsed` to tell ESLint that the variable is exported.
const variable =
'name' in node &&
typeof node.name === 'string' &&
this.set.get(node.name);
if (variable) {
variable.eslintUsed = true;
}
};
}
class PatternVisitor extends TSESLintScope.PatternVisitor {
constructor(
options: TSESLintScope.PatternVisitorOptions,
rootPattern: TSESTree.BaseNode,
callback: TSESLintScope.PatternVisitorCallback,
) {
super(options, rootPattern, callback);
}
Identifier(node: TSESTree.Identifier): void {
super.Identifier(node);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
ArrayPattern(node: TSESTree.ArrayPattern): void {
node.elements.forEach(this.visit, this);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
ObjectPattern(node: TSESTree.ObjectPattern): void {
node.properties.forEach(this.visit, this);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
RestElement(node: TSESTree.RestElement): void {
super.RestElement(node);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
TSParameterProperty(node: TSESTree.TSParameterProperty): void {
this.visit(node.parameter);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
}
}
class Referencer extends TSESLintScope.Referencer<ScopeManager> {
protected typeMode: boolean;
constructor(
options: TSESLintScope.ScopeManagerOptions,
scopeManager: ScopeManager,
) {
super(options, scopeManager);
this.typeMode = false;
}
/**
* Override to use PatternVisitor we overrode.
* @param node The Identifier node to visit.
* @param [options] The flag to visit right-hand side nodes.
* @param callback The callback function for left-hand side nodes.
*/
visitPattern<T extends TSESTree.BaseNode>(
node: T,
options: TSESLintScope.PatternVisitorOptions,
callback: TSESLintScope.PatternVisitorCallback,
): void {
if (!node) {
return;
}
if (typeof options === 'function') {
callback = options;
options = { processRightHandNodes: false };
}
const visitor = new PatternVisitor(this.options, node, callback);
visitor.visit(node);
if (options.processRightHandNodes) {
visitor.rightHandNodes.forEach(this.visit, this);
}
}
/**
* Override.
* Visit `node.typeParameters` and `node.returnType` additionally to find `typeof` expressions.
* @param node The function node to visit.
*/
visitFunction(
node:
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.ArrowFunctionExpression,
): void {
const { type, id, typeParameters, params, returnType, body } = node;
const scopeManager = this.scopeManager;
const upperScope = this.currentScope();
// Process the name.
if (type === AST_NODE_TYPES.FunctionDeclaration && id) {
upperScope.__define(
id,
new TSESLintScope.Definition(
'FunctionName',
id,
node,
null,
null,
null,
),
);
// Remove overload definition to avoid confusion of no-redeclare rule.
const { defs, identifiers } = upperScope.set.get(id.name)!;
for (let i = 0; i < defs.length; ++i) {
const def = defs[i];
if (
def.type === 'FunctionName' &&
def.node.type === AST_NODE_TYPES.TSDeclareFunction
) {
defs.splice(i, 1);
identifiers.splice(i, 1);
break;
}
}
} else if (type === AST_NODE_TYPES.FunctionExpression && id) {
scopeManager.__nestFunctionExpressionNameScope(node);
}
// Open the function scope.
scopeManager.__nestFunctionScope(node, this.isInnerMethodDefinition);
const innerScope = this.currentScope();
// Process the type parameters
this.visit(typeParameters);
// Process parameter declarations.
for (let i = 0; i < params.length; ++i) {
this.visitPattern(
params[i],
{ processRightHandNodes: true },
(pattern, info) => {
if (
pattern.type !== AST_NODE_TYPES.Identifier ||
pattern.name !== 'this'
) {
innerScope.__define(
pattern,
new TSESLintScope.ParameterDefinition(
pattern,
node,
i,
info.rest,
),
);
this.referencingDefaultValue(pattern, info.assignments, null, true);
}
},
);
}
// Process the return type.
this.visit(returnType);
// Process the body.
if (body && body.type === AST_NODE_TYPES.BlockStatement) {
this.visitChildren(body);
} else {
this.visit(body);
}
// Close the function scope.
this.close(node);
}
/**
* Override.
* Visit decorators.
* @param node The class node to visit.
*/
visitClass(node: TSESTree.ClassDeclaration | TSESTree.ClassExpression): void {
this.visitDecorators(node.decorators);
const upperTypeMode = this.typeMode;
this.typeMode = true;
if (node.superTypeParameters) {
this.visit(node.superTypeParameters);
}
if (node.implements) {
node.implements.forEach(this.visit, this);
}
this.typeMode = upperTypeMode;
super.visitClass(node);
}
/**
* Visit typeParameters.
* @param node The node to visit.
*/
visitTypeParameters(node: {
typeParameters?:
| TSESTree.TSTypeParameterDeclaration
| TSESTree.TSTypeParameterInstantiation;
}): void {
if (node.typeParameters) {
const upperTypeMode = this.typeMode;
this.typeMode = true;
this.visit(node.typeParameters);
this.typeMode = upperTypeMode;
}
}
/**
* Override.
*/
JSXOpeningElement(node: TSESTree.JSXOpeningElement): void {
this.visit(node.name);
this.visitTypeParameters(node);
node.attributes.forEach(this.visit, this);
}
/**
* Override.
* Don't create the reference object in the type mode.
* @param node The Identifier node to visit.
*/
Identifier(node: TSESTree.Identifier): void {
this.visitDecorators(node.decorators);
if (!this.typeMode) {
super.Identifier(node);
}
this.visit(node.typeAnnotation);
}
/**
* Override.
* Visit decorators.
* @param node The MethodDefinition node to visit.
*/
MethodDefinition(
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition,
): void {
this.visitDecorators(node.decorators);
super.MethodDefinition(node);
}
/**
* Don't create the reference object for the key if not computed.
* @param node The ClassProperty node to visit.
*/
ClassProperty(
node: TSESTree.ClassProperty | TSESTree.TSAbstractClassProperty,
): void {
const upperTypeMode = this.typeMode;
const { computed, decorators, key, typeAnnotation, value } = node;
this.typeMode = false;
this.visitDecorators(decorators);
if (computed) {
this.visit(key);
}
this.typeMode = true;
this.visit(typeAnnotation);
this.typeMode = false;
this.visit(value);
this.typeMode = upperTypeMode;
}
/**
* Visit new expression.
* @param node The NewExpression node to visit.
*/
NewExpression(node: TSESTree.NewExpression): void {
this.visitTypeParameters(node);
this.visit(node.callee);
node.arguments.forEach(this.visit, this);
}
/**
* Override.
* Visit call expression.
* @param node The CallExpression node to visit.
*/
CallExpression(node: TSESTree.CallExpression): void {
this.visitTypeParameters(node);
this.visit(node.callee);
node.arguments.forEach(this.visit, this);
}
/**
* Visit optional member expression.
* @param node The OptionalMemberExpression node to visit.
*/
OptionalMemberExpression(node: TSESTree.OptionalMemberExpression): void {
this.visit(node.object);
if (node.computed) {
this.visit(node.property);
}
}
/**
* Visit optional call expression.
* @param node The OptionalMemberExpression node to visit.
*/
OptionalCallExpression(node: TSESTree.OptionalCallExpression): void {
this.visitTypeParameters(node);
this.visit(node.callee);
node.arguments.forEach(this.visit, this);
}
/**
* Define the variable of this function declaration only once.
* Because to avoid confusion of `no-redeclare` rule by overloading.
* @param node The TSDeclareFunction node to visit.
*/
TSDeclareFunction(node: TSESTree.TSDeclareFunction): void {
const scopeManager = this.scopeManager;
const upperScope = this.currentScope();
const { id, typeParameters, params, returnType } = node;
// Ignore this if other overload have already existed.
if (id) {
const variable = upperScope.set.get(id.name);
const defs = variable?.defs;
const existed = defs?.some((d): boolean => d.type === 'FunctionName');
if (!existed) {
upperScope.__define(
id,
new TSESLintScope.Definition(
'FunctionName',
id,
node,
null,
null,
null,
),
);
}
}
// Open the function scope.
scopeManager.__nestEmptyFunctionScope(node);
const innerScope = this.currentScope();
// Process the type parameters
this.visit(typeParameters);
// Process parameter declarations.
for (let i = 0; i < params.length; ++i) {
this.visitPattern(
params[i],
{ processRightHandNodes: true },
(pattern, info) => {
innerScope.__define(
pattern,
new TSESLintScope.ParameterDefinition(pattern, node, i, info.rest),
);
// Set `variable.eslintUsed` to tell ESLint that the variable is used.
const variable = innerScope.set.get(pattern.name);
if (variable) {
variable.eslintUsed = true;
}
this.referencingDefaultValue(pattern, info.assignments, null, true);
},
);
}
// Process the return type.
this.visit(returnType);
// Close the function scope.
this.close(node);
}
/**
* Create reference objects for the references in parameters and return type.
* @param node The TSEmptyBodyFunctionExpression node to visit.
*/
TSEmptyBodyFunctionExpression(
node: TSESTree.TSEmptyBodyFunctionExpression,
): void {
const upperTypeMode = this.typeMode;
const { typeParameters, params, returnType } = node;
this.typeMode = true;
this.visit(typeParameters);
params.forEach(this.visit, this);
this.visit(returnType);
this.typeMode = upperTypeMode;
}
/**
* Don't make variable because it declares only types.
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param node The TSInterfaceDeclaration node to visit.
*/
TSInterfaceDeclaration(node: TSESTree.TSInterfaceDeclaration): void {
this.visitTypeNodes(node);
}
/**
* Don't make variable because it declares only types.
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param node The TSClassImplements node to visit.
*/
TSClassImplements(node: TSESTree.TSClassImplements): void {
this.visitTypeNodes(node);
}
/**
* Don't make variable because it declares only types.
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param node The TSIndexSignature node to visit.
*/
TSIndexSignature(node: TSESTree.TSIndexSignature): void {
this.visitTypeNodes(node);
}
/**
* Visit type assertion.
* @param node The TSTypeAssertion node to visit.
*/
TSTypeAssertion(node: TSESTree.TSTypeAssertion): void {
if (this.typeMode) {
this.visit(node.typeAnnotation);
} else {
this.typeMode = true;
this.visit(node.typeAnnotation);
this.typeMode = false;
}
this.visit(node.expression);
}
/**
* Visit as expression.
* @param node The TSAsExpression node to visit.
*/
TSAsExpression(node: TSESTree.TSAsExpression): void {
this.visit(node.expression);
if (this.typeMode) {
this.visit(node.typeAnnotation);
} else {
this.typeMode = true;
this.visit(node.typeAnnotation);
this.typeMode = false;
}
}
/**
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param node The TSTypeAnnotation node to visit.
*/
TSTypeAnnotation(node: TSESTree.TSTypeAnnotation): void {
this.visitTypeNodes(node);
}
/**
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param node The TSTypeParameterDeclaration node to visit.
*/
TSTypeParameterDeclaration(node: TSESTree.TSTypeParameterDeclaration): void {
this.visitTypeNodes(node);
}
/**
* Create reference objects for the references in `typeof` expression.
* @param node The TSTypeQuery node to visit.
*/
TSTypeQuery(node: TSESTree.TSTypeQuery): void {
if (this.typeMode) {
this.typeMode = false;
this.visitChildren(node);
this.typeMode = true;
} else {
this.visitChildren(node);
}
}
/**
* @param node The TSTypeParameter node to visit.
*/
TSTypeParameter(node: TSESTree.TSTypeParameter): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSInferType node to visit.
*/
TSInferType(node: TSESTree.TSInferType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSTypeReference node to visit.
*/
TSTypeReference(node: TSESTree.TSTypeReference): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSTypeLiteral node to visit.
*/
TSTypeLiteral(node: TSESTree.TSTypeLiteral): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSLiteralType node to visit.
*/
TSLiteralType(node: TSESTree.TSLiteralType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSIntersectionType node to visit.
*/
TSIntersectionType(node: TSESTree.TSIntersectionType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSConditionalType node to visit.
*/
TSConditionalType(node: TSESTree.TSConditionalType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSIndexedAccessType node to visit.
*/
TSIndexedAccessType(node: TSESTree.TSIndexedAccessType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSMappedType node to visit.
*/
TSMappedType(node: TSESTree.TSMappedType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSOptionalType node to visit.
*/
TSOptionalType(node: TSESTree.TSOptionalType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSParenthesizedType node to visit.
*/
TSParenthesizedType(node: TSESTree.TSParenthesizedType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSRestType node to visit.
*/
TSRestType(node: TSESTree.TSRestType): void {
this.visitTypeNodes(node);
}
/**
* @param node The TSTupleType node to visit.
*/
TSTupleType(node: TSESTree.TSTupleType): void {
this.visitTypeNodes(node);
}
/**
* Create reference objects for the object part. (This is `obj.prop`)
* @param node The TSQualifiedName node to visit.
*/
TSQualifiedName(node: TSESTree.TSQualifiedName): void {
this.visit(node.left);
}
/**
* Create reference objects for the references in computed keys.
* @param node The TSPropertySignature node to visit.
*/
TSPropertySignature(node: TSESTree.TSPropertySignature): void {
const upperTypeMode = this.typeMode;
const { computed, key, typeAnnotation, initializer } = node;
if (computed) {
this.typeMode = false;
this.visit(key);
this.typeMode = true;
} else {
this.typeMode = true;
this.visit(key);
}
this.visit(typeAnnotation);
this.visit(initializer);
this.typeMode = upperTypeMode;
}
/**
* Create reference objects for the references in computed keys.
* @param node The TSMethodSignature node to visit.
*/
TSMethodSignature(node: TSESTree.TSMethodSignature): void {
const upperTypeMode = this.typeMode;
const { computed, key, typeParameters, params, returnType } = node;
if (computed) {
this.typeMode = false;
this.visit(key);
this.typeMode = true;
} else {
this.typeMode = true;
this.visit(key);
}
this.visit(typeParameters);
params.forEach(this.visit, this);
this.visit(returnType);
this.typeMode = upperTypeMode;
}
/**
* Create variable object for the enum.
* The enum declaration creates a scope for the enum members.
*
* enum E {
* A,
* B,
* C = A + B // A and B are references to the enum member.
* }
*
* const a = 0
* enum E {
* A = a // a is above constant.
* }
*
* @param node The TSEnumDeclaration node to visit.
*/
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void {
const { id, members } = node;
const scopeManager = this.scopeManager;
const scope = this.currentScope();
if (id) {
scope.__define(id, new TSESLintScope.Definition('EnumName', id, node));
}
scopeManager.__nestEnumScope(node);
for (const member of members) {
this.visit(member);
}
this.close(node);
}
/**
* Create variable object for the enum member and create reference object for the initializer.
* And visit the initializer.
*
* @param node The TSEnumMember node to visit.
*/
TSEnumMember(node: TSESTree.TSEnumMember): void {
const { id, initializer } = node;
const scope = this.currentScope();
scope.__define(
id,
new TSESLintScope.Definition('EnumMemberName', id, node),
);
if (initializer) {
scope.__referencing(
id,
TSESLintScope.Reference.WRITE,
initializer,
null,
false,
true,
);
this.visit(initializer);
}
}
/**
* Create the variable object for the module name, and visit children.
* @param node The TSModuleDeclaration node to visit.
*/
TSModuleDeclaration(node: TSESTree.TSModuleDeclaration): void {
const scope = this.currentScope();
const { id, body } = node;
if (node.global) {
this.visitGlobalAugmentation(node);
return;
}
if (id && id.type === AST_NODE_TYPES.Identifier) {
scope.__define(
id,
new TSESLintScope.Definition(
'NamespaceName',
id,
node,
null,
null,
null,
),
);
}
this.visit(body);
}
TSTypeAliasDeclaration(node: TSESTree.TSTypeAliasDeclaration): void {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}
/**
* Process the module block.
* @param node The TSModuleBlock node to visit.
*/
TSModuleBlock(node: TSESTree.TSModuleBlock): void {
this.scopeManager.__nestBlockScope(node);
this.visitChildren(node);
this.close(node);
}
TSAbstractClassProperty(node: TSESTree.TSAbstractClassProperty): void {
this.ClassProperty(node);
}
TSAbstractMethodDefinition(node: TSESTree.TSAbstractMethodDefinition): void {
this.MethodDefinition(node);
}
/**
* Process import equal declaration
* @param node The TSImportEqualsDeclaration node to visit.
*/
TSImportEqualsDeclaration(node: TSESTree.TSImportEqualsDeclaration): void {
const { id, moduleReference } = node;
if (id && id.type === AST_NODE_TYPES.Identifier) {
this.currentScope().__define(
id,
new TSESLintScope.Definition(
'ImportBinding',
id,
node,
null,
null,
null,
),
);
}
this.visit(moduleReference);
}
/**
* Process the global augmentation.
* 1. Set the global scope as the current scope.
* 2. Configure the global scope to set `variable.eslintUsed = true` for all defined variables. This means `no-unused-vars` doesn't warn those.
* @param node The TSModuleDeclaration node to visit.
*/
visitGlobalAugmentation(node: TSESTree.TSModuleDeclaration): void {
const scopeManager = this.scopeManager;
const currentScope = this.currentScope();
const globalScope = scopeManager.globalScope;
const originalDefine = globalScope.__define;
globalScope.__define = overrideDefine(originalDefine);
scopeManager.__currentScope = globalScope;
// Skip TSModuleBlock to avoid to create that block scope.
if (node.body && node.body.type === AST_NODE_TYPES.TSModuleBlock) {
node.body.body.forEach(this.visit, this);
}
scopeManager.__currentScope = currentScope;
globalScope.__define = originalDefine;
}
/**
* Process decorators.
* @param decorators The decorator nodes to visit.
*/
visitDecorators(decorators?: TSESTree.Decorator[]): void {
if (decorators) {
decorators.forEach(this.visit, this);
}
}
/**
* Process all child of type nodes
* @param node node to be processed
*/
visitTypeNodes(node: TSESTree.Node): void {
if (this.typeMode) {
this.visitChildren(node);
} else {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}
}
}
export function analyzeScope(
ast: TSESTree.Program,
parserOptions: ParserOptions,
): ScopeManager {
const options = {
ignoreEval: true,
optimistic: false,
directive: false,
nodejsScope:
parserOptions.sourceType === 'script' &&
parserOptions.ecmaFeatures?.globalReturn === true,
impliedStrict: false,
sourceType: parserOptions.sourceType,
ecmaVersion: parserOptions.ecmaVersion ?? 2018,
childVisitorKeys,
fallback,
};
const scopeManager = new ScopeManager(options);
const referencer = new Referencer(options, scopeManager);
referencer.visit(ast);
return scopeManager;
}