@@ -11,14 +11,23 @@ export const getEnumNames = (schemaObject: SchemaObject | undefined) => {
1111 ) ;
1212} ;
1313
14+ export const getEnumDescriptions = ( schemaObject : SchemaObject | undefined ) => {
15+ return (
16+ schemaObject ?. [ 'x-enumDescriptions' ] ||
17+ schemaObject ?. [ 'x-enumdescriptions' ] ||
18+ schemaObject ?. [ 'x-enum-descriptions' ]
19+ ) ;
20+ } ;
21+
1422export const getEnum = (
1523 value : string ,
1624 enumName : string ,
1725 names : string [ ] | undefined ,
1826 enumGenerationType : EnumGeneration ,
27+ descriptions ?: string [ ] ,
1928) => {
2029 if ( enumGenerationType === EnumGeneration . CONST )
21- return getTypeConstEnum ( value , enumName , names ) ;
30+ return getTypeConstEnum ( value , enumName , names , descriptions ) ;
2231 if ( enumGenerationType === EnumGeneration . ENUM )
2332 return getNativeEnum ( value , enumName , names ) ;
2433 if ( enumGenerationType === EnumGeneration . UNION )
@@ -30,6 +39,7 @@ const getTypeConstEnum = (
3039 value : string ,
3140 enumName : string ,
3241 names ?: string [ ] ,
42+ descriptions ?: string [ ] ,
3343) => {
3444 let enumValue = `export type ${ enumName } = typeof ${ enumName } [keyof typeof ${ enumName } ]` ;
3545
@@ -40,9 +50,9 @@ const getTypeConstEnum = (
4050
4151 enumValue += ';\n' ;
4252
43- const implementation = getEnumImplementation ( value , names ) ;
53+ const implementation = getEnumImplementation ( value , names , descriptions ) ;
4454
45- enumValue += ` \n\n` ;
55+ enumValue += ' \n\n' ;
4656
4757 enumValue += '// eslint-disable-next-line @typescript-eslint/no-redeclare\n' ;
4858
@@ -51,15 +61,23 @@ const getTypeConstEnum = (
5161 return enumValue ;
5262} ;
5363
54- export const getEnumImplementation = ( value : string , names ?: string [ ] ) => {
64+ export const getEnumImplementation = (
65+ value : string ,
66+ names ?: string [ ] ,
67+ descriptions ?: string [ ] ,
68+ ) => {
5569 // empty enum or null-only enum
5670 if ( value === '' ) return '' ;
5771
5872 return [ ...new Set ( value . split ( ' | ' ) ) ] . reduce ( ( acc , val , index ) => {
5973 const name = names ?. [ index ] ;
74+ const description = descriptions ?. [ index ] ;
75+ const comment = description ? ` /** ${ description } */\n` : '' ;
76+
6077 if ( name ) {
6178 return (
6279 acc +
80+ comment +
6381 ` ${ keyword . isIdentifierNameES5 ( name ) ? name : `'${ name } '` } : ${ val } ,\n`
6482 ) ;
6583 }
@@ -83,6 +101,7 @@ export const getEnumImplementation = (value: string, names?: string[]) => {
83101
84102 return (
85103 acc +
104+ comment +
86105 ` ${ keyword . isIdentifierNameES5 ( key ) ? key : `'${ key } '` } : ${ val } ,\n`
87106 ) ;
88107 } , '' ) ;
0 commit comments