Skip to content

Commit 7504323

Browse files
committed
Allow string initializer for enums
1 parent 7106690 commit 7504323

6 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/Transpiler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export abstract class LuaTranspiler {
282282
}
283283

284284
public transpileEnum(node: ts.EnumDeclaration): string {
285-
let val = 0;
285+
let val: number | string = 0;
286286
let result = "";
287287

288288
const type = this.checker.getTypeAtLocation(node);
@@ -298,8 +298,10 @@ export abstract class LuaTranspiler {
298298
if (member.initializer) {
299299
if (ts.isNumericLiteral(member.initializer)) {
300300
val = parseInt(member.initializer.text);
301+
} else if (ts.isStringLiteral(member.initializer)) {
302+
val = `"${member.initializer.text}"`;
301303
} else {
302-
throw new TranspileError("Only numeric initializers allowed for enums.", node);
304+
throw new TranspileError("Only numeric or string initializers allowed for enums.", node);
303305
}
304306
}
305307

@@ -313,7 +315,9 @@ export abstract class LuaTranspiler {
313315
result += this.indent + `${defName}=${val}\n`;
314316
}
315317

316-
val++;
318+
if (typeof val === "number") {
319+
val++;
320+
}
317321
});
318322
return result;
319323
}

test/translation/lua/enum.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
TestEnum={}
22
TestEnum.val1=0
33
TestEnum.val2=2
4-
TestEnum.val3=3
4+
TestEnum.val3=3
5+
TestEnum.val4="bye"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
val1=0
22
val2=2
33
val3=3
4+
val4="bye"
45
local a = val1

test/translation/ts/enum.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
enum TestEnum {
22
val1 = 0,
33
val2 = 2,
4-
val3
5-
}
4+
val3,
5+
val4 = "bye"
6+
}

test/translation/ts/enumMembersOnly.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
enum TestEnum {
33
val1 = 0,
44
val2 = 2,
5-
val3
5+
val3,
6+
val4 = "bye"
67
}
78

8-
const a = TestEnum.val1;
9+
const a = TestEnum.val1;

test/unit/enum.spec.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Expect, Test, TestCase } from "alsatian";
22
import * as util from "../src/util"
33

44
export class EnumTests {
5-
@Test("Unsuported enum")
6-
public unsuportedEnum() {
5+
@Test("String enum")
6+
public stringEnum() {
77
// Transpile & Assert
88
Expect(() => {
99
let lua = util.transpileString(
@@ -13,6 +13,20 @@ export class EnumTests {
1313
val3 = "bye"
1414
}`
1515
);
16-
}).toThrowError(Error, "Only numeric initializers allowed for enums.");
17-
}
16+
}).not.toThrow();
17+
}
18+
19+
@Test("Unsuported enum")
20+
public unsuportedEnum() {
21+
// Transpile & Assert
22+
Expect(() => {
23+
let lua = util.transpileString(
24+
`enum TestEnum {
25+
val1 = [],
26+
val2 = "ok",
27+
val3 = "bye"
28+
}`
29+
);
30+
}).toThrowError(Error, "Only numeric or string initializers allowed for enums.");
31+
}
1832
}

0 commit comments

Comments
 (0)