Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export abstract class LuaTranspiler {
}

public transpileEnum(node: ts.EnumDeclaration): string {
let val = 0;
let val: number | string = 0;
let result = "";

const type = this.checker.getTypeAtLocation(node);
Expand All @@ -294,13 +294,19 @@ export abstract class LuaTranspiler {
result += this.makeExport(name, node);
}

let hasStringInitializers = false;
node.members.forEach(member => {
if (member.initializer) {
if (ts.isNumericLiteral(member.initializer)) {
val = parseInt(member.initializer.text);
} else if (ts.isStringLiteral(member.initializer)) {
hasStringInitializers = true;
val = `"${member.initializer.text}"`;
} else {
throw new TranspileError("Only numeric initializers allowed for enums.", node);
throw new TranspileError("Only numeric or string initializers allowed for enums.", node);
}
} else if (hasStringInitializers) {
throw new TranspileError("Invalid heterogeneous enum.", node);
}

if (membersOnly) {
Expand All @@ -313,7 +319,9 @@ export abstract class LuaTranspiler {
result += this.indent + `${defName}=${val}\n`;
}

val++;
if (typeof val === "number") {
val++;
}
});
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions test/translation/lua/enumHeterogeneous.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TestEnum={}
TestEnum.val1=0
TestEnum.val2=3
TestEnum.val3="baz"
1 change: 1 addition & 0 deletions test/translation/lua/enumMembersOnly.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
val1=0
val2=2
val3=3
val4="bye"
local a = val1
4 changes: 4 additions & 0 deletions test/translation/lua/enumString.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TestEnum={}
TestEnum.val1="foo"
TestEnum.val2="bar"
TestEnum.val3="baz"
5 changes: 5 additions & 0 deletions test/translation/ts/enumHeterogeneous.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum TestEnum {
val1,
val2 = 3,
val3 = "baz",
}
5 changes: 3 additions & 2 deletions test/translation/ts/enumMembersOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
enum TestEnum {
val1 = 0,
val2 = 2,
val3
val3,
val4 = "bye"
}

const a = TestEnum.val1;
const a = TestEnum.val1;
5 changes: 5 additions & 0 deletions test/translation/ts/enumString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum TestEnum {
val1 = "foo",
val2 = "bar",
val3 = "baz",
}
18 changes: 16 additions & 2 deletions test/unit/enum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@ import { Expect, Test, TestCase } from "alsatian";
import * as util from "../src/util"

export class EnumTests {
@Test("Invalid heterogeneous enum")
public invalidHeterogeneousEnum() {
// Transpile & Assert
Expect(() => {
let lua = util.transpileString(
`enum TestEnum {
a,
b = "ok",
c,
}`
);
}).toThrowError(Error, "Invalid heterogeneous enum.");
}

@Test("Unsuported enum")
public unsuportedEnum() {
// Transpile & Assert
Expect(() => {
let lua = util.transpileString(
`enum TestEnum {
val1 = "test",
val1 = [],
val2 = "ok",
val3 = "bye"
}`
);
}).toThrowError(Error, "Only numeric initializers allowed for enums.");
}).toThrowError(Error, "Only numeric or string initializers allowed for enums.");
}
}