Skip to content

Commit deaf455

Browse files
committed
Fixed and improved lualib ternary conditional
1 parent cb5390c commit deaf455

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

src/Transpiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ export abstract class LuaTranspiler {
874874
const val2 = this.transpileExpression(node.whenFalse);
875875

876876
return this.transpileLuaLibFunction(LuaLibFeature.Ternary, condition,
877-
`function() return ${val1} end`, `function() return ${val2} end)`);
877+
`function() return ${val1} end`, `function() return ${val2} end`);
878878
}
879879

880880
public transpilePostfixUnaryExpression(node: ts.PostfixUnaryExpression): string {

src/lualib/Ternary.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function __TS__Ternary(condition: any, cb1: () => void, cb2: () => void) {
1+
function __TS__Ternary<T>(condition: boolean, cb1: () => T, cb2: () => T): T {
22
if (condition) {
3-
cb1();
3+
return cb1();
44
} else {
5-
cb2();
5+
return cb2();
66
}
77
}

test/unit/expressions.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,6 @@ export class ExpressionTests {
181181
Expect(util.transpileString(input)).toBe(lua);
182182
}
183183

184-
@TestCase("1 + a ? 3*a : c", "TS_ITE(1+a,function() return 3*a end,function() return c end)")
185-
@TestCase("a ? b : c", "TS_ITE(a,function() return b end,function() return c end)")
186-
@Test("Ternary operator")
187-
public conditional(input: string, lua: string) {
188-
Expect(util.transpileString(input)).toBe(lua);
189-
}
190-
191184
@Test("Null Expression")
192185
public nullExpression() {
193186
Expect(util.transpileString("null")).toBe("nil");

test/unit/lualib.spec.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Expect, Test, TestCase } from "alsatian";
1+
import { Expect, Test, TestCase, FocusTests } from "alsatian";
22
import * as util from "../src/util";
33

4+
@FocusTests
45
export class LuaLibArrayTests {
56

67
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
@@ -261,4 +262,39 @@ export class LuaLibArrayTests {
261262
// Assert
262263
Expect(result).toBe(JSON.stringify([0].concat(inp)));
263264
}
265+
266+
@TestCase("true", "4", "5", 4)
267+
@TestCase("false", "4", "5", 5)
268+
@TestCase("3", "4", "5", 4)
269+
@Test("Ternary Conditional")
270+
public ternaryConditional(condition: string, lhs: string, rhs: string, expected: any) {
271+
// Transpile
272+
const lua = util.transpileString(`return ${condition} ? ${lhs} : ${rhs};`);
273+
274+
// Execute
275+
const result = util.executeLua(lua);
276+
277+
// Assert
278+
Expect(result).toBe(expected);
279+
}
280+
281+
@TestCase("true", 11)
282+
@TestCase("false", 13)
283+
@TestCase("a < 4", 13)
284+
@TestCase("a == 8", 11)
285+
@Test("Ternary Conditional Delayed")
286+
public ternaryConditionalDelayed(condition: string, expected: any) {
287+
// Transpile
288+
const lua = util.transpileString(
289+
`let a = 3;
290+
let delay = () => ${condition} ? a + 3 : a + 5;
291+
a = 8;
292+
return delay();`);
293+
294+
// Execute
295+
const result = util.executeLua(lua);
296+
297+
// Assert
298+
Expect(result).toBe(expected);
299+
}
264300
}

0 commit comments

Comments
 (0)