1- import { ClassDeclaration , Project , Scope , InterfaceDeclaration } from 'ts-morph' ;
1+ import { ClassDeclaration , Project , Scope , InterfaceDeclaration , SourceFile } from 'ts-morph' ;
22import { FormProperty , Resource , SwaggerDefinition } from '../../../core/types.js' ;
33import { camelCase , getTypeScriptType , pascalCase , singular } from '../../../core/utils.js' ;
44import { commonStandaloneImports } from './common-imports.js' ;
@@ -301,19 +301,29 @@ export class FormComponentGenerator {
301301 formArrayProps . forEach ( prop => {
302302 const arrayName = prop . name ;
303303 const singularPascal = pascalCase ( singular ( arrayName ) ) ;
304- const arrayGetterName = `${ camelCase ( singular ( arrayName ) ) } Array` ;
304+ const singularCamel = camelCase ( singular ( arrayName ) ) ;
305+ const arrayGetterName = `${ singularCamel } Array` ;
305306 const arrayItemInterfaceName = `${ singularPascal } Form` ;
307+
306308 classDeclaration . addGetAccessor ( {
307309 name : arrayGetterName ,
308310 returnType : `FormArray<FormGroup<${ arrayItemInterfaceName } >>` ,
309- statements : `return this.form.get('${ arrayName } ') as FormArray<FormGroup<${ arrayItemInterfaceName } >>;`
311+ statements : `return this.form.get('${ arrayName } ') as FormArray<FormGroup<${ arrayItemInterfaceName } >>;` ,
312+ docs : [ `Getter for the ${ singularCamel } FormArray.` ]
310313 } ) ;
314+
315+ // FIX: Move the `docs` from the parameter to the method itself and format with @param.
311316 const createMethod = classDeclaration . addMethod ( {
312317 name : `create${ singularPascal } ` ,
313318 scope : Scope . Private ,
314- parameters : [ { name : 'item?' , type : 'any' , initializer : '{}' , docs : [ "(optional) An object to patch the form group with." ] } ] ,
319+ docs : [
320+ `Creates a FormGroup for a single ${ singularCamel } item.` ,
321+ `@param item (optional) An object to patch the new form group with.`
322+ ] ,
323+ parameters : [ { name : 'item?' , type : 'any' , initializer : '{}' } ] ,
315324 returnType : `FormGroup<${ arrayItemInterfaceName } >`
316325 } ) ;
326+
317327 const itemSchema = ( prop . schema . items as SwaggerDefinition ) . properties ! ;
318328 const formControls = Object . entries ( itemSchema )
319329 . map ( ( [ key , schema ] ) => {
@@ -323,9 +333,19 @@ export class FormComponentGenerator {
323333 return `'${ key } ': new FormControl<${ this . getFormControlTypeString ( schema ) } >(item?.${ key } ?? null${ validatorString } )` ;
324334 } )
325335 . join ( ',\n ' ) ;
336+
326337 createMethod . setBodyText ( `return new FormGroup<${ arrayItemInterfaceName } >({\n ${ formControls } \n });` ) ;
327- classDeclaration . addMethod ( { name : `add${ singularPascal } ` , statements : `this.${ arrayGetterName } .push(this.create${ singularPascal } ());` } ) ;
328- classDeclaration . addMethod ( { name : `remove${ singularPascal } ` , parameters : [ { name : 'index' , type : 'number' } ] , statements : `this.${ arrayGetterName } .removeAt(index);` } ) ;
338+ classDeclaration . addMethod ( {
339+ name : `add${ singularPascal } ` ,
340+ statements : `this.${ arrayGetterName } .push(this.create${ singularPascal } ());` ,
341+ docs : [ `Adds a new, empty ${ singularCamel } to the form array.` ]
342+ } ) ;
343+ classDeclaration . addMethod ( {
344+ name : `remove${ singularPascal } ` ,
345+ parameters : [ { name : 'index' , type : 'number' } ] ,
346+ statements : `this.${ arrayGetterName } .removeAt(index);` ,
347+ docs : [ `Removes a ${ singularCamel } from the form array at a given index.` , `@param index The index of the item to remove.` ]
348+ } ) ;
329349 } ) ;
330350 }
331351
0 commit comments