@@ -30,9 +30,80 @@ function getIndexClosedBracketTypeParam(line) {
3030 return end ;
3131}
3232
33- function getTypeParam ( line ) {
33+ /**
34+ *
35+ * @param {string } string
36+ * @returns {string[] }
37+ */
38+ function splitType ( string ) {
39+ string = string . split ( ',' ) ;
40+ let newCode = [ ] ;
41+ let last = "" ;
42+ for ( let part of string ) {
43+ if ( last . split ( "(" ) . length == last . split ( ")" ) . length && last . split ( "[" ) . length == last . split ( "]" ) . length && last . split ( "{" ) . length == last . split ( "}" ) . length ) {
44+ last = part ;
45+ newCode . push ( part ) ;
46+ }
47+ else {
48+ last += "," + part ;
49+ newCode [ newCode . length - 1 ] = last ;
50+ }
51+ }
52+ return newCode ;
53+ }
54+
55+ /**
56+ * @param {string } string
57+ */
58+ function parseType ( string , tab = 0 ) {
59+ const typeArray = [ ] ;
60+ let currTab = tab ;
61+ if ( string . startsWith ( "[" ) ) {
62+ typeArray . push ( Array ( currTab * 4 ) . fill ( true ) . map ( ( ) => " " ) . join ( "" ) + "- __Array__: " ) ;
63+ currTab ++ ;
64+ string = string . substring ( 1 , string . length - 1 ) ;
65+ const stringSplitted = splitType ( string ) . map ( el => el . trim ( ) ) ;
66+ for ( let val of stringSplitted ) {
67+ if ( val . startsWith ( "[" ) || val . startsWith ( "{" ) ) {
68+ typeArray . push ( ...parseType ( val , currTab ) ) ;
69+ }
70+ else {
71+ typeArray . push ( Array ( currTab * 4 ) . fill ( true ) . map ( ( ) => " " ) . join ( "" ) + `- _${ val } _ ` )
72+ }
73+ }
74+ } else if ( string . startsWith ( "{" ) ) {
75+ typeArray . push ( Array ( currTab * 4 ) . fill ( true ) . map ( ( ) => " " ) . join ( "" ) + "- __Object__: " ) ;
76+ currTab ++ ;
77+ string = string . substring ( 1 , string . length - 1 ) ;
78+ const stringSplitted = splitType ( string ) . map ( el => el . trim ( ) ) ;
79+ for ( let val of stringSplitted ) {
80+ const semicolonIndex = val . indexOf ( ":" ) ;
81+ const [ key , value ] = [ val . substring ( 0 , semicolonIndex ) . trim ( ) , val . substring ( semicolonIndex + 1 , val . length ) . trim ( ) ] ;
82+ if ( value . startsWith ( "[" ) || value . startsWith ( "{" ) ) {
83+ typeArray . push ( ...parseType ( val , currTab ) ) ;
84+ } else {
85+ typeArray . push ( Array ( currTab * 4 ) . fill ( true ) . map ( ( ) => " " ) . join ( "" ) + `- ___${ key } __ : _${ value } _ ` ) ;
86+ }
87+ }
88+ } else {
89+ const stringSplitted = splitType ( string ) . map ( el => el . trim ( ) ) ;
90+ for ( let val of stringSplitted ) {
91+ if ( val . startsWith ( "[" ) || val . startsWith ( "{" ) ) {
92+ typeArray . push ( ...parseType ( val , currTab ) ) ;
93+ } else {
94+ typeArray . push ( Array ( currTab * 4 ) . fill ( true ) . map ( ( ) => " " ) . join ( "" ) + `- _${ val } _ ` ) ;
95+ }
96+ }
97+ }
98+ return typeArray ;
99+ }
100+
101+ function getTypeString ( line , isReturn ) {
34102 const end = getIndexClosedBracketTypeParam ( line ) ;
35- return line . substring ( 1 , end ) ;
103+ let type = line . substring ( 1 , end ) ;
104+ return isReturn
105+ ? parseType ( type )
106+ : type ;
36107}
37108
38109function getNameDescriptionParam ( line ) {
@@ -95,7 +166,7 @@ function getJsDoc(fileSplitted) {
95166
96167/**
97168 *
98- * @param {{title: string, description: string, usage?: string, params: [{name: string, description: string, type: string}], returns: string, type: string} } jsDoc
169+ * @param {{title: string, description: string, usage?: string, params: [{name: string, description: string, type: string}], returns: {name: string, description: string, type: string[]} , type: string} } jsDoc
99170 */
100171function jsDoc2Markdown ( jsDoc ) {
101172 const markdown =
@@ -115,7 +186,8 @@ ${jsDoc.params.map(el => `> - __${el.name}__: _${el.type}_${el.description ? '
115186
116187> ### Returns
117188>
118- > - ${ jsDoc . returns . name ? `__${ jsDoc . returns . name } __: ` : "" } _${ jsDoc . returns . type } _${ jsDoc . returns . description ? ' \n' + jsDoc . returns . description : "" }
189+ > ${ jsDoc . returns . name ? `__${ jsDoc . returns . name } __` : "" } ${ jsDoc . returns . description ? ': ' + jsDoc . returns . description : "" }
190+ ${ jsDoc . returns . type . map ( el => `> ` + el ) . join ( "\n" ) }
119191>` ;
120192
121193 return markdown ;
@@ -124,7 +196,7 @@ ${jsDoc.params.map(el => `> - __${el.name}__: _${el.type}_${el.description ? '
124196/**
125197 *
126198 * @param {string } file
127- * @returns {{title: string, description: string, params: [{name: string, description: string, type: string}], returns: string, type: string} }
199+ * @returns {{title: string, description: string, params: [{name: string, description: string, type: string}], returns: {name: string, description: string, type: string} , type: string} }
128200 */
129201function buildHooksUtilsMarkdownObject ( file ) {
130202 const fileSplitted = file . split ( "\n" ) ;
@@ -148,15 +220,15 @@ function buildHooksUtilsMarkdownObject(file) {
148220 if ( depuratedLine . startsWith ( "@param" ) ) {
149221 const typeNameDescriptionParam = depuratedLine . split ( "@param " ) [ 1 ] ;
150222 const param = {
151- type : getTypeParam ( typeNameDescriptionParam ) ,
223+ type : getTypeString ( typeNameDescriptionParam , false ) ,
152224 ...getNameDescriptionParam ( typeNameDescriptionParam )
153225 } ;
154226 obj . params . push ( param ) ;
155227 }
156228 if ( depuratedLine . startsWith ( "@returns" ) ) {
157229 const returnSplitted = depuratedLine . split ( "@returns " ) [ 1 ] ;
158230 const returns = {
159- type : getTypeParam ( returnSplitted ) ,
231+ type : getTypeString ( returnSplitted , true ) ,
160232 ...getNameDescriptionParam ( returnSplitted )
161233 } ;
162234 obj . returns = returns ;
0 commit comments