Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strong-jars-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

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.
7 changes: 7 additions & 0 deletions packages/plugins/other/visitor-plugin-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ export function getBaseTypeNode(typeNode: TypeNode): NamedTypeNode {

export function convertNameParts(str: string, func: (str: string) => string, removeUnderscore = false): string {
if (removeUnderscore) {
// Special case: if the string is just underscores ad we're being asked to remove them, preserve it as-is.
// this prevents empty identifiers from making their way into the output. Yes, we want to remove underscores,
// but we can't when that's all the string is.
if (/^_+$/.test(str)) {
return str;
}

return func(str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,15 @@ describe('convertFactory', () => {
expect(factory('_Myname')).toBe('Myname');
expect(factory('My_name')).toBe('Myname');
});

it('Should handle enum values that are just underscores when transformUnderscore is true', () => {
const factory = convertFactory({
namingConvention: {
transformUnderscore: true,
},
});

expect(factory('_')).toBe('_');
expect(factory('__')).toBe('__');
});
});