Skip to content

Commit 8480ba4

Browse files
claude[bot]claude
andauthored
fix: escape enum keys starting with numbers
This fixes an issue where enum keys starting with numbers weren't being properly quoted, resulting in invalid TypeScript code. Now enum keys that start with numbers will be properly quoted in the generated output. Co-authored-by: bcherny <bcherny@users.noreply.github.com> 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 43ba08b commit 8480ba4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ function generateComment(comment?: string, deprecated?: boolean): string {
330330

331331
function generateStandaloneEnum(ast: TEnum, options: Options): string {
332332
const containsSpecialCharacters = (key: string): boolean => /[^a-zA-Z0-9_]/.test(key)
333+
const startsWithNumber = (key: string): boolean => /^[0-9]/.test(key)
333334

334335
return (
335336
(hasComment(ast) ? generateComment(ast.comment, ast.deprecated) + '\n' : '') +
@@ -340,7 +341,7 @@ function generateStandaloneEnum(ast: TEnum, options: Options): string {
340341
ast.params
341342
.map(
342343
({ast, keyName}) =>
343-
(containsSpecialCharacters(keyName) ? `"${keyName}"` : keyName) + ' = ' + generateType(ast, options),
344+
(containsSpecialCharacters(keyName) || startsWithNumber(keyName) ? `"${keyName}"` : keyName) + ' = ' + generateType(ast, options),
344345
)
345346
.join(',\n') +
346347
'\n' +

test/e2e/enum.numeric.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const input = {
2+
title: 'NumericEnum',
3+
type: 'object',
4+
properties: {
5+
numericKeysEnum: {
6+
type: 'string',
7+
enum: ['0bar', '1baz', '2foo']
8+
},
9+
numericValueEnum: {
10+
type: 'number',
11+
enum: [10, 20, 30]
12+
},
13+
mixedKeysEnum: {
14+
type: 'string',
15+
enum: ['abc', '0abc', '1def']
16+
}
17+
},
18+
required: [
19+
'numericKeysEnum',
20+
'numericValueEnum',
21+
'mixedKeysEnum'
22+
],
23+
additionalProperties: false
24+
}

0 commit comments

Comments
 (0)