@@ -65,6 +65,13 @@ class PatternVisitor extends OriginalPatternVisitor {
65
65
this . rightHandNodes . push ( node . typeAnnotation ) ;
66
66
}
67
67
}
68
+
69
+ RestElement ( node ) {
70
+ super . RestElement ( node ) ;
71
+ if ( node . typeAnnotation ) {
72
+ this . rightHandNodes . push ( node . typeAnnotation ) ;
73
+ }
74
+ }
68
75
}
69
76
70
77
class Referencer extends OriginalReferencer {
@@ -120,7 +127,7 @@ class Referencer extends OriginalReferencer {
120
127
const { defs, identifiers } = upperScope . set . get ( id . name ) ;
121
128
for ( let i = 0 ; i < defs . length ; ++ i ) {
122
129
const def = defs [ i ] ;
123
- if ( def . type === "FunctionName" && def . node . type === "TSEmptyBodyFunctionDeclaration " ) {
130
+ if ( def . type === "FunctionName" && def . node . type === "TSDeclareFunction " ) {
124
131
defs . splice ( i , 1 ) ;
125
132
identifiers . splice ( i , 1 ) ;
126
133
break ;
@@ -239,28 +246,9 @@ class Referencer extends OriginalReferencer {
239
246
super . MethodDefinition ( node ) ;
240
247
}
241
248
242
- /**
243
- * Override.
244
- * Don't make variable if `kind === "type"`.
245
- * It doesn't declare variables but declare types.
246
- * @param {VariableDeclaration } node The VariableDeclaration node to visit.
247
- * @returns {void }
248
- */
249
- VariableDeclaration ( node ) {
250
- if ( node . kind !== "type" ) {
251
- super . VariableDeclaration ( node ) ;
252
- return ;
253
- }
254
-
255
- // To detect typeof.
256
- this . typeMode = true ;
257
- this . visitChildren ( node ) ;
258
- this . typeMode = false ;
259
- }
260
-
261
249
/**
262
250
* Don't create the reference object for the key if not computed.
263
- * @param {TSEmptyBodyFunctionDeclaration } node The TSEmptyBodyFunctionDeclaration node to visit.
251
+ * @param {ClassProperty } node The ClassProperty node to visit.
264
252
* @returns {void }
265
253
*/
266
254
ClassProperty ( node ) {
@@ -311,23 +299,25 @@ class Referencer extends OriginalReferencer {
311
299
/**
312
300
* Define the variable of this function declaration only once.
313
301
* Because to avoid confusion of `no-redeclare` rule by overloading.
314
- * @param {TSEmptyBodyFunctionDeclaration } node The TSEmptyBodyFunctionDeclaration node to visit.
302
+ * @param {TSDeclareFunction } node The TSDeclareFunction node to visit.
315
303
* @returns {void }
316
304
*/
317
- TSEmptyBodyFunctionDeclaration ( node ) {
305
+ TSDeclareFunction ( node ) {
318
306
const upperTypeMode = this . typeMode ;
319
307
const scope = this . currentScope ( ) ;
320
308
const { id, typeParameters, params, returnType } = node ;
321
309
322
310
// Ignore this if other overloadings have already existed.
323
- const variable = scope . set . get ( id . name ) ;
324
- const defs = variable && variable . defs ;
325
- const existed = defs && defs . some ( d => d . type === "FunctionName" ) ;
326
- if ( ! existed ) {
327
- scope . __define (
328
- id ,
329
- new Definition ( "FunctionName" , id , node , null , null , null )
330
- ) ;
311
+ if ( id ) {
312
+ const variable = scope . set . get ( id . name ) ;
313
+ const defs = variable && variable . defs ;
314
+ const existed = defs && defs . some ( d => d . type === "FunctionName" ) ;
315
+ if ( ! existed ) {
316
+ scope . __define (
317
+ id ,
318
+ new Definition ( "FunctionName" , id , node , null , null , null )
319
+ ) ;
320
+ }
331
321
}
332
322
333
323
// Find `typeof` expressions.
@@ -337,9 +327,6 @@ class Referencer extends OriginalReferencer {
337
327
this . visit ( returnType ) ;
338
328
this . typeMode = upperTypeMode ;
339
329
}
340
- TSEmptyBodyDeclareFunction ( node ) {
341
- this . TSEmptyBodyFunctionDeclaration ( node ) ;
342
- }
343
330
344
331
/**
345
332
* Create reference objects for the references in parameters and return type.
@@ -364,43 +351,79 @@ class Referencer extends OriginalReferencer {
364
351
* @returns {void }
365
352
*/
366
353
TSInterfaceDeclaration ( node ) {
354
+ this . visitTypeNodes ( node ) ;
355
+ }
356
+
357
+ /**
358
+ * Don't make variable because it declares only types.
359
+ * Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
360
+ * @param {TSClassImplements } node The TSClassImplements node to visit.
361
+ * @returns {void }
362
+ */
363
+ TSClassImplements ( node ) {
364
+ this . visitTypeNodes ( node ) ;
365
+ }
366
+
367
+ /**
368
+ * Don't make variable because it declares only types.
369
+ * Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
370
+ * @param {TSIndexSignature } node The TSIndexSignature node to visit.
371
+ * @returns {void }
372
+ */
373
+ TSIndexSignature ( node ) {
374
+ this . visitTypeNodes ( node ) ;
375
+ }
376
+
377
+ /**
378
+ * Visit type assertion.
379
+ * @param {TSTypeAssertion } node The TSTypeAssertion node to visit.
380
+ * @returns {void }
381
+ */
382
+ TSTypeAssertion ( node ) {
367
383
if ( this . typeMode ) {
368
- this . visitChildren ( node ) ;
384
+ this . visit ( node . typeAnnotation ) ;
369
385
} else {
370
386
this . typeMode = true ;
371
- this . visitChildren ( node ) ;
387
+ this . visit ( node . typeAnnotation ) ;
372
388
this . typeMode = false ;
373
389
}
390
+
391
+ this . visit ( node . expression ) ;
374
392
}
375
393
376
394
/**
377
- * Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations .
378
- * @param {TSTypeAnnotation } node The TSTypeAnnotation node to visit.
395
+ * Visit as expression.
396
+ * @param {TSAsExpression } node The TSAsExpression node to visit.
379
397
* @returns {void }
380
398
*/
381
- TSTypeAnnotation ( node ) {
399
+ TSAsExpression ( node ) {
400
+ this . visit ( node . expression ) ;
401
+
382
402
if ( this . typeMode ) {
383
- this . visitChildren ( node ) ;
403
+ this . visit ( node . typeAnnotation ) ;
384
404
} else {
385
405
this . typeMode = true ;
386
- this . visitChildren ( node ) ;
406
+ this . visit ( node . typeAnnotation ) ;
387
407
this . typeMode = false ;
388
408
}
389
409
}
390
410
411
+ /**
412
+ * Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
413
+ * @param {TSTypeAnnotation } node The TSTypeAnnotation node to visit.
414
+ * @returns {void }
415
+ */
416
+ TSTypeAnnotation ( node ) {
417
+ this . visitTypeNodes ( node ) ;
418
+ }
419
+
391
420
/**
392
421
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
393
422
* @param {TSTypeParameterDeclaration } node The TSTypeParameterDeclaration node to visit.
394
423
* @returns {void }
395
424
*/
396
425
TSTypeParameterDeclaration ( node ) {
397
- if ( this . typeMode ) {
398
- this . visitChildren ( node ) ;
399
- } else {
400
- this . typeMode = true ;
401
- this . visitChildren ( node ) ;
402
- this . typeMode = false ;
403
- }
426
+ this . visitTypeNodes ( node ) ;
404
427
}
405
428
406
429
/**
@@ -418,9 +441,113 @@ class Referencer extends OriginalReferencer {
418
441
}
419
442
}
420
443
444
+ /**
445
+ * @param {TSTypeParameter } node The TSTypeParameter node to visit.
446
+ * @returns {void }
447
+ */
448
+ TSTypeParameter ( node ) {
449
+ this . visitTypeNodes ( node ) ;
450
+ }
451
+
452
+ /**
453
+ * @param {TSInferType } node The TSInferType node to visit.
454
+ * @returns {void }
455
+ */
456
+ TSInferType ( node ) {
457
+ this . visitTypeNodes ( node ) ;
458
+ }
459
+
460
+ /**
461
+ * @param {TSTypeReference } node The TSTypeReference node to visit.
462
+ * @returns {void }
463
+ */
464
+ TSTypeReference ( node ) {
465
+ this . visitTypeNodes ( node ) ;
466
+ }
467
+
468
+ /**
469
+ * @param {TSTypeLiteral } node The TSTypeLiteral node to visit.
470
+ * @returns {void }
471
+ */
472
+ TSTypeLiteral ( node ) {
473
+ this . visitTypeNodes ( node ) ;
474
+ }
475
+
476
+ /**
477
+ * @param {TSLiteralType } node The TSLiteralType node to visit.
478
+ * @returns {void }
479
+ */
480
+ TSLiteralType ( node ) {
481
+ this . visitTypeNodes ( node ) ;
482
+ }
483
+
484
+ /**
485
+ * @param {TSIntersectionType } node The TSIntersectionType node to visit.
486
+ * @returns {void }
487
+ */
488
+ TSIntersectionType ( node ) {
489
+ this . visitTypeNodes ( node ) ;
490
+ }
491
+
492
+ /**
493
+ * @param {TSConditionalType } node The TSConditionalType node to visit.
494
+ * @returns {void }
495
+ */
496
+ TSConditionalType ( node ) {
497
+ this . visitTypeNodes ( node ) ;
498
+ }
499
+
500
+ /**
501
+ * @param {TSIndexedAccessType } node The TSIndexedAccessType node to visit.
502
+ * @returns {void }
503
+ */
504
+ TSIndexedAccessType ( node ) {
505
+ this . visitTypeNodes ( node ) ;
506
+ }
507
+
508
+ /**
509
+ * @param {TSMappedType } node The TSMappedType node to visit.
510
+ * @returns {void }
511
+ */
512
+ TSMappedType ( node ) {
513
+ this . visitTypeNodes ( node ) ;
514
+ }
515
+
516
+ /**
517
+ * @param {TSOptionalType } node The TSOptionalType node to visit.
518
+ * @returns {void }
519
+ */
520
+ TSOptionalType ( node ) {
521
+ this . visitTypeNodes ( node ) ;
522
+ }
523
+
524
+ /**
525
+ * @param {TSParenthesizedType } node The TSParenthesizedType node to visit.
526
+ * @returns {void }
527
+ */
528
+ TSParenthesizedType ( node ) {
529
+ this . visitTypeNodes ( node ) ;
530
+ }
531
+
532
+ /**
533
+ * @param {TSRestType } node The TSRestType node to visit.
534
+ * @returns {void }
535
+ */
536
+ TSRestType ( node ) {
537
+ this . visitTypeNodes ( node ) ;
538
+ }
539
+
540
+ /**
541
+ * @param {TSTupleType } node The TSTupleType node to visit.
542
+ * @returns {void }
543
+ */
544
+ TSTupleType ( node ) {
545
+ this . visitTypeNodes ( node ) ;
546
+ }
547
+
421
548
/**
422
549
* Create reference objects for the object part. (This is `obj.prop`)
423
- * @param {TSTypeQuery } node The TSTypeQuery node to visit.
550
+ * @param {TSQualifiedName } node The TSQualifiedName node to visit.
424
551
* @returns {void }
425
552
*/
426
553
TSQualifiedName ( node ) {
@@ -457,7 +584,7 @@ class Referencer extends OriginalReferencer {
457
584
*/
458
585
TSMethodSignature ( node ) {
459
586
const upperTypeMode = this . typeMode ;
460
- const { computed, key, typeParameters, params, typeAnnotation } = node ;
587
+ const { computed, key, typeParameters, params, returnType } = node ;
461
588
462
589
if ( computed ) {
463
590
this . typeMode = false ;
@@ -469,7 +596,7 @@ class Referencer extends OriginalReferencer {
469
596
}
470
597
this . visit ( typeParameters ) ;
471
598
params . forEach ( this . visit , this ) ;
472
- this . visit ( typeAnnotation ) ; // Maybe returnType?
599
+ this . visit ( returnType ) ;
473
600
474
601
this . typeMode = upperTypeMode ;
475
602
}
@@ -556,6 +683,12 @@ class Referencer extends OriginalReferencer {
556
683
this . visit ( body ) ;
557
684
}
558
685
686
+ TSTypeAliasDeclaration ( node ) {
687
+ this . typeMode = true ;
688
+ this . visitChildren ( node ) ;
689
+ this . typeMode = false ;
690
+ }
691
+
559
692
/**
560
693
* Process the module block.
561
694
* @param {TSModuleBlock } node The TSModuleBlock node to visit.
@@ -583,11 +716,11 @@ class Referencer extends OriginalReferencer {
583
716
* @returns {void }
584
717
*/
585
718
TSImportEqualsDeclaration ( node ) {
586
- const { name , moduleReference } = node ;
587
- if ( name && name . type === "Identifier" ) {
719
+ const { id , moduleReference } = node ;
720
+ if ( id && id . type === "Identifier" ) {
588
721
this . currentScope ( ) . __define (
589
- name ,
590
- new Definition ( "ImportBinding" , name , node , null , null , null )
722
+ id ,
723
+ new Definition ( "ImportBinding" , id , node , null , null , null )
591
724
) ;
592
725
}
593
726
this . visit ( moduleReference ) ;
@@ -628,6 +761,21 @@ class Referencer extends OriginalReferencer {
628
761
decorators . forEach ( this . visit , this ) ;
629
762
}
630
763
}
764
+
765
+ /**
766
+ * Process all child of type nodes
767
+ * @param {any } node node to be processed
768
+ * @returns {void }
769
+ */
770
+ visitTypeNodes ( node ) {
771
+ if ( this . typeMode ) {
772
+ this . visitChildren ( node ) ;
773
+ } else {
774
+ this . typeMode = true ;
775
+ this . visitChildren ( node ) ;
776
+ this . typeMode = false ;
777
+ }
778
+ }
631
779
}
632
780
633
781
module . exports = function ( ast , parserOptions , extraOptions ) {
0 commit comments