Skip to content

Commit accdab6

Browse files
Add special handling for identifiers which consist entirely of _ characters when transformUnderscore is true to emit valid Typescript syntax (#10412)
* Add special handling for identifiers that consist entirely of _ when we are asked to remove underscores. Added a test for this behaviour as well. * Added changeset for change. * Revert "Add special handling for identifiers that consist entirely of _ when we are asked to remove underscores. Added a test for this behaviour as well." This reverts commit 3765f2a. * Added more tightly scoped behaviour and test for enums from Typescript specifically. * Update packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts Co-authored-by: Eddy Nguyen <[email protected]> --------- Co-authored-by: Eddy Nguyen <[email protected]>
1 parent d63ed00 commit accdab6

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

.changeset/strong-jars-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
---
4+
5+
Add special handling for identifiers that consist entirely of _ characters when transformUnderscore is true. This prevents _ values in GraphQL enums from being emitted without identifers in the resulting types.

packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ export interface RawTypesConfig extends RawConfig {
493493
directiveArgumentAndInputFieldMappingTypeSuffix?: string;
494494
}
495495

496+
const onlyUnderscoresPattern = /^_+$/;
497+
496498
export class BaseTypesVisitor<
497499
TRawConfig extends RawTypesConfig = RawTypesConfig,
498500
TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig
@@ -903,7 +905,9 @@ export class BaseTypesVisitor<
903905
const optionName = this.makeValidEnumIdentifier(
904906
this.convertName(enumOption, {
905907
useTypesPrefix: false,
906-
transformUnderscore: true,
908+
// We can only strip out the underscores if the value contains other
909+
// characters. Otherwise we'll generate syntactically invalid code.
910+
transformUnderscore: !onlyUnderscoresPattern.test(enumOption.name.value),
907911
})
908912
);
909913
const comment = this.getNodeComment(enumOption);

packages/plugins/typescript/typescript/tests/typescript.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('TypeScript', () => {
193193
}`);
194194
});
195195

196-
it('Should removed underscore from enum values', async () => {
196+
it('Should remove underscore from enum values', async () => {
197197
const schema = buildSchema(/* GraphQL */ `
198198
enum MyEnum {
199199
A_B_C
@@ -213,6 +213,24 @@ describe('TypeScript', () => {
213213
}`);
214214
});
215215

216+
it('Should leave underscores in enum values when the value is only underscores', async () => {
217+
const schema = buildSchema(/* GraphQL */ `
218+
enum MyEnum {
219+
_
220+
__
221+
_TEST
222+
}
223+
`);
224+
const result = await plugin(schema, [], {}, { outputFile: '' });
225+
226+
expect(result.content).toBeSimilarStringTo(`
227+
export enum MyEnum {
228+
_ = '_',
229+
__ = '__',
230+
Test = '_TEST'
231+
}`);
232+
});
233+
216234
it('Should work with enum as const', async () => {
217235
const schema = buildSchema(/* GraphQL */ `
218236
enum MyEnum {

0 commit comments

Comments
 (0)