@@ -20,21 +20,34 @@ type Options<T = TypeNode> = {
20
20
enumsPrefix : string ;
21
21
currentType : T ;
22
22
customScalars ?: ScalarMap ;
23
+ transformUnderscore : boolean ;
24
+ } ;
25
+
26
+ const convertName = ( value : string , fn : ( v : string ) => string , transformUnderscore : boolean ) : string => {
27
+ if ( transformUnderscore ) {
28
+ return fn ( value ) ;
29
+ }
30
+
31
+ return value
32
+ . split ( '_' )
33
+ . map ( ( s ) => fn ( s ) )
34
+ . join ( '_' ) ;
23
35
} ;
24
36
25
37
const createNameConverter =
26
- ( convention : NamingConvention ) =>
38
+ ( convention : NamingConvention , transformUnderscore : boolean ) =>
27
39
( value : string , prefix = '' ) => {
28
40
switch ( convention ) {
29
- case 'upper-case#upperCase' :
30
- return `${ prefix } ${ upperCase ( value || '' ) } ` ;
41
+ case 'upper-case#upperCase' : {
42
+ return `${ prefix } ${ convertName ( value , ( s ) => upperCase ( s || '' ) , transformUnderscore ) } ` ;
43
+ }
31
44
case 'keep' :
32
45
return `${ prefix } ${ value } ` ;
33
46
case 'pascal-case#pascalCase' :
34
47
// fallthrough
35
48
default :
36
49
// default to pascal case in case of unknown values
37
- return `${ prefix } ${ pascalCase ( value || '' ) } ` ;
50
+ return `${ prefix } ${ convertName ( value , ( s ) => pascalCase ( s || '' ) , transformUnderscore ) } ` ;
38
51
}
39
52
} ;
40
53
@@ -46,8 +59,8 @@ const toMockName = (typedName: string, casedName: string, prefix?: string) => {
46
59
return `${ a ( firstWord , { articleOnly : true } ) } ${ casedName } ` ;
47
60
} ;
48
61
49
- const updateTextCase = ( str : string , enumValuesConvention : NamingConvention ) => {
50
- const convert = createNameConverter ( enumValuesConvention ) ;
62
+ const updateTextCase = ( str : string , enumValuesConvention : NamingConvention , transformUnderscore : boolean ) => {
63
+ const convert = createNameConverter ( enumValuesConvention , transformUnderscore ) ;
51
64
52
65
if ( str . charAt ( 0 ) === '_' ) {
53
66
return str . replace (
@@ -91,7 +104,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
91
104
92
105
casual . seed ( hashedString ( opts . typeName + opts . fieldName ) ) ;
93
106
const name = opts . currentType . name . value ;
94
- const casedName = createNameConverter ( opts . typenamesConvention ) ( name ) ;
107
+ const casedName = createNameConverter ( opts . typenamesConvention , opts . transformUnderscore ) ( name ) ;
95
108
switch ( name ) {
96
109
case 'String' :
97
110
return `'${ casual . word } '` ;
@@ -109,11 +122,15 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
109
122
switch ( foundType . type ) {
110
123
case 'enum' : {
111
124
// It's an enum
112
- const typenameConverter = createNameConverter ( opts . typenamesConvention ) ;
125
+ const typenameConverter = createNameConverter (
126
+ opts . typenamesConvention ,
127
+ opts . transformUnderscore ,
128
+ ) ;
113
129
const value = foundType . values ? foundType . values [ 0 ] : '' ;
114
130
return `${ typenameConverter ( foundType . name , opts . enumsPrefix ) } .${ updateTextCase (
115
131
value ,
116
132
opts . enumValuesConvention ,
133
+ opts . transformUnderscore ,
117
134
) } `;
118
135
}
119
136
case 'union' :
@@ -206,8 +223,9 @@ const getMockString = (
206
223
addTypename = false ,
207
224
prefix ,
208
225
typesPrefix = '' ,
226
+ transformUnderscore : boolean ,
209
227
) => {
210
- const typenameConverter = createNameConverter ( typenamesConvention ) ;
228
+ const typenameConverter = createNameConverter ( typenamesConvention , transformUnderscore ) ;
211
229
const casedName = typenameConverter ( typeName ) ;
212
230
const casedNameWithPrefix = typenameConverter ( typeName , typesPrefix ) ;
213
231
const typename = addTypename ? `\n __typename: '${ casedName } ',` : '' ;
@@ -246,15 +264,17 @@ const getImportTypes = ({
246
264
typesFile,
247
265
typesPrefix,
248
266
enumsPrefix,
267
+ transformUnderscore,
249
268
} : {
250
269
typenamesConvention : NamingConvention ;
251
270
definitions : any ;
252
271
types : TypeItem [ ] ;
253
272
typesFile : string ;
254
273
typesPrefix : string ;
255
274
enumsPrefix : string ;
275
+ transformUnderscore : boolean ;
256
276
} ) => {
257
- const typenameConverter = createNameConverter ( typenamesConvention ) ;
277
+ const typenameConverter = createNameConverter ( typenamesConvention , transformUnderscore ) ;
258
278
const typeImports = typesPrefix ?. endsWith ( '.' )
259
279
? [ typesPrefix . slice ( 0 , - 1 ) ]
260
280
: definitions
@@ -296,6 +316,7 @@ export interface TypescriptMocksPluginConfig {
296
316
terminateCircularRelationships ?: boolean ;
297
317
typesPrefix ?: string ;
298
318
enumsPrefix ?: string ;
319
+ transformUnderscore ?: boolean ;
299
320
}
300
321
301
322
interface TypeItem {
@@ -316,6 +337,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
316
337
317
338
const enumValuesConvention = config . enumValues || 'pascal-case#pascalCase' ;
318
339
const typenamesConvention = config . typenames || 'pascal-case#pascalCase' ;
340
+ const transformUnderscore = config . transformUnderscore ?? true ;
319
341
// List of types that are enums
320
342
const types : TypeItem [ ] = [ ] ;
321
343
const visitor : VisitorType = {
@@ -357,6 +379,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
357
379
enumsPrefix : config . enumsPrefix ,
358
380
currentType : node . type ,
359
381
customScalars : config . scalars ,
382
+ transformUnderscore,
360
383
} ) ;
361
384
362
385
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -384,6 +407,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
384
407
enumsPrefix : config . enumsPrefix ,
385
408
currentType : field . type ,
386
409
customScalars : config . scalars ,
410
+ transformUnderscore,
387
411
} ) ;
388
412
389
413
return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
@@ -399,6 +423,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
399
423
false ,
400
424
config . prefix ,
401
425
config . typesPrefix ,
426
+ transformUnderscore ,
402
427
) ;
403
428
} ,
404
429
} ;
@@ -421,6 +446,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
421
446
! ! config . addTypename ,
422
447
config . prefix ,
423
448
config . typesPrefix ,
449
+ transformUnderscore ,
424
450
) ;
425
451
} ,
426
452
} ;
@@ -441,6 +467,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
441
467
! ! config . addTypename ,
442
468
config . prefix ,
443
469
config . typesPrefix ,
470
+ transformUnderscore ,
444
471
) ;
445
472
} ,
446
473
} ;
@@ -467,6 +494,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
467
494
typesFile,
468
495
typesPrefix : config . typesPrefix ,
469
496
enumsPrefix : config . enumsPrefix ,
497
+ transformUnderscore : transformUnderscore ,
470
498
} ) ;
471
499
// List of function that will generate the mock.
472
500
// We generate it after having visited because we need to distinct types from enums
0 commit comments