1+ import { EnumDeclaration } from 'typescript' ;
2+
13import {
24 type Comments ,
35 compiler ,
@@ -121,10 +123,34 @@ export const generateType = ({
121123} ;
122124
123125const processComposition = ( props : TypesProps ) => {
124- processType ( props ) ;
125- props . model . enums . forEach ( ( enumerator ) =>
126- processEnum ( { ...props , model : enumerator } ) ,
127- ) ;
126+ const config = getConfig ( ) ;
127+
128+ if ( config . types . enums !== 'typescript+namespace' ) {
129+ processType ( props ) ;
130+ props . model . enums . forEach ( ( enumerator ) =>
131+ processEnum ( { ...props , model : enumerator } ) ,
132+ ) ;
133+ } else {
134+ const enumDeclarations = [ ] as EnumDeclaration [ ] ;
135+ props . model . enums . forEach ( ( enumerator ) =>
136+ processScopedEnum ( {
137+ ...props ,
138+ model : enumerator ,
139+ onNode : ( node ) => {
140+ enumDeclarations . push ( node as EnumDeclaration ) ;
141+ } ,
142+ } ) ,
143+ ) ;
144+ processType ( props ) ;
145+ if ( enumDeclarations . length > 0 ) {
146+ props . onNode (
147+ compiler . types . namespace ( {
148+ name : props . model . name ,
149+ statements : enumDeclarations ,
150+ } ) ,
151+ ) ;
152+ }
153+ }
128154} ;
129155
130156const processEnum = ( { client, model, onNode } : TypesProps ) => {
@@ -146,7 +172,10 @@ const processEnum = ({ client, model, onNode }: TypesProps) => {
146172 model . deprecated && '@deprecated' ,
147173 ] ;
148174
149- if ( config . types . enums === 'typescript' ) {
175+ if (
176+ config . types . enums === 'typescript' ||
177+ config . types . enums === 'typescript+namespace'
178+ ) {
150179 generateEnum ( {
151180 client,
152181 comments,
@@ -185,6 +214,30 @@ const processEnum = ({ client, model, onNode }: TypesProps) => {
185214 } ) ;
186215} ;
187216
217+ const processScopedEnum = ( { model, onNode } : TypesProps ) => {
218+ const properties : Record < string | number , unknown > = { } ;
219+ const comments : Record < string | number , Comments > = { } ;
220+ model . enum . forEach ( ( enumerator ) => {
221+ const { key, value } = enumEntry ( enumerator ) ;
222+ properties [ key ] = value ;
223+ const comment = enumerator . customDescription || enumerator . description ;
224+ if ( comment ) {
225+ comments [ key ] = [ escapeComment ( comment ) ] ;
226+ }
227+ } ) ;
228+ onNode (
229+ compiler . types . enum ( {
230+ comments,
231+ leadingComment : [
232+ model . description && escapeComment ( model . description ) ,
233+ model . deprecated && '@deprecated' ,
234+ ] ,
235+ name : model . meta ?. name || model . name ,
236+ obj : properties ,
237+ } ) ,
238+ ) ;
239+ } ;
240+
188241const processType = ( { client, model, onNode } : TypesProps ) => {
189242 generateType ( {
190243 client,
0 commit comments