@@ -81,7 +81,7 @@ export default class typescriptToFileDatas {
8181 parentJson : null | AnyOption ,
8282 option : TSTypeAnnotationConfig ,
8383 ) : void {
84- const { node, parentKey, file } = option || { } ;
84+ const { node, parentKey, file, namespaces } = option || { } ;
8585
8686 const key = _ . get ( node , 'key.name' ) || _ . get ( node , 'key.value' ) || '' ;
8787 const required = ! node . optional ;
@@ -92,6 +92,7 @@ export default class typescriptToFileDatas {
9292 typeAnnotation : node . typeAnnotation ,
9393 file,
9494 attrKey : key ,
95+ namespaces,
9596 } ) ;
9697
9798 if ( typeAnnotation && typeof typeAnnotation === 'object' ) {
@@ -338,7 +339,7 @@ export default class typescriptToFileDatas {
338339 }
339340 const key = _ . get ( path , 'node.key.name' ) || _ . get ( path , 'node.key.value' ) ;
340341
341- _this . handleTSTypeAnnotation ( json , null , { node : path . node , file } ) ;
342+ _this . handleTSTypeAnnotation ( json , null , { node : path . node , file, namespaces , } ) ;
342343
343344 path . traverse ( {
344345 TSTypeLiteral ( path : AnyOption ) {
@@ -352,6 +353,7 @@ export default class typescriptToFileDatas {
352353 node : path . node ,
353354 parentKey : key ,
354355 file,
356+ namespaces,
355357 } ) ;
356358 } ,
357359 } ) ;
@@ -576,10 +578,12 @@ export default class typescriptToFileDatas {
576578 * @param {AnyOption } typeAnnotation
577579 * @returns {any } {string}
578580 */
579- getTypeName ( typeName : AnyOption ) : string {
581+ getTypeName ( typeName : AnyOption , namespaces ?: string [ ] ) : string {
580582 const name = _ . get ( typeName , 'name' ) ;
581-
582583 if ( name ) {
584+ if ( typeName . type === 'Identifier' && Array . isArray ( namespaces ) && namespaces . length ) {
585+ return `${ namespaces . join ( '.' ) } .${ name } `
586+ }
583587 return name ;
584588 }
585589 const { left, right } = typeName || { } ;
@@ -624,7 +628,7 @@ export default class typescriptToFileDatas {
624628 * @returns {any }
625629 */
626630 transformTypeAnnotation ( option : TypeAnnotationConfig ) : AnyOption | null {
627- const { typeAnnotation, file, attrKey, refKey } = option || { } ;
631+ const { typeAnnotation, file, attrKey, refKey, namespaces } = option || { } ;
628632 if ( ! typeAnnotation ) return null ;
629633 const cType = _ . get ( typeAnnotation , 'type' ) ;
630634 if ( ! cType ) return null ;
@@ -648,17 +652,16 @@ export default class typescriptToFileDatas {
648652
649653 // 处理 Number/自定义 等类型
650654 if ( cType === 'TSTypeReference' ) {
651- const name = this . getTypeName ( typeAnnotation . typeName ) ;
652-
655+ const name = this . getTypeName ( typeAnnotation . typeName , namespaces ) ;
653656 const type = this . handleRelationTypes ( name ) ;
654-
655657 let items = null ;
656658 if ( typeAnnotation . typeParameters ) {
657659 items = this . transformTypeAnnotation ( {
658660 typeAnnotation : typeAnnotation . typeParameters ,
659661 file,
660662 attrKey,
661663 refKey : name ,
664+ namespaces,
662665 } ) ;
663666 }
664667 // promise 特殊处理
@@ -698,7 +701,12 @@ export default class typescriptToFileDatas {
698701 if ( cType === 'TSTypeParameterInstantiation' ) {
699702 const params : ( AnyOption | null ) [ ] = [ ] ;
700703 typeAnnotation . params . forEach ( ( item : AnyOption ) => {
701- const res = this . transformTypeAnnotation ( { typeAnnotation : item , file, attrKey } ) ;
704+ const res = this . transformTypeAnnotation ( {
705+ typeAnnotation : item ,
706+ file,
707+ attrKey,
708+ namespaces,
709+ } ) ;
702710 if ( res ) {
703711 params . push ( res ) ;
704712 }
@@ -718,6 +726,7 @@ export default class typescriptToFileDatas {
718726 typeAnnotation : typeAnnotation . typeAnnotation ,
719727 file,
720728 attrKey,
729+ namespaces,
721730 } ) ;
722731 }
723732
@@ -727,6 +736,7 @@ export default class typescriptToFileDatas {
727736 typeAnnotation : typeAnnotation . elementType ,
728737 file,
729738 attrKey,
739+ namespaces,
730740 } ) ;
731741 return type ? { type : 'array' , items : type } : null ;
732742 }
@@ -737,6 +747,7 @@ export default class typescriptToFileDatas {
737747 typeAnnotation : typeAnnotation . typeAnnotation ,
738748 file,
739749 attrKey,
750+ namespaces,
740751 } ) ;
741752 return type ;
742753 }
@@ -745,7 +756,7 @@ export default class typescriptToFileDatas {
745756 const members = typeAnnotation . members || [ ] ;
746757 const json = { type : 'object' , properties : { } , required : [ ] } ;
747758 members . forEach ( ( item : AnyOption ) => {
748- this . handleTSTypeAnnotation ( json , null , { node : item , file } ) ;
759+ this . handleTSTypeAnnotation ( json , null , { node : item , file, namespaces , } ) ;
749760 } ) ;
750761
751762 this . formatJsonSchema ( json ) ;
0 commit comments