Skip to content

Commit 4a0f7c4

Browse files
committed
fix: replace special characters using SPECIAL_CHARACTER_MAP for duplicate-identifiers
1 parent 2a4b067 commit 4a0f7c4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import ts, { type LiteralTypeNode, type TypeLiteralNode } from "typescript";
44
export const JS_PROPERTY_INDEX_RE = /^[A-Za-z_$][A-Za-z_$0-9]*$/;
55
export const JS_ENUM_INVALID_CHARS_RE = /[^A-Za-z_$0-9]+(.)?/g;
66
export const JS_PROPERTY_INDEX_INVALID_CHARS_RE = /[^A-Za-z_$0-9]+/g;
7+
export const SPECIAL_CHARACTER_MAP: Record<string, string> = {
8+
'+': 'Plus',
9+
// Add more mappings as needed
10+
};
11+
712

813
export const BOOLEAN = ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
914
export const FALSE = ts.factory.createLiteralTypeNode(ts.factory.createFalse());
@@ -308,7 +313,7 @@ export function tsEnumMember(value: string | number, metadata: { name?: string;
308313
if (invalidCharMatch[0] === name) {
309314
name = `"${name}"`;
310315
} else {
311-
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_");
316+
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, (substring) => SPECIAL_CHARACTER_MAP[substring] || "_");
312317
}
313318
}
314319
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ describe("tsEnum", () => {
208208
NotFound = 101,
209209
// User doesn't have permissions
210210
PermissionDenied = 102
211+
}`);
212+
});
213+
214+
test("replace special character", () => {
215+
expect(
216+
astToString(tsEnum("SPECIAL_CHARACTER_MAP", ['Etc/GMT+0', "Etc/GMT+1", "Etc/GMT-1"])).trim(),
217+
).toBe(`enum SPECIAL_CHARACTER_MAP {
218+
Etc_GMTPlus0 = "Etc/GMT+0",
219+
Etc_GMTPlus1 = "Etc/GMT+1",
220+
Etc_GMT_1 = "Etc/GMT-1"
211221
}`);
212222
});
213223
});

0 commit comments

Comments
 (0)