fix(codegen): generate string literal unions for Enums (#95) #196
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.
Description
This PR updates the TypeScript generator to output String Union Types (e.g.,
type State = 'NY' | 'NJ';) instead of TypeScriptenumdeclarations. This aligns the output with modern TypeScript best practices and resolves issue #95.Changes
TypescriptVisitor: The visitor now generatesexport type <Name> = 'Value1' | 'Value2';syntax for enum declarations.typescriptvisitor.jstest mocks to support the new logic (specifically adding.getName()to property mocks) and verify the correct output format.Fixes
Fixes #95
Note on Snapshots
I verified the logic is correct via local testing (see the diff below), but I was unable to commit the updated
.snapfiles because my local environment is locked in CI mode and refuses to write new snapshots.Verification (npm test output):
As you can see, the generated output (Received) correctly matches the desired String Union syntax:
564 passing (12s) 2 failing 1) codegen #formats check we can convert all formats from namespace versioned CTO, format 'typescript': Error: expect(received).toMatchSnapshot() Snapshot name: `codegen #formats check we can convert all formats from namespace versioned CTO, format 'typescript' 4` - Snapshot - 15 + Received + 5 @@ -15,24 +15,15 @@ export type CategoryUnion = IGeneralCategory; export interface IGeneralCategory extends ICategory { } - export enum State { - MA = 'MA', - NY = 'NY', - CO = 'CO', - WA = 'WA', - IL = 'IL', - CA = 'CA', - } + export type State = + 'MA' | 'NY' | 'CO' | 'WA' | 'IL' | 'CA'; - export enum TShirtSizeType { - SMALL = 'SMALL', - MEDIUM = 'MEDIUM', - LARGE = 'LARGE', - } + export type TShirtSizeType = + 'SMALL' | 'MEDIUM' | 'LARGE'; export type EmployeeTShirtSizes = Map<string, ITShirtSizeType>; export interface IAddress extends IConcept { street: string; @@ -40,10 +31,9 @@ state?: State; zipCode: string; country: string; } - export enum Level { - } + export type Level = never; ", } at /home/axorb_13/accord_project/concerto-codegen/test/codegen/codegen.js:70:41 at Map.forEach (<anonymous>) at Context.<anonymous> (test/codegen/codegen.js:69:23) at process.processImmediate (node:internal/timers:504:21) 2) codegen #formats check we can convert all formats from namespace unversioned CTO, format 'typescript': Error: expect(received).toMatchSnapshot() Snapshot name: `codegen #formats check we can convert all formats from namespace unversioned CTO, format 'typescript' 4` - Snapshot - 15 + Received + 5 @@ -15,24 +15,15 @@ export type CategoryUnion = IGeneralCategory; export interface IGeneralCategory extends ICategory { } - export enum State { - MA = 'MA', - NY = 'NY', - CO = 'CO', - WA = 'WA', - IL = 'IL', - CA = 'CA', - } + export type State = + 'MA' | 'NY' | 'CO' | 'WA' | 'IL' | 'CA'; - export enum TShirtSizeType { - SMALL = 'SMALL', - MEDIUM = 'MEDIUM', - LARGE = 'LARGE', - } + export type TShirtSizeType = + 'SMALL' | 'MEDIUM' | 'LARGE'; export type EmployeeTShirtSizes = Map<string, ITShirtSizeType>; export interface IAddress extends IConcept { street: string; @@ -40,10 +31,9 @@ state?: State; zipCode: string; country: string; } - export enum Level { - } + export type Level = never; ", } at /home/axorb_13/accord_project/concerto-codegen/test/codegen/codegen.js:97:41 at Map.forEach (<anonymous>) at Context.<anonymous> (test/codegen/codegen.js:96:23) at process.processImmediate (node:internal/timers:504:21) =============================== Coverage summary =============================== Statements : 99.16% ( 2985/3010 ) Branches : 96.22% ( 1451/1508 ) Functions : 99.6% ( 507/509 ) Lines : 99.19% ( 2950/2974 ) ================================================================================ [axorb_13@axorb-linux concerto-codegen]$I tried updating the snap files, using different flags and even some modifications in the file:
/concerto-codegen/test/codegen/codegen.jsBut to no avail as of now.