Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Normalize TypeScript Enum Key Names #206

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions plugins/typescript/src/core/schemaToEnumDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ describe("schemaToTypeAliasDeclaration", () => {
it("should generate a string enums", () => {
const schema: SchemaObject = {
type: "string",
enum: ["AVAILABLE", "PENDING", "SOLD"],
enum: ["AVAILABLE", "PENDING", "PENDING/SUBMITTED", "SOLD", "SOLD:SHIPPED"],
};

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export enum Test {
AVAILABLE = \\"AVAILABLE\\",
PENDING = \\"PENDING\\",
SOLD = \\"SOLD\\"
PENDING_SUBMITTED = \\"PENDING/SUBMITTED\\",
SOLD = \\"SOLD\\",
SOLD_SHIPPED = \\"SOLD:SHIPPED\\"
}"
`);
});
Expand Down
3 changes: 2 additions & 1 deletion plugins/typescript/src/core/schemaToEnumDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function getEnumMembers(
let enumValueNode: ts.Expression | undefined = undefined;

if (typeof enumValue === "string") {
enumName = enumValue;
enumName = enumValue
.replace(/[^a-z0-9_]/gi, "_");
enumValueNode = f.createStringLiteral(enumValue);
} else if (typeof enumValue === "number") {
enumName = convertNumberToWord(enumValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe("generateSchemaTypes", () => {
export enum DogBreed {
saimois = \\"saimois\\",
bengal = \\"bengal\\",
british shorthair = \\"british shorthair\\"
british_shorthair = \\"british shorthair\\"
}

/**
Expand Down