Skip to content

Commit

Permalink
fix: valid identifiers with enumsAsConst (#9150)
Browse files Browse the repository at this point in the history
  • Loading branch information
rliljest authored Mar 28, 2023
1 parent 5216fb5 commit 92d86b0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-files-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript': patch
---

Properly escape enum identifiers when enumsAsConst is used
10 changes: 6 additions & 4 deletions packages/plugins/typescript/typescript/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,12 @@ export class TsVisitor<
.withBlock(
node.values
.map(enumOption => {
const optionName = this.convertName(enumOption, {
useTypesPrefix: false,
transformUnderscore: true,
});
const optionName = this.makeValidEnumIdentifier(
this.convertName(enumOption, {
useTypesPrefix: false,
transformUnderscore: true,
})
);
const comment = transformComment(enumOption.description as any as string, 1);
const name = enumOption.name as unknown as string;
const enumValue: string | number = getValueFromConfig(name) ?? name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ describe('TypeScript', () => {
X_Y_Z
_TEST
My_Value
_123
}
`);
const result = (await plugin(
Expand All @@ -223,7 +224,8 @@ describe('TypeScript', () => {
ABC: 'A_B_C',
XYZ: 'X_Y_Z',
Test: '_TEST',
MyValue: 'My_Value'
MyValue: 'My_Value',
'123': '_123'
} as const;
export type MyEnum = typeof MyEnum[keyof typeof MyEnum];`);
});
Expand Down

0 comments on commit 92d86b0

Please sign in to comment.