Skip to content

Commit 0887fd4

Browse files
zengjiePerryvw
authored andcommitted
Allow string initializer for enums (#139)
* Allow string initializer for enums * Add more test cases for enums. * Revert original enum test cases.
1 parent 666e7eb commit 0887fd4

File tree

8 files changed

+49
-7
lines changed

8 files changed

+49
-7
lines changed

src/Transpiler.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export abstract class LuaTranspiler {
275275
}
276276

277277
public transpileEnum(node: ts.EnumDeclaration): string {
278-
let val = 0;
278+
let val: number | string = 0;
279279
let result = "";
280280

281281
const type = this.checker.getTypeAtLocation(node);
@@ -287,13 +287,19 @@ export abstract class LuaTranspiler {
287287
result += this.makeExport(name, node);
288288
}
289289

290+
let hasStringInitializers = false;
290291
node.members.forEach(member => {
291292
if (member.initializer) {
292293
if (ts.isNumericLiteral(member.initializer)) {
293294
val = parseInt(member.initializer.text);
295+
} else if (ts.isStringLiteral(member.initializer)) {
296+
hasStringInitializers = true;
297+
val = `"${member.initializer.text}"`;
294298
} else {
295-
throw new TranspileError("Only numeric initializers allowed for enums.", node);
299+
throw new TranspileError("Only numeric or string initializers allowed for enums.", node);
296300
}
301+
} else if (hasStringInitializers) {
302+
throw new TranspileError("Invalid heterogeneous enum.", node);
297303
}
298304

299305
if (membersOnly) {
@@ -306,7 +312,9 @@ export abstract class LuaTranspiler {
306312
result += this.indent + `${defName}=${val}\n`;
307313
}
308314

309-
val++;
315+
if (typeof val === "number") {
316+
val++;
317+
}
310318
});
311319
return result;
312320
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TestEnum={}
2+
TestEnum.val1=0
3+
TestEnum.val2=3
4+
TestEnum.val3="baz"
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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TestEnum={}
2+
TestEnum.val1="foo"
3+
TestEnum.val2="bar"
4+
TestEnum.val3="baz"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum TestEnum {
2+
val1,
3+
val2 = 3,
4+
val3 = "baz",
5+
}

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/translation/ts/enumString.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum TestEnum {
2+
val1 = "foo",
3+
val2 = "bar",
4+
val3 = "baz",
5+
}

test/unit/enum.spec.ts

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

44
export class EnumTests {
5+
@Test("Invalid heterogeneous enum")
6+
public invalidHeterogeneousEnum() {
7+
// Transpile & Assert
8+
Expect(() => {
9+
let lua = util.transpileString(
10+
`enum TestEnum {
11+
a,
12+
b = "ok",
13+
c,
14+
}`
15+
);
16+
}).toThrowError(Error, "Invalid heterogeneous enum.");
17+
}
18+
519
@Test("Unsuported enum")
620
public unsuportedEnum() {
721
// Transpile & Assert
822
Expect(() => {
923
let lua = util.transpileString(
1024
`enum TestEnum {
11-
val1 = "test",
25+
val1 = [],
1226
val2 = "ok",
1327
val3 = "bye"
1428
}`
1529
);
16-
}).toThrowError(Error, "Only numeric initializers allowed for enums.");
30+
}).toThrowError(Error, "Only numeric or string initializers allowed for enums.");
1731
}
1832
}

0 commit comments

Comments
 (0)