Skip to content

Commit 9da96cd

Browse files
JeanRemiDelteilJean-Remi Delteil
and
Jean-Remi Delteil
authored
Do not add readonly on enum with the immutable option, (#1602)
Test 'export' modifier on enum Co-authored-by: Jean-Remi Delteil <[email protected]>
1 parent 677e04a commit 9da96cd

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

.changeset/tender-ducks-own.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Do not add readonly on Typescript enum when the --immutable option is used.

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ dist
33
node_modules
44

55
packages/openapi-typescript/test/fixtures/cli-outputs/out
6+
7+
# IntelliJ IDEA settings folder
8+
/.idea

packages/openapi-typescript/src/lib/ts.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export function tsEnum(
230230
name: string,
231231
members: (string | number)[],
232232
metadata?: { name?: string; description?: string }[],
233-
options?: { readonly?: boolean; export?: boolean },
233+
options?: { export?: boolean },
234234
) {
235235
let enumName = name.replace(JS_ENUM_INVALID_CHARS_RE, (c) => {
236236
const last = c[c.length - 1];
@@ -244,10 +244,7 @@ export function tsEnum(
244244
enumName = `${enumName[0].toUpperCase()}${enumName.substring(1)}`;
245245
return ts.factory.createEnumDeclaration(
246246
/* modifiers */ options
247-
? tsModifiers({
248-
readonly: options.readonly ?? false,
249-
export: options.export ?? false,
250-
})
247+
? tsModifiers({ export: options.export ?? false })
251248
: undefined,
252249
/* name */ enumName,
253250
/* members */ members.map((value, i) =>

packages/openapi-typescript/src/transform/schema-object.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ export function transformSchemaObjectWithComposition(
126126
enumName,
127127
schemaObject.enum as (string | number)[],
128128
metadata,
129-
{ export: true, readonly: options.ctx.immutable },
129+
130+
{
131+
export: true,
132+
// readonly: TS enum do not support the readonly modifier
133+
},
130134
);
131135
options.ctx.injectFooter.push(enumType);
132136
return ts.factory.createTypeReferenceNode(enumType.name);

packages/openapi-typescript/test/lib/ts.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ describe("tsEnum", () => {
9898
}`);
9999
});
100100

101+
test("with setting: export", () => {
102+
expect(
103+
astToString(
104+
tsEnum("-my-color-", ["green", "red", "blue"], undefined, {
105+
export: true,
106+
}),
107+
).trim(),
108+
).toBe(`export enum MyColor {
109+
green = "green",
110+
red = "red",
111+
blue = "blue"
112+
}`);
113+
});
114+
101115
test("name from path", () => {
102116
expect(
103117
astToString(

0 commit comments

Comments
 (0)