Skip to content

Conversation

@rockaxorb13
Copy link

Description

This PR updates the TypeScript generator to output String Union Types (e.g., type State = 'NY' | 'NJ';) instead of TypeScript enum declarations. This aligns the output with modern TypeScript best practices and resolves issue #95.

Changes

  • Modified TypescriptVisitor: The visitor now generates export type <Name> = 'Value1' | 'Value2'; syntax for enum declarations.
  • Updated Unit Tests: Updated typescriptvisitor.js test 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 .snap files 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.js

But to no avail as of now.

Signed-off-by: Aadityavardhan Singh <singhrashmi018@gmail.com>
)

Signed-off-by: Aadityavardhan Singh <singhrashmi018@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript codegen is broken with a model containing enum or scalar

1 participant