@@ -675,35 +675,58 @@ export class JSDocTsdParser {
675
675
}
676
676
677
677
private mapVariableType ( variableType : string ) {
678
- let matches = variableType . match ( / (?: A r r a y \. < ( [ ^ > ] + ) > ) | (?: ( [ ^ \[ ] * ) \[ \] ) / i) ;
679
-
680
- if ( matches ) {
681
- let type = matches [ 1 ] || matches [ 2 ] ;
682
-
683
- if ( type === "*" || type === "" ) {
684
- // wrong type definition
685
- return dom . type . any ;
678
+ // resolve array types
679
+ // jsdoc will provide arrays always as "Array.<>" if it's typed or as "Array" if it's not typed
680
+ let resultType : dom . Type = dom . type . any ;
681
+ while ( / ^ A r r a y / i. test ( variableType ) ) {
682
+ // it's an array, check if it's typed
683
+ let arrayTypeMatches = variableType . match ( / A r r a y \. < ( \( ? [ \w | ] + \) ? ) > / i) ; // @todo : can contain namepaths
684
+ if ( arrayTypeMatches && ! ! arrayTypeMatches [ 1 ] ) {
685
+ const arrayTypeString : string = arrayTypeMatches [ 1 ] ;
686
+ const arrayType = ( arrayTypeString . toLowerCase ( ) === "array" ) ? dom . type . array ( dom . type . any ) : this . mapVariableTypeString ( arrayTypeString )
687
+ resultType = ( resultType === dom . type . any )
688
+ ? dom . type . array ( arrayType )
689
+ : dom . type . array ( resultType ) ; // nested array
690
+
691
+ // remove the string from the variable type (nested arrays)
692
+ const regExp = new RegExp ( `Array.<${ arrayTypeString . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) } >` , "i" ) ;
693
+ variableType = variableType . replace ( regExp , "" ) ;
686
694
} else {
687
- if ( type === "bool" ) {
688
- type = "boolean" ;
689
- }
695
+ resultType = dom . type . array ( resultType ) ;
690
696
691
- return dom . type . array ( type as dom . Type ) ;
697
+ // remove the array keyword
698
+ variableType = variableType . replace ( / ^ A r r a y ( \. < ) ? / i, "" ) ;
692
699
}
693
- } else {
694
- if ( variableType . match ( / a r r a y / i) || variableType === "*" ) {
695
- return dom . type . any ;
696
- } else {
697
- if ( variableType === "bool" ) {
698
- variableType = "boolean" ;
699
- }
700
+ }
700
701
701
- if ( variableType === "function" ) {
702
- variableType = "Function" ;
703
- }
704
- return variableType as dom . Type ;
705
- }
702
+ if ( resultType === dom . type . any ) {
703
+ resultType = this . mapVariableTypeString ( variableType ) ;
704
+ }
705
+
706
+ return resultType ;
707
+ }
708
+
709
+ private mapVariableTypeString ( variableType : string ) : dom . Type {
710
+ if ( variableType === "bool" ) {
711
+ variableType = "boolean" ;
712
+ }
713
+
714
+ if ( variableType === "function" ) {
715
+ variableType = "Function" ;
706
716
}
717
+
718
+ if ( variableType === "*" ) {
719
+ variableType = "any" ;
720
+ }
721
+
722
+ // check if it's a union type
723
+ let resultType : dom . Type = variableType as dom . Type ;
724
+ if ( variableType . indexOf ( "|" ) > - 1 ) {
725
+ variableType = variableType . replace ( / \( | \) / g, "" ) ;
726
+ resultType = this . mapTypesToUnion ( variableType . split ( "|" ) ) ;
727
+ }
728
+
729
+ return resultType ;
707
730
}
708
731
709
732
private parseClass ( jsdocItem : IClassDoclet , domClass ?: dom . ClassDeclaration ) : dom . DeclarationBase {
0 commit comments