1
- type UnknownObject = Record < string , unknown > ;
2
- type StringObject = Record < string , string > ;
1
+ import {
2
+ DeclarationReflection ,
3
+ SomeType ,
4
+ LiteralType ,
5
+ IntrinsicType ,
6
+ ReflectionType ,
7
+ ReflectionKind ,
8
+ ReferenceType ,
9
+ } from 'typedoc' ;
3
10
4
11
const tableSeparator = '|' ;
5
12
6
- export function convertJsonToMarkdown ( jsonItems : Record < string , unknown > [ ] , filter : RegExp ) : string {
13
+ export function convertJsonToMarkdown ( jsonItems : DeclarationReflection [ ] , filter : RegExp ) : string {
7
14
const result : string [ ] = [ ] ;
8
- const filteredItems = jsonItems . filter ( ( { name } ) => ( name as string ) . match ( filter ) ) ;
15
+ const filteredItems = jsonItems . filter ( ( { name } ) => name . match ( filter ) ) ;
9
16
10
17
const displaySection = ( stringArray : string [ ] , sectionString : string ) => {
11
18
const res = stringArray ;
@@ -17,29 +24,23 @@ export function convertJsonToMarkdown(jsonItems: Record<string, unknown>[], filt
17
24
18
25
const interfaces = getInterfaces ( filteredItems ) ,
19
26
types = getTypes ( filteredItems ) ,
20
- classes = getClass ( filteredItems ) ,
21
- typeAlias = getTypeAlias ( filteredItems ) ,
22
- variables = getVariables ( filteredItems ) ;
27
+ classes = getClass ( filteredItems ) ;
23
28
24
29
// Create Table
25
- result . push (
26
- ...( interfaces ?. length ? [ `* [**Interfaces**](#interfaces)` ] : [ ] ) ,
27
- ...( types ?. length ? [ `* [**Types**](#types)` ] : [ ] ) ,
28
- ...( classes ?. length ? [ `* [**Classes**](#classes)` ] : [ ] ) ,
29
- ...( typeAlias ?. length ? [ `* [**Type alias**](#type-alias)` ] : [ ] ) ,
30
- ...( variables ?. length ? [ `* [**Variables**](#variables)` ] : [ ] ) ,
30
+ result . push (
31
+ ...( interfaces ?. length ? [ `* [**Interfaces**](#interfaces)` ] : [ ] ) ,
32
+ ...( types ?. length ? [ `* [**Types**](#types)` ] : [ ] ) ,
33
+ ...( classes ?. length ? [ `* [**Classes**](#classes)` ] : [ ] ) ,
31
34
) ;
32
35
33
36
displaySection ( interfaces , `\n## Interfaces` ) ;
34
37
displaySection ( types , `\n## Types` ) ;
35
38
displaySection ( classes , `\n## Classes` ) ;
36
- displaySection ( typeAlias , `\n## Type alias` ) ;
37
- displaySection ( variables , `\n## Variables` ) ;
38
39
39
40
return result . join ( '\n' ) ;
40
41
}
41
42
42
- function getInterfaces ( filteredJSON : UnknownObject [ ] ) : string [ ] {
43
+ function getInterfaces ( filteredJSON : DeclarationReflection [ ] ) : string [ ] {
43
44
const res : string [ ] = [ ] ;
44
45
45
46
filteredJSON . filter ( ( { kindString : filteredString , children : filteredChildren } ) => filteredString === 'Interface'
@@ -48,26 +49,26 @@ function getInterfaces(filteredJSON: UnknownObject[]): string[] {
48
49
49
50
// Find default values
50
51
const defaultValues : Record < string , string > = { } ;
51
- ( ( ( filteredJSON . find ( ( { kindString : defaultString , name : defaultName } ) => defaultString === 'Variable'
52
- && ( defaultName as string ) . toLowerCase ( ) === `${ ( name as string ) . toLowerCase ( ) } defaultdoc` )
53
- ?. type as UnknownObject ) ?. declaration as UnknownObject ) ?. children as UnknownObject [ ] )
54
- ?. forEach ( ( { name, defaultValue } ) => {
55
- defaultValues [ name as string ] = defaultValue as string ;
56
- } ) ;
52
+ ( filteredJSON . find ( ( { kindString : defaultString , name : defaultName } ) => {
53
+ return defaultString === 'Variable' && defaultName . toLowerCase ( ) === `${ name . toLowerCase ( ) } defaultdoc` ;
54
+ } ) as unknown as ReflectionType )
55
+ ?. declaration . children ?. forEach ( ( { name, defaultValue } ) => {
56
+ defaultValues [ name ] = defaultValue ?. toString ( ) || '' ;
57
+ } ) ;
57
58
58
59
res . push (
59
60
tableSeparator + [ 'name' , 'Type' , 'Required' , 'Default' , 'Description' ] . join ( ` ${ tableSeparator } ` ) + tableSeparator
60
61
) ;
61
62
res . push (
62
63
tableSeparator + [ '---' , '---' , ':---:' , '---' , '---' ] . join ( `${ tableSeparator } ` ) + tableSeparator
63
64
) ;
64
- ( children as UnknownObject [ ] ) . forEach ( ( { name, type, signatures, flags, comment } ) => {
65
- const commentString : string = ( comment as StringObject || ( signatures && ( signatures as Record < string , UnknownObject > [ ] ) [ 0 ] ?. comment ) ) ?. shortText as unknown as string || '' ;
65
+ children ? .forEach ( ( { name, type, signatures, flags, comment } ) => {
66
+ const commentString : string = ( comment || ( signatures && signatures [ 0 ] ?. comment ) ) ?. shortText as unknown as string || '' ;
66
67
res . push ( tableSeparator + [
67
68
`**\`${ name } \`**` ,
68
- type ? printType ( type as UnknownObject ) : printType ( ( signatures as UnknownObject [ ] ) [ 0 ] ?. type as UnknownObject ) ,
69
- ! ( flags as StringObject ) . isOptional ? '✴️' : '' ,
70
- defaultValues [ name as string ] ? `\`${ defaultValues [ name as string ] } \`` : '' ,
69
+ type ? printType ( type ) : printType ( signatures ?. [ 0 ] ?. type ) ,
70
+ ! flags . isOptional ? '✴️' : '' ,
71
+ defaultValues [ name ] ? `\`${ defaultValues [ name ] } \`` : '' ,
71
72
commentString . replace ( / \n / g, '' )
72
73
] . join ( ` ${ tableSeparator } ` ) + tableSeparator ) ;
73
74
} ) ;
@@ -76,131 +77,84 @@ function getInterfaces(filteredJSON: UnknownObject[]): string[] {
76
77
return res ;
77
78
}
78
79
79
- function getTypes ( filteredJSON : UnknownObject [ ] ) : string [ ] {
80
+ function getTypes ( filteredJSON : DeclarationReflection [ ] ) : string [ ] {
80
81
const res : string [ ] = [ ] ;
81
- filteredJSON . filter ( ( item ) => item . kindString === 'Enumeration' ) . forEach ( ( enumeration : UnknownObject ) => {
82
+ filteredJSON . filter ( ( item ) => item . kindString === 'Enumeration' ) . forEach ( ( enumeration : DeclarationReflection ) => {
82
83
res . push ( `\n### ${ enumeration . name } ` ) ;
83
84
res . push ( `| |\n|:---:|` ) ;
84
85
if ( enumeration . children ) {
85
- res . push ( ( enumeration . children as UnknownObject [ ] ) . map ( ( property ) => `| \`${ property . name } \` |` ) . join ( '\n' ) ) ;
86
+ res . push ( enumeration . children . map ( ( property ) => `| \`${ property . name } \` |` ) . join ( '\n' ) ) ;
86
87
}
87
88
} ) ;
88
89
return res ;
89
90
}
90
91
91
- function getClass ( filteredJSON : UnknownObject [ ] ) : string [ ] {
92
+ function getClass ( filteredJSON : DeclarationReflection [ ] ) : string [ ] {
92
93
const res : string [ ] = [ ] ;
93
- filteredJSON . filter ( ( item ) => item . kindString === 'Class' ) . forEach ( ( {
94
- name : className ,
95
- comment : classComment ,
96
- children : classChildren ,
97
- } ) => {
98
- res . push ( `\n### ${ className } ` ) ;
99
- const comments = ( classComment as UnknownObject ) ?. shortText as string ;
94
+ filteredJSON . filter ( ( item ) => item . kindString === 'Class' ) . forEach ( ( typeDocClass ) => {
95
+ res . push ( `\n### ${ typeDocClass . name } ` ) ;
96
+ const comments = typeDocClass . comment ?. shortText ;
100
97
if ( comments ) {
101
98
res . push ( `_${ comments . replace ( / \n / gmi, '_\n_' ) } _\n` ) ;
102
99
}
103
100
104
101
// Methods
105
- const methods = ( classChildren as UnknownObject [ ] ) ?. filter ( ( item ) => item . kindString === 'Method' ) ;
106
- if ( typeof methods !== 'undefined' && methods . length ) {
107
- res . push ( `#### Methods` ) ;
108
- methods . forEach ( ( { signatures : methodSignatures , name : methodName } ) => {
109
- const { signatureParameters, signatureType, signatureComment } = ( methodSignatures as UnknownObject [ ] ) [ 0 ] ;
110
- const params : string [ ] = [ ] ;
111
- const parameterSection : string [ ] = [ ] ;
112
- if ( signatureParameters && ( signatureParameters as UnknownObject [ ] ) . length ) {
113
- parameterSection . push ( `Name | Type | Description \n---|---|---` ) ;
114
- ( signatureParameters as UnknownObject [ ] ) ?. map ( ( {
115
- name : paramName ,
116
- type : paramType ,
117
- comment : paramComment ,
118
- } ) => {
119
- params . push ( `\`${ paramName } \`: ${ printType ( paramType as UnknownObject ) } ` ) ;
120
- parameterSection . push ( `**${ paramName } ** | ${ printType ( paramType as UnknownObject ) } | ${ ( paramComment as StringObject ) ?. shortText } ` ) ;
121
- } ) ;
122
- }
123
- res . push ( `> **${ methodName } **(${ ( params || [ '' ] ) . join ( ',' ) } ) => ${ printType ( signatureType as UnknownObject ) } \n` ) ;
124
- const comments = ( ( signatureComment as UnknownObject ) ?. shortText as string ) ;
125
- if ( comments ) {
126
- res . push ( `_${ comments . replace ( / \n / gmi, '_\n_' ) } _\n` ) ;
127
- }
128
- res . push ( `${ ( parameterSection || [ '' ] ) . join ( '\n' ) } ` ) ;
129
- } ) ;
130
- }
131
- } ) ;
132
-
133
- return res ;
134
- }
135
-
136
- function getTypeAlias ( filteredJSON : UnknownObject [ ] ) : string [ ] {
137
- const res : string [ ] = [ ] ;
138
- filteredJSON . filter ( ( { kindString : itemString , type : itemType } ) => itemString === 'Type alias'
139
- && ( itemType as UnknownObject ) . type !== 'template-literal' ) . forEach ( ( {
140
- name : typeAliasName ,
141
- comment : typeAliasComment ,
142
- type : typeAliasType ,
143
- typeParameter : typeAliasTypeParameter ,
144
- } ) => {
145
- res . push ( `\n### ${ typeAliasName } ` ) ;
146
- if ( typeAliasComment ) {
147
- res . push ( `\n${ ( typeAliasComment as UnknownObject ) ?. shortText } ` ) ;
102
+ const methods = typeDocClass . children ?. filter ( ( method ) => method . kind === ReflectionKind . Method )
103
+ . filter ( ( method ) => method . decorators ?. [ 0 ] . name === 'Method' && ( method . decorators ?. [ 0 ] . type as ReferenceType ) ?. package === '@stencil/core' ) ;
104
+ if ( ! methods ?. length ) {
105
+ return ;
148
106
}
149
-
150
- switch ( ( typeAliasType as UnknownObject ) . type ) {
151
- case 'reference' :
152
- ( ( typeAliasTypeParameter || ( typeAliasType as UnknownObject ) . typeArguments ) as UnknownObject [ ] ) ?. map ( ( typeParameter ) => {
153
- res . push ( `\n> - ${ printType ( typeParameter as UnknownObject ) !== '_unknown_' ?
154
- printType ( typeParameter as UnknownObject ) :
155
- printType ( typeParameter . type as UnknownObject ) } `
156
- ) ;
107
+ res . push ( `#### Methods` ) ;
108
+ methods . forEach ( ( { signatures : methodSignatures , name : methodName } ) => {
109
+ if ( ! methodSignatures ?. [ 0 ] ) {
110
+ return ;
111
+ }
112
+ const { parameters, type, comment } = methodSignatures [ 0 ] ;
113
+ const params : string [ ] = [ ] ;
114
+ const parameterSection : string [ ] = [ ] ;
115
+ if ( parameters && parameters . length ) {
116
+ parameterSection . push ( `Name | Type | Description \n---|---|---` ) ;
117
+ parameters ?. map ( ( { name : paramName , type : paramType , comment : paramComment } ) => {
118
+ params . push ( `\`${ paramName } \`: ${ printType ( paramType ) } ` ) ;
119
+ parameterSection . push ( `**${ paramName } ** | ${ printType ( paramType ) } | ${ paramComment ?. shortText || '' } ` ) ;
157
120
} ) ;
158
- break ;
159
- case 'union' :
160
- ( ( typeAliasType as UnknownObject ) . types as UnknownObject [ ] ) . forEach ( ( typeVariant ) => {
161
- res . push ( `\n> - ${ printType ( typeVariant ) } ` ) ;
162
- } ) ;
163
- break ;
164
- }
165
- } ) ;
166
- filteredJSON . filter ( ( item ) => item . kindString === 'Interface'
167
- && typeof item . extendedTypes !== 'undefined'
168
- && ( item . extendedTypes as UnknownObject [ ] ) [ 0 ] ?. type === 'reference' ) . forEach ( ( {
169
- name : typeAliasName ,
170
- extendedTypes : typeAliasExtendedTypes ,
171
- } ) => {
172
-
173
- res . push ( `\n### ${ typeAliasName } ` ) ;
174
- res . push ( `\n> _Based on ${ printType ( ( typeAliasExtendedTypes as UnknownObject [ ] ) [ 0 ] ) } _` ) ;
175
- } ) ;
176
- return res ;
177
- }
178
-
179
- function getVariables ( filteredJSON : UnknownObject [ ] ) : string [ ] {
180
- const res : string [ ] = [ ] ;
181
- filteredJSON
182
- . filter ( ( {
183
- kindString : itemString ,
184
- type : itemType ,
185
- } ) => itemString === 'Variable' && ( itemType as UnknownObject ) . type !== 'array' && ( itemType as UnknownObject ) . type !== 'reflection' )
186
- . forEach ( ( { name : variableName , type : variableType } ) => {
187
- res . push ( `\n### ${ variableName } ` ) ;
188
- res . push ( `${ printType ( variableType as UnknownObject ) } ` ) ;
121
+ }
122
+ res . push ( `> **${ methodName } **(${ ( params || [ '' ] ) . join ( ',' ) } ) => ${ printType ( type ) } \n` ) ;
123
+ const comments = comment ?. shortText ;
124
+ if ( comments ) {
125
+ res . push ( `_${ comments . replace ( / \n / gmi, '_\n_' ) } _\n` ) ;
126
+ }
127
+ res . push ( `${ ( parameterSection || [ '' ] ) . join ( '\n' ) } ` ) ;
189
128
} ) ;
129
+ } ) ;
130
+
190
131
return res ;
191
132
}
192
133
193
- function printType ( typeObject : UnknownObject ) {
194
- const getTypeValue = ( tObj : UnknownObject ) => typeof tObj . name !== 'undefined' ? `${ tObj . name } ` : `${ tObj . value } ` ;
195
- if ( typeObject && typeObject . type ) {
196
- switch ( typeObject . type ) {
134
+ function printType ( typeObject ?: SomeType | unknown ) {
135
+ const getTypeValue = ( tObj : SomeType | LiteralType | IntrinsicType ) => {
136
+ if ( 'name' in tObj ) {
137
+ return tObj . name . toString ( ) ;
138
+ }
139
+ if ( 'value' in tObj ) {
140
+ return tObj . value ?. toString ( ) ;
141
+ }
142
+ return tObj . type
143
+ } ;
144
+ const someType = typeObject as SomeType ;
145
+ if ( someType && someType . type ) {
146
+ switch ( someType . type ) {
197
147
case 'intrinsic' :
198
148
case 'literal' :
199
- return `_${ getTypeValue ( typeObject ) } _` ;
200
- case 'reference' :
201
- return `\`${ typeObject . name } \`` ;
149
+ return `_${ getTypeValue ( someType ) } _` ;
150
+ case 'reference' : {
151
+ if ( someType . name === 'Promise' ) {
152
+ return `\`${ someType . name } <${ getTypeValue ( someType . typeArguments ?. [ 0 ] as IntrinsicType ) } >\`` ;
153
+ }
154
+ return `\`${ someType . name } \`` ;
155
+ }
202
156
case 'union' :
203
- return ( typeObject . types as UnknownObject [ ] ) . map ( ( tObj ) => `\`${ getTypeValue ( tObj ) } \`` ) . join ( ' \\| ' ) ;
157
+ return someType . types . map ( ( tObj ) => `\`${ getTypeValue ( tObj ) } \`` ) . join ( ' \\| ' ) ;
204
158
}
205
159
}
206
160
return '_unknown_' ;
0 commit comments