Skip to content

Commit 70549d4

Browse files
authored
Revert "[improvement] Generate more lenient enums" (#93)
* Revert "[improvement] Generate more lenient enums (#85)" This reverts commit 3164166. * merge conflicts
1 parent 4635335 commit 70549d4

7 files changed

Lines changed: 91 additions & 50 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export interface IBaz_Str {
2+
'str': string;
3+
'type': "str";
4+
}
5+
6+
export interface IBaz_Date {
7+
'date': string;
8+
'type': "date";
9+
}
10+
11+
function isStr(obj: IBaz): obj is IBaz_Str {
12+
return (obj.type === "str");
13+
}
14+
15+
function str(obj: string): IBaz_Str {
16+
return {
17+
str: obj,
18+
type: "str",
19+
};
20+
}
21+
22+
function isDate(obj: IBaz): obj is IBaz_Date {
23+
return (obj.type === "date");
24+
}
25+
26+
function date(obj: string): IBaz_Date {
27+
return {
28+
date: obj,
29+
type: "date",
30+
};
31+
}
32+
33+
export type IBaz = IBaz_Str | IBaz_Date;
34+
35+
export interface IBazVisitor<T> {
36+
'str': (obj: string) => T;
37+
'date': (obj: string) => T;
38+
'unknown': (obj: IBaz) => T;
39+
}
40+
41+
function visit<T>(obj: IBaz, visitor: IBazVisitor<T>): T {
42+
if (isStr(obj)) {
43+
return visitor.str(obj.str);
44+
}
45+
if (isDate(obj)) {
46+
return visitor.date(obj.date);
47+
}
48+
return visitor.unknown(obj);
49+
}
50+
51+
export const IBaz = {
52+
isStr: isStr,
53+
str: str,
54+
isDate: isDate,
55+
date: date,
56+
visit: visit
57+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as IBaz from "./baz";
2+
3+
export interface IFoo {
4+
'myField': { [key: string]: IBaz.IBaz };
5+
}

src/commands/generate/__tests__/resources/test-cases/example-types/product/enumExample.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* This enumerates the numbers 1:2 also 100.
33
*
44
*/
5-
export type EnumExample = "ONE" | "TWO" | "ONE_HUNDRED";
6-
7-
export const EnumExample = {
8-
ONE: "ONE" as "ONE",
9-
TWO: "TWO" as "TWO",
10-
ONE_HUNDRED: "ONE_HUNDRED" as "ONE_HUNDRED"
11-
};
5+
export enum EnumExample {
6+
ONE = "ONE",
7+
TWO = "TWO",
8+
/**
9+
* Value of 100.
10+
*/
11+
ONE_HUNDRED = "ONE_HUNDRED"
12+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export type SimpleEnum = "VALUE";
2-
3-
export const SimpleEnum = {
4-
VALUE: "VALUE" as "VALUE"
5-
};
1+
export enum SimpleEnum {
2+
VALUE = "VALUE"
3+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
export type EnumExample = "ONE" | "TWO";
2-
3-
export const EnumExample = {
4-
ONE: "ONE" as "ONE",
5-
TWO: "TWO" as "TWO"
6-
};
1+
export enum EnumExample {
2+
ONE = "ONE",
3+
TWO = "TWO"
4+
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* Some documentation
33
*/
4-
export type EnumWithDocs = "FOO" | "BAR";
5-
6-
export const EnumWithDocs = {
7-
FOO: "FOO" as "FOO",
8-
BAR: "BAR" as "BAR"
9-
};
4+
export enum EnumWithDocs {
5+
/**
6+
* Some field documentation
7+
*/
8+
FOO = "FOO",
9+
BAR = "BAR"
10+
}

src/commands/generate/typeGenerator.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,38 +54,19 @@ export function generateType(
5454
* export type EnumExample = "ONE" | "TWO";
5555
* export const EnumExample = { ONE: "ONE" as "ONE", TWO: "TWO" as "TWO" };
5656
* ```
57-
*
58-
* We do not use TypeScript Enums because they can not be assigned to an equivalent enum, making interop across
59-
* libraries more difficult
6057
*/
6158
export async function generateEnum(definition: IEnumDefinition, simpleAst: SimpleAst): Promise<void> {
6259
const sourceFile = simpleAst.createSourceFile(definition.typeName);
6360

64-
const enumType = sourceFile.addTypeAlias({
61+
sourceFile.addEnum({
62+
docs: definition.docs != null ? [{ description: definition.docs }] : undefined,
6563
isExported: true,
64+
members: definition.values.map(({ docs, value }) => ({
65+
docs: docs != null ? [{ description: docs }] : undefined,
66+
name: value,
67+
value,
68+
})),
6669
name: definition.typeName.name,
67-
type: definition.values.map(({ value }) => doubleQuote(value)).join(" | "),
68-
});
69-
if (definition.docs != null) {
70-
enumType.addJsDoc(definition.docs);
71-
}
72-
73-
const variableStatement = sourceFile.addVariableStatement({
74-
declarationKind: VariableDeclarationKind.Const,
75-
declarations: [
76-
{
77-
initializer: "{}",
78-
name: definition.typeName.name,
79-
},
80-
],
81-
isExported: true,
82-
});
83-
const objectLiteralExpr = variableStatement.getDeclarations()[0].getInitializer() as ObjectLiteralExpression;
84-
definition.values.forEach(value => {
85-
objectLiteralExpr.addPropertyAssignment({
86-
initializer: `${doubleQuote(value.value)} as ${doubleQuote(value.value)}`,
87-
name: value.value,
88-
});
8970
});
9071

9172
sourceFile.formatText();

0 commit comments

Comments
 (0)