fix: preserve mixed case enum keys with underscores like _123ValCamelCase, Val_Snake_Case, _Val_12_CamelCase
#1558
+168
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes enum key formatting to preserve mixed case names with underscores while still transforming pure lowercase snake_case to PascalCase. This resolves TypeScript compilation errors caused by stripping leading underscores from numeric-prefixed identifiers.
Problem
When generating TypeScript enums from OpenAPI schemas, the formatter was incorrectly transforming enum keys that contained underscores and mixed case (e.g.,
_123ValCamelCase,_Val_12_CamelCase,Val_Snake_Case) into camelCase variants, losing the original casing and underscore structure.Critical Issue: The transformation of
_123ValCamelCaseto123ValCamelCaseproduced invalid TypeScript code that failed to compile, since identifiers cannot start with a digit. The leading underscore is semantically important and must be preserved.The issue occurred because the regex pattern in
type-name-formatter.tsonly checked for all-uppercase constants (/^(?!\d)([A-Z0-9_]{1,})$/g), causing mixed case values to fall through to thelodash.startCasetransformation, which would strip meaningful casing information and create syntax errors.Example Issue
Given this enum schema:
{
"type": "string",
"enum": [
"VAL_1", // SCREAMING_SNAKE_CASE (uppercase only)
"_123ValCamelCase", // Mixed case starting with underscore + digit
"_Val_12_CamelCase", // Mixed case with underscores
"Val_Snake_Case", // Mixed case with underscores
"local_only" // Pure lowercase snake_case
]
}Before:
_123ValCamelCase→123ValCamelCase❌ TypeScript compilation error!_Val_12_CamelCase→Val12CamelCase1❌Val_Snake_Case→ValSnakeCase❌local_only→LocalOnly✅ (correct)After:
_123ValCamelCase→_123ValCamelCase✅ Valid TypeScript_Val_12_CamelCase→_Val_12_CamelCase✅Val_Snake_Case→Val_Snake_Case✅local_only→LocalOnly✅Solution
Added specific logic in
src/type-name-formatter.tsto detect and preserve mixed case enum keys with underscores. The fix distinguishes between:_123ValCamelCase,_Val_12_CamelCase) - now preservedlocal_only) - still transformed to PascalCaseThe solution adds a new condition specifically for
enum-keytypes that checks for:/[A-Z]/.test(name))This prevents unwanted transformation of intentionally mixed case enum keys while maintaining the existing behavior for pure lowercase snake_case, and critically prevents TypeScript syntax errors from removing semantically important leading underscores.
Changes
Core Fix
Test Coverage
_123ValCamelCase(underscore + digit prefix) and addedLowercaseSnakeCaseEnumto test pure lowercase transformationMeta
Testing
All 134 tests pass, including:
_123ValCamelCase→_123ValCamelCase(preventing TS errors)local_only→LocalOnly)Note
Fixes enum key formatting by detecting mixed case (uppercase + lowercase) in enum keys and preserving their original form, preventing TypeScript compilation errors from invalid identifiers like
123ValCamelCase, while maintaining PascalCase transformation for pure lowercase snake_case values.