This repository has been archived by the owner on Jan 19, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
analyze-scope.js
803 lines (709 loc) · 22.6 KB
/
analyze-scope.js
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
"use strict";
/* eslint-disable new-cap, no-underscore-dangle */
const escope = require("eslint-scope");
const { Definition, ParameterDefinition } = require("eslint-scope/lib/definition");
const OriginalPatternVisitor = require("eslint-scope/lib/pattern-visitor");
const Reference = require("eslint-scope/lib/reference");
const OriginalReferencer = require("eslint-scope/lib/referencer");
const Scope = require("eslint-scope/lib/scope").Scope;
const fallback = require("eslint-visitor-keys").getKeys;
const childVisitorKeys = require("./visitor-keys");
/**
* 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) {
return /* @this {Scope} */ function(node, definition) {
define.call(this, node, definition);
// Set `variable.eslintUsed` to tell ESLint that the variable is exported.
const variable = this.set.get(node.name);
if (variable) {
variable.eslintUsed = true;
}
};
}
/** The scope class for enum. */
class EnumScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "enum", upperScope, block, false);
}
}
class PatternVisitor extends OriginalPatternVisitor {
Identifier(node) {
super.Identifier(node);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
ArrayPattern(node) {
node.elements.forEach(this.visit, this);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
ObjectPattern(node) {
node.properties.forEach(this.visit, this);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
RestElement(node) {
super.RestElement(node);
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}
}
class Referencer extends OriginalReferencer {
constructor(...args) {
super(...args);
this.typeMode = false;
}
/**
* Override to use PatternVisitor we overrode.
* @param {Identifier} node The Identifier node to visit.
* @param {Object} [options] The flag to visit right-hand side nodes.
* @param {Function} callback The callback function for left-hand side nodes.
* @returns {void}
*/
visitPattern(node, options, callback) {
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 {FunctionDeclaration|FunctionExpression|ArrowFunctionExpression} node The function node to visit.
* @returns {void}
*/
visitFunction(node) {
const { type, id, typeParameters, params, returnType, body } = node;
const scopeManager = this.scopeManager;
const upperScope = this.currentScope();
// Process the name.
if (type === "FunctionDeclaration" && id) {
upperScope.__define(
id,
new 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 === "TSDeclareFunction") {
defs.splice(i, 1);
identifiers.splice(i, 1);
break;
}
}
} else if (type === "FunctionExpression" && id) {
scopeManager.__nestFunctionExpressionNameScope(node);
}
// Process the type parameters
this.visit(typeParameters);
// Open the function scope.
scopeManager.__nestFunctionScope(node, this.isInnerMethodDefinition);
const innerScope = this.currentScope();
// Process parameter declarations.
for (let i = 0; i < params.length; ++i) {
this.visitPattern(
params[i],
{ processRightHandNodes: true },
(pattern, info) => {
innerScope.__define(
pattern,
new 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.type === "BlockStatement") {
this.visitChildren(body);
} else {
this.visit(body);
}
// Close the function scope.
this.close(node);
}
/**
* Override.
* Visit decorators.
* @param {ClassDeclaration|ClassExpression|TSAbstractClassDeclaration} node The class node to visit.
* @returns {void}
*/
visitClass(node) {
this.visitDecorators(node.decorators);
const upperTypeMode = this.typeMode;
this.typeMode = true;
if (node.superTypeParameters) {
this.visit(node.superTypeParameters);
}
if (node.implements) {
this.visit(node.implements);
}
this.typeMode = upperTypeMode;
super.visitClass(node);
}
/**
* Visit typeParameters.
* @param {*} node The node to visit.
* @returns {void}
*/
visitTypeParameters(node) {
if (node.typeParameters) {
const upperTypeMode = this.typeMode;
this.typeMode = true;
this.visit(node.typeParameters);
this.typeMode = upperTypeMode;
}
}
/**
* Override.
* Don't create the reference object in the type mode.
* @param {Identifier} node The Identifier node to visit.
* @returns {void}
*/
Identifier(node) {
this.visitDecorators(node.decorators);
if (!this.typeMode) {
super.Identifier(node);
}
this.visit(node.typeAnnotation);
}
/**
* Override.
* Visit decorators.
* @param {MethodDefinition} node The MethodDefinition node to visit.
* @returns {void}
*/
MethodDefinition(node) {
this.visitDecorators(node.decorators);
super.MethodDefinition(node);
}
/**
* Don't create the reference object for the key if not computed.
* @param {ClassProperty} node The ClassProperty node to visit.
* @returns {void}
*/
ClassProperty(node) {
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 {NewExpression} node The NewExpression node to visit.
* @returns {void}
*/
NewExpression(node) {
this.visitTypeParameters(node);
this.visit(node.callee);
if (node.arguments) {
node.arguments.forEach(this.visit, this);
}
}
/**
* Override.
* Visit call expression.
* @param {CallExpression} node The CallExpression node to visit.
* @returns {void}
*/
CallExpression(node) {
this.visitTypeParameters(node);
this.visit(node.callee);
if (node.arguments) {
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 {TSDeclareFunction} node The TSDeclareFunction node to visit.
* @returns {void}
*/
TSDeclareFunction(node) {
const upperTypeMode = this.typeMode;
const scope = this.currentScope();
const { id, typeParameters, params, returnType } = node;
// Ignore this if other overloadings have already existed.
if (id) {
const variable = scope.set.get(id.name);
const defs = variable && variable.defs;
const existed = defs && defs.some(d => d.type === "FunctionName");
if (!existed) {
scope.__define(
id,
new Definition("FunctionName", id, node, null, null, null)
);
}
}
// Find `typeof` expressions.
this.typeMode = true;
this.visit(typeParameters);
params.forEach(this.visit, this);
this.visit(returnType);
this.typeMode = upperTypeMode;
}
/**
* Create reference objects for the references in parameters and return type.
* @param {TSEmptyBodyFunctionExpression} node The TSEmptyBodyFunctionExpression node to visit.
* @returns {void}
*/
TSEmptyBodyFunctionExpression(node) {
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 {TSInterfaceDeclaration} node The TSInterfaceDeclaration node to visit.
* @returns {void}
*/
TSInterfaceDeclaration(node) {
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 {TSClassImplements} node The TSClassImplements node to visit.
* @returns {void}
*/
TSClassImplements(node) {
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 {TSIndexSignature} node The TSIndexSignature node to visit.
* @returns {void}
*/
TSIndexSignature(node) {
this.visitTypeNodes(node);
}
/**
* Visit type assertion.
* @param {TSTypeAssertion} node The TSTypeAssertion node to visit.
* @returns {void}
*/
TSTypeAssertion(node) {
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 {TSAsExpression} node The TSAsExpression node to visit.
* @returns {void}
*/
TSAsExpression(node) {
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 {TSTypeAnnotation} node The TSTypeAnnotation node to visit.
* @returns {void}
*/
TSTypeAnnotation(node) {
this.visitTypeNodes(node);
}
/**
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param {TSTypeParameterDeclaration} node The TSTypeParameterDeclaration node to visit.
* @returns {void}
*/
TSTypeParameterDeclaration(node) {
this.visitTypeNodes(node);
}
/**
* Create reference objects for the references in `typeof` expression.
* @param {TSTypeQuery} node The TSTypeQuery node to visit.
* @returns {void}
*/
TSTypeQuery(node) {
if (this.typeMode) {
this.typeMode = false;
this.visitChildren(node);
this.typeMode = true;
} else {
this.visitChildren(node);
}
}
/**
* @param {TSTypeParameter} node The TSTypeParameter node to visit.
* @returns {void}
*/
TSTypeParameter(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSInferType} node The TSInferType node to visit.
* @returns {void}
*/
TSInferType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSTypeReference} node The TSTypeReference node to visit.
* @returns {void}
*/
TSTypeReference(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSTypeLiteral} node The TSTypeLiteral node to visit.
* @returns {void}
*/
TSTypeLiteral(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSLiteralType} node The TSLiteralType node to visit.
* @returns {void}
*/
TSLiteralType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSIntersectionType} node The TSIntersectionType node to visit.
* @returns {void}
*/
TSIntersectionType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSConditionalType} node The TSConditionalType node to visit.
* @returns {void}
*/
TSConditionalType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSIndexedAccessType} node The TSIndexedAccessType node to visit.
* @returns {void}
*/
TSIndexedAccessType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSMappedType} node The TSMappedType node to visit.
* @returns {void}
*/
TSMappedType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSOptionalType} node The TSOptionalType node to visit.
* @returns {void}
*/
TSOptionalType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSParenthesizedType} node The TSParenthesizedType node to visit.
* @returns {void}
*/
TSParenthesizedType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSRestType} node The TSRestType node to visit.
* @returns {void}
*/
TSRestType(node) {
this.visitTypeNodes(node);
}
/**
* @param {TSTupleType} node The TSTupleType node to visit.
* @returns {void}
*/
TSTupleType(node) {
this.visitTypeNodes(node);
}
/**
* Create reference objects for the object part. (This is `obj.prop`)
* @param {TSQualifiedName} node The TSQualifiedName node to visit.
* @returns {void}
*/
TSQualifiedName(node) {
this.visit(node.left);
}
/**
* Create reference objects for the references in computed keys.
* @param {TSPropertySignature} node The TSPropertySignature node to visit.
* @returns {void}
*/
TSPropertySignature(node) {
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 {TSMethodSignature} node The TSMethodSignature node to visit.
* @returns {void}
*/
TSMethodSignature(node) {
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 {TSEnumDeclaration} node The TSEnumDeclaration node to visit.
* @returns {void}
*/
TSEnumDeclaration(node) {
const { id, members } = node;
const scopeManager = this.scopeManager;
const scope = this.currentScope();
if (id) {
scope.__define(id, new Definition("EnumName", id, node));
}
scopeManager.__nestScope(new EnumScope(scopeManager, scope, 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 {TSEnumMember} node The TSEnumMember node to visit.
* @returns {void}
*/
TSEnumMember(node) {
const { id, initializer } = node;
const scope = this.currentScope();
scope.__define(id, new Definition("EnumMemberName", id, node));
if (initializer) {
scope.__referencing(
id,
Reference.WRITE,
initializer,
null,
false,
true
);
this.visit(initializer);
}
}
/**
* Create the variable object for the module name, and visit children.
* @param {TSModuleDeclaration} node The TSModuleDeclaration node to visit.
* @returns {void}
*/
TSModuleDeclaration(node) {
const scope = this.currentScope();
const { id, body } = node;
if (node.global) {
this.visitGlobalAugmentation(node);
return;
}
if (id && id.type === "Identifier") {
scope.__define(
id,
new Definition("NamespaceName", id, node, null, null, null)
);
}
this.visit(body);
}
TSTypeAliasDeclaration(node) {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}
/**
* Process the module block.
* @param {TSModuleBlock} node The TSModuleBlock node to visit.
* @returns {void}
*/
TSModuleBlock(node) {
this.scopeManager.__nestBlockScope(node);
this.visitChildren(node);
this.close(node);
}
TSAbstractClassDeclaration(node) {
this.ClassDeclaration(node);
}
TSAbstractClassProperty(node) {
this.ClassProperty(node);
}
TSAbstractMethodDefinition(node) {
this.MethodDefinition(node);
}
/**
* Process import equal declaration
* @param {TSImportEqualsDeclaration} node The TSImportEqualsDeclaration node to visit.
* @returns {void}
*/
TSImportEqualsDeclaration(node) {
const { id, moduleReference } = node;
if (id && id.type === "Identifier") {
this.currentScope().__define(
id,
new 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 {TSModuleDeclaration} node The TSModuleDeclaration node to visit.
* @returns {void}
*/
visitGlobalAugmentation(node) {
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.
for (const moduleItem of node.body.body) {
this.visit(moduleItem);
}
scopeManager.__currentScope = currentScope;
globalScope.__define = originalDefine;
}
/**
* Process decorators.
* @param {Decorator[]|undefined} decorators The decorator nodes to visit.
* @returns {void}
*/
visitDecorators(decorators) {
if (decorators) {
decorators.forEach(this.visit, this);
}
}
/**
* Process all child of type nodes
* @param {any} node node to be processed
* @returns {void}
*/
visitTypeNodes(node) {
if (this.typeMode) {
this.visitChildren(node);
} else {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}
}
}
module.exports = function(ast, parserOptions) {
const options = {
ignoreEval: true,
optimistic: false,
directive: false,
nodejsScope:
parserOptions.sourceType === "script" &&
(parserOptions.ecmaFeatures &&
parserOptions.ecmaFeatures.globalReturn) === true,
impliedStrict: false,
sourceType: parserOptions.sourceType,
ecmaVersion: parserOptions.ecmaVersion || 2018,
childVisitorKeys,
fallback
};
const scopeManager = new escope.ScopeManager(options);
const referencer = new Referencer(options, scopeManager);
referencer.visit(ast);
return scopeManager;
};