Skip to content

Commit dc7b07b

Browse files
jcogilviebcherny
authored andcommitted
Add ts enums (#15)
* Adding support for ts enums; added tsEnumNames to schema * Exporting ts enum types if exporting is enabled * Adding internal publication * Improved ts enum support for inline enums * Bumping version; updating tests * Adding file ref test * Undoing publish change * Remove todo that was fixed * v1.2.3: add generated typescript helpers * Adding util class generation for enums * Bug * Commit dist * Fix toStringValue generated method * Lint * Bad quote! Bad! * Adding enum validation/error case tests * Add ability to generate const enums * Fix lint * Moving enum utils; removing setting for using ts enums; using ts enums for int-backed enums and string unions for string-backed * Removing enum utils * Last round of code review feedback * Fixing tests * Updating dist * Default to const enums * Update dist * Remove propagation logic now that EnumValue is only used for int enums * Dist
1 parent 608a91f commit dc7b07b

27 files changed

+750
-191
lines changed

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "0.2.5",
3+
"configurations": [
4+
{
5+
// Name of configuration; appears in the launch configuration drop down menu.
6+
"name": "Run mocha",
7+
// Type of configuration. Possible values: "node", "mono".
8+
"type": "node",
9+
// Workspace relative or absolute path to the program.
10+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
11+
// Automatically stop program after launch.
12+
"stopOnEntry": false,
13+
// Command line arguments passed to the program.
14+
"args": ["out/test/test.js"],
15+
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
16+
"cwd": "${workspaceRoot}",
17+
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
18+
"runtimeExecutable": null,
19+
// Environment variables passed to the program.
20+
"env": { "NODE_ENV": "production"}
21+
}
22+
]
23+
}

dist/index.d.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Generated by dts-bundle v0.5.0
22

3-
export function compile(schema: JSONSchema.Schema, settings?: TsType.TsTypeSettings): string;
3+
export function compile(schema: JSONSchema.Schema, path: string, settings?: TsType.TsTypeSettings): string;
44
export function compileFromFile(inputFilename: string): Promise<string | Error>;
55

66
export namespace JSONSchema {
@@ -42,6 +42,7 @@ export namespace JSONSchema {
4242
[k: string]: HttpJsonSchemaOrgDraft04Schema | string[];
4343
};
4444
enum?: any[];
45+
tsEnumNames?: string[];
4546
type?: SimpleTypes | SimpleTypes[];
4647
allOf?: HttpJsonSchemaOrgDraft04Schema[];
4748
anyOf?: HttpJsonSchemaOrgDraft04Schema[];
@@ -56,17 +57,18 @@ export namespace JSONSchema {
5657

5758
export namespace TsType {
5859
interface TsTypeSettings {
59-
declareSimpleType?: boolean;
60+
declarationDescription?: boolean;
6061
declareReferenced?: boolean;
61-
useFullReferencePathAsName?: boolean;
62-
useInterfaceDeclaration?: boolean;
63-
endTypeWithSemicolon?: boolean;
62+
declareSimpleType?: boolean;
6463
endPropertyWithSemicolon?: boolean;
65-
declarationDescription?: boolean;
64+
endTypeWithSemicolon?: boolean;
6665
propertyDescription?: boolean;
66+
useConstEnums?: boolean;
67+
useFullReferencePathAsName?: boolean;
68+
useInterfaceDeclaration?: boolean;
6769
}
6870
var DEFAULT_SETTINGS: TsTypeSettings;
69-
abstract class TsType {
71+
abstract class TsTypeBase {
7072
id?: string;
7173
description?: string;
7274
protected safeId(): string | undefined;
@@ -82,37 +84,52 @@ export namespace TsType {
8284
interface TsProp {
8385
name: string;
8486
required: boolean;
85-
type: TsType;
87+
type: TsTypeBase;
8688
}
87-
class Any extends TsType {
89+
class Any extends TsTypeBase {
8890
_type(): string;
8991
}
90-
class String extends TsType {
92+
class String extends TsTypeBase {
9193
_type(): string;
9294
}
93-
class Boolean extends TsType {
95+
class Boolean extends TsTypeBase {
9496
_type(): string;
9597
}
96-
class Number extends TsType {
98+
class Number extends TsTypeBase {
9799
_type(): string;
98100
}
99-
class Object extends TsType {
101+
class Object extends TsTypeBase {
100102
_type(): string;
101103
}
102-
class Void extends TsType {
104+
class Void extends TsTypeBase {
103105
_type(): string;
104106
}
105-
class Literal extends TsType {
107+
class Literal extends TsTypeBase {
106108
constructor(value: any);
107109
_type(): any;
108110
}
109-
class Array extends TsType {
110-
constructor(type?: TsType);
111+
class EnumValue {
112+
identifier: string;
113+
value: string;
114+
constructor(enumValues: string[]);
115+
toDeclaration(): string;
116+
toString(): string;
117+
}
118+
class Enum extends TsTypeBase {
119+
enumValues: EnumValue[];
120+
constructor(enumValues: EnumValue[]);
121+
isSimpleType(): boolean;
122+
_type(settings: TsTypeSettings): string;
123+
toSafeType(settings: TsTypeSettings): string;
124+
toDeclaration(settings: TsTypeSettings): string;
125+
}
126+
class Array extends TsTypeBase {
127+
constructor(type?: TsTypeBase);
111128
_type(settings: TsTypeSettings): string;
112129
}
113-
class Intersection extends TsType {
114-
protected data: TsType[];
115-
constructor(data: TsType[]);
130+
class Intersection extends TsTypeBase {
131+
protected data: TsTypeBase[];
132+
constructor(data: TsTypeBase[]);
116133
isSimpleType(): boolean;
117134
_type(settings: TsTypeSettings): string;
118135
toSafeType(settings: TsTypeSettings): string;
@@ -121,7 +138,7 @@ export namespace TsType {
121138
isSimpleType(): boolean;
122139
_type(settings: TsTypeSettings): string;
123140
}
124-
class Interface extends TsType {
141+
class Interface extends TsTypeBase {
125142
constructor(props: TsProp[]);
126143
static reference(id: string): Interface;
127144
protected _type(settings: TsTypeSettings, declaration?: boolean): string;

0 commit comments

Comments
 (0)