Skip to content

Do not add readonly on enum with the immutable option, #1602

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

Merged
merged 1 commit into from
Apr 3, 2024
Merged
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/tender-ducks-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Do not add readonly on Typescript enum when the --immutable option is used.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ dist
node_modules

packages/openapi-typescript/test/fixtures/cli-outputs/out

# IntelliJ IDEA settings folder
/.idea
7 changes: 2 additions & 5 deletions packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function tsEnum(
name: string,
members: (string | number)[],
metadata?: { name?: string; description?: string }[],
options?: { readonly?: boolean; export?: boolean },
options?: { export?: boolean },
) {
let enumName = name.replace(JS_ENUM_INVALID_CHARS_RE, (c) => {
const last = c[c.length - 1];
Expand All @@ -244,10 +244,7 @@ export function tsEnum(
enumName = `${enumName[0].toUpperCase()}${enumName.substring(1)}`;
return ts.factory.createEnumDeclaration(
/* modifiers */ options
? tsModifiers({
readonly: options.readonly ?? false,
export: options.export ?? false,
})
? tsModifiers({ export: options.export ?? false })
: undefined,
/* name */ enumName,
/* members */ members.map((value, i) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ export function transformSchemaObjectWithComposition(
enumName,
schemaObject.enum as (string | number)[],
metadata,
{ export: true, readonly: options.ctx.immutable },

{
export: true,
// readonly: TS enum do not support the readonly modifier
},
);
options.ctx.injectFooter.push(enumType);
return ts.factory.createTypeReferenceNode(enumType.name);
Expand Down
14 changes: 14 additions & 0 deletions packages/openapi-typescript/test/lib/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe("tsEnum", () => {
}`);
});

test("with setting: export", () => {
expect(
astToString(
tsEnum("-my-color-", ["green", "red", "blue"], undefined, {
export: true,
}),
).trim(),
).toBe(`export enum MyColor {
green = "green",
red = "red",
blue = "blue"
}`);
});

test("name from path", () => {
expect(
astToString(
Expand Down
Loading