Skip to content

Commit f5905b2

Browse files
Rene-SackersffMathy
authored andcommitted
Added useConst to enumEmitOptions (#28)
1 parent 95276c2 commit f5905b2

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

doc/CONFIGURATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ The `EmitOptions` are the root options. They include two properties:
3535

3636
- `filter` **(enumObject: CSharpEnum) => boolean**
3737

38+
- `useConst` **boolean**
39+
3840
- `structEmitOptions` **StructEmitOptions**
3941
configures default struct settings. This settings hierachy is flat.
4042

doc/recipes/OTHER.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,33 @@ The following TypeScript code would be generated:
281281
declare type MyEnum =
282282
'FirstOption' |
283283
'SecondOption'
284+
```
285+
286+
# Declare enums as const
287+
```typescript
288+
var typescriptCode = emitter.emit(<EmitOptions>{
289+
defaults: <DefaultEmitOptions>{
290+
enumEmitOptions: <EnumEmitOptions>{
291+
useConst: true
292+
}
293+
}
294+
});
295+
```
296+
297+
Given the following CSharp model code:
298+
299+
```csharp
300+
public enum MyEnum {
301+
FirstOption,
302+
SecondOption
303+
}
304+
```
305+
306+
The following TypeScript code would be generated:
307+
308+
```typescript
309+
const enum MyEnum {
310+
FirstOption,
311+
SecondOption = 1
312+
}
284313
```

src/Emitter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ export class Emitter {
215215
options.strategy = "default";
216216
}
217217

218+
if (!options.useConst) {
219+
options.useConst = false;
220+
}
221+
218222
return options;
219223
}
220224

src/EnumEmitter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface EnumEmitOptionsBase {
99
declare?: boolean;
1010
strategy?: "default" | "string-union";
1111
filter?: (enumObject: CSharpEnum) => boolean;
12+
useConst?: boolean;
1213
}
1314

1415
export interface EnumEmitOptions extends EnumEmitOptionsBase {
@@ -51,6 +52,9 @@ export class EnumEmitter {
5152
if (options.declare)
5253
modifiers.push(ts.createToken(ts.SyntaxKind.DeclareKeyword));
5354

55+
if (options.useConst)
56+
modifiers.push(ts.createToken(ts.SyntaxKind.ConstKeyword));
57+
5458
var node: ts.Statement;
5559

5660
if (options.strategy === "string-union") {

0 commit comments

Comments
 (0)