@@ -27,6 +27,7 @@ export interface IProp {
27
27
optional : boolean ;
28
28
importType ?: string ;
29
29
importPath ?: string ;
30
+ documentation ?: string ;
30
31
}
31
32
32
33
export interface IPropTypes {
@@ -121,7 +122,9 @@ function parseAst(ast: any, instanceOfResolver: InstanceOfResolver): IParsingRes
121
122
propTypes = { } ;
122
123
walk ( attributeNode . value , {
123
124
'ObjectProperty' : ( propertyNode : any ) : void => {
124
- propTypes [ propertyNode . key . name ] = getTypeFromPropType ( propertyNode . value , instanceOfResolver ) ;
125
+ const prop : IProp = getTypeFromPropType ( propertyNode . value , instanceOfResolver ) ;
126
+ prop . documentation = getOptionalDocumentation ( propertyNode ) ;
127
+ propTypes [ propertyNode . key . name ] = prop ;
125
128
}
126
129
} ) ;
127
130
}
@@ -135,6 +138,12 @@ function parseAst(ast: any, instanceOfResolver: InstanceOfResolver): IParsingRes
135
138
} ;
136
139
}
137
140
141
+ function getOptionalDocumentation ( propertyNode : any ) : string {
142
+ return ( ( ( propertyNode . leadingComments || [ ] ) as any [ ] )
143
+ . filter ( ( comment : any ) => comment . type == 'CommentBlock' ) [ 0 ] || { } )
144
+ . value ;
145
+ }
146
+
138
147
interface IAstWalkHandlers {
139
148
[ type : string ] : ( node : IASTNode ) => void ;
140
149
}
@@ -293,26 +302,41 @@ export class Writer {
293
302
}
294
303
}
295
304
296
- public props ( name : string , props : any , fn ?: ( ) => void ) : void {
305
+ public props ( name : string , props : IPropTypes , fn ?: ( ) => void ) : void {
297
306
this . interface ( `${ name } Props` , ( ) => {
298
307
this . prop ( 'key' , 'any' , true ) ;
299
308
Object . keys ( props ) . forEach ( ( propName : any ) => {
300
309
const prop : IProp = props [ propName ] ;
301
- this . prop ( propName , prop . type , prop . optional ) ;
310
+ this . prop ( propName , prop . type , prop . optional , prop . documentation ) ;
302
311
} ) ;
303
312
} ) ;
304
313
if ( fn ) {
305
314
fn ( ) ;
306
315
}
307
316
}
308
317
309
- public prop ( name : string , type : string , optional : boolean , fn ?: ( ) => void ) : void {
318
+ public prop ( name : string , type : string , optional : boolean , documentation ?: string ) : void {
310
319
this . indent ( ) ;
320
+ if ( documentation ) {
321
+ this . comment ( documentation ) ;
322
+ }
311
323
this . code += `${ name } ${ optional ? '?' : '' } : ${ type } ;` ;
312
324
this . nl ( ) ;
313
- if ( fn ) {
314
- fn ( ) ;
315
- }
325
+ }
326
+
327
+ public comment ( comment : string ) : void {
328
+ this . code += '/*' ;
329
+ const lines : string [ ] = ( comment || '' ) . replace ( / \t / g, '' ) . split ( / \n / g) ;
330
+ lines . forEach ( ( line : string , index : number ) => {
331
+ this . code += line ;
332
+ if ( index < lines . length - 1 ) {
333
+ this . nl ( ) ;
334
+ this . indent ( ) ;
335
+ }
336
+ } ) ;
337
+ this . code += '*/' ;
338
+ this . nl ( ) ;
339
+ this . indent ( ) ;
316
340
}
317
341
318
342
public interface ( name : string , fn : ( ) => void ) : void {
0 commit comments