@@ -65,6 +65,13 @@ class PatternVisitor extends OriginalPatternVisitor {
6565 this . rightHandNodes . push ( node . typeAnnotation ) ;
6666 }
6767 }
68+
69+ RestElement ( node ) {
70+ super . RestElement ( node ) ;
71+ if ( node . typeAnnotation ) {
72+ this . rightHandNodes . push ( node . typeAnnotation ) ;
73+ }
74+ }
6875}
6976
7077class Referencer extends OriginalReferencer {
@@ -120,7 +127,7 @@ class Referencer extends OriginalReferencer {
120127 const { defs, identifiers } = upperScope . set . get ( id . name ) ;
121128 for ( let i = 0 ; i < defs . length ; ++ i ) {
122129 const def = defs [ i ] ;
123- if ( def . type === "FunctionName" && def . node . type === "TSEmptyBodyFunctionDeclaration " ) {
130+ if ( def . type === "FunctionName" && def . node . type === "TSDeclareFunction " ) {
124131 defs . splice ( i , 1 ) ;
125132 identifiers . splice ( i , 1 ) ;
126133 break ;
@@ -239,28 +246,9 @@ class Referencer extends OriginalReferencer {
239246 super . MethodDefinition ( node ) ;
240247 }
241248
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-
261249 /**
262250 * 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.
264252 * @returns {void }
265253 */
266254 ClassProperty ( node ) {
@@ -311,23 +299,25 @@ class Referencer extends OriginalReferencer {
311299 /**
312300 * Define the variable of this function declaration only once.
313301 * 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.
315303 * @returns {void }
316304 */
317- TSEmptyBodyFunctionDeclaration ( node ) {
305+ TSDeclareFunction ( node ) {
318306 const upperTypeMode = this . typeMode ;
319307 const scope = this . currentScope ( ) ;
320308 const { id, typeParameters, params, returnType } = node ;
321309
322310 // 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+ }
331321 }
332322
333323 // Find `typeof` expressions.
@@ -337,9 +327,6 @@ class Referencer extends OriginalReferencer {
337327 this . visit ( returnType ) ;
338328 this . typeMode = upperTypeMode ;
339329 }
340- TSEmptyBodyDeclareFunction ( node ) {
341- this . TSEmptyBodyFunctionDeclaration ( node ) ;
342- }
343330
344331 /**
345332 * Create reference objects for the references in parameters and return type.
@@ -364,43 +351,79 @@ class Referencer extends OriginalReferencer {
364351 * @returns {void }
365352 */
366353 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 ) {
367383 if ( this . typeMode ) {
368- this . visitChildren ( node ) ;
384+ this . visit ( node . typeAnnotation ) ;
369385 } else {
370386 this . typeMode = true ;
371- this . visitChildren ( node ) ;
387+ this . visit ( node . typeAnnotation ) ;
372388 this . typeMode = false ;
373389 }
390+
391+ this . visit ( node . expression ) ;
374392 }
375393
376394 /**
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.
379397 * @returns {void }
380398 */
381- TSTypeAnnotation ( node ) {
399+ TSAsExpression ( node ) {
400+ this . visit ( node . expression ) ;
401+
382402 if ( this . typeMode ) {
383- this . visitChildren ( node ) ;
403+ this . visit ( node . typeAnnotation ) ;
384404 } else {
385405 this . typeMode = true ;
386- this . visitChildren ( node ) ;
406+ this . visit ( node . typeAnnotation ) ;
387407 this . typeMode = false ;
388408 }
389409 }
390410
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+
391420 /**
392421 * Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
393422 * @param {TSTypeParameterDeclaration } node The TSTypeParameterDeclaration node to visit.
394423 * @returns {void }
395424 */
396425 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 ) ;
404427 }
405428
406429 /**
@@ -418,9 +441,113 @@ class Referencer extends OriginalReferencer {
418441 }
419442 }
420443
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+
421548 /**
422549 * 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.
424551 * @returns {void }
425552 */
426553 TSQualifiedName ( node ) {
@@ -457,7 +584,7 @@ class Referencer extends OriginalReferencer {
457584 */
458585 TSMethodSignature ( node ) {
459586 const upperTypeMode = this . typeMode ;
460- const { computed, key, typeParameters, params, typeAnnotation } = node ;
587+ const { computed, key, typeParameters, params, returnType } = node ;
461588
462589 if ( computed ) {
463590 this . typeMode = false ;
@@ -469,7 +596,7 @@ class Referencer extends OriginalReferencer {
469596 }
470597 this . visit ( typeParameters ) ;
471598 params . forEach ( this . visit , this ) ;
472- this . visit ( typeAnnotation ) ; // Maybe returnType?
599+ this . visit ( returnType ) ;
473600
474601 this . typeMode = upperTypeMode ;
475602 }
@@ -556,6 +683,12 @@ class Referencer extends OriginalReferencer {
556683 this . visit ( body ) ;
557684 }
558685
686+ TSTypeAliasDeclaration ( node ) {
687+ this . typeMode = true ;
688+ this . visitChildren ( node ) ;
689+ this . typeMode = false ;
690+ }
691+
559692 /**
560693 * Process the module block.
561694 * @param {TSModuleBlock } node The TSModuleBlock node to visit.
@@ -583,11 +716,11 @@ class Referencer extends OriginalReferencer {
583716 * @returns {void }
584717 */
585718 TSImportEqualsDeclaration ( node ) {
586- const { name , moduleReference } = node ;
587- if ( name && name . type === "Identifier" ) {
719+ const { id , moduleReference } = node ;
720+ if ( id && id . type === "Identifier" ) {
588721 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 )
591724 ) ;
592725 }
593726 this . visit ( moduleReference ) ;
@@ -628,6 +761,21 @@ class Referencer extends OriginalReferencer {
628761 decorators . forEach ( this . visit , this ) ;
629762 }
630763 }
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+ }
631779}
632780
633781module . exports = function ( ast , parserOptions , extraOptions ) {
0 commit comments