Skip to content

Commit daa904f

Browse files
hazzard993Perryvw
authored andcommitted
Console function calls (#454)
* Added console call expression translations * Added console translation tests * Fixed tests * Formatting and readability changes * Formatting change to stringFormatCall
1 parent 5bb7a0b commit daa904f

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

src/LuaTransformer.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,6 +3148,10 @@ export class LuaTransformer {
31483148
return this.transformObjectCallExpression(node);
31493149
}
31503150

3151+
if (ownerType.symbol && ownerType.symbol.escapedName === "Console") {
3152+
return this.transformConsoleCallExpression(node);
3153+
}
3154+
31513155
if (ownerType.symbol && ownerType.symbol.escapedName === "SymbolConstructor") {
31523156
return this.transformSymbolCallExpression(node);
31533157
}
@@ -3676,6 +3680,97 @@ export class LuaTransformer {
36763680
}
36773681
}
36783682

3683+
public transformConsoleCallExpression(expression: ts.CallExpression): ExpressionVisitResult {
3684+
const method = expression.expression as ts.PropertyAccessExpression;
3685+
const methodName = method.name.escapedText;
3686+
3687+
switch (methodName) {
3688+
case "log":
3689+
if (expression.arguments.length > 0
3690+
&& this.isStringFormatTemplate(expression.arguments[0])) {
3691+
// print(string.format([arguments]))
3692+
const stringFormatCall = tstl.createCallExpression(
3693+
tstl.createTableIndexExpression(
3694+
tstl.createIdentifier("string"),
3695+
tstl.createStringLiteral("format")),
3696+
this.transformArguments(expression.arguments)
3697+
);
3698+
return tstl.createCallExpression(
3699+
tstl.createIdentifier("print"),
3700+
[stringFormatCall]
3701+
);
3702+
}
3703+
// print([arguments])
3704+
return tstl.createCallExpression(
3705+
tstl.createIdentifier("print"),
3706+
this.transformArguments(expression.arguments)
3707+
);
3708+
case "assert":
3709+
const args = this.transformArguments(expression.arguments);
3710+
if (expression.arguments.length > 1
3711+
&& this.isStringFormatTemplate(expression.arguments[1])) {
3712+
// assert([condition], string.format([arguments]))
3713+
const stringFormatCall = tstl.createCallExpression(
3714+
tstl.createTableIndexExpression(
3715+
tstl.createIdentifier("string"),
3716+
tstl.createStringLiteral("format")),
3717+
args.slice(1)
3718+
);
3719+
return tstl.createCallExpression(
3720+
tstl.createIdentifier("assert"),
3721+
[args[0], stringFormatCall]
3722+
);
3723+
}
3724+
// assert()
3725+
return tstl.createCallExpression(
3726+
tstl.createIdentifier("assert"),
3727+
args
3728+
);
3729+
case "trace":
3730+
if (expression.arguments.length > 0
3731+
&& this.isStringFormatTemplate(expression.arguments[0])) {
3732+
// print(debug.traceback(string.format([arguments])))
3733+
const stringFormatCall = tstl.createCallExpression(
3734+
tstl.createTableIndexExpression(
3735+
tstl.createIdentifier("string"),
3736+
tstl.createStringLiteral("format")),
3737+
this.transformArguments(expression.arguments)
3738+
);
3739+
const debugTracebackCall = tstl.createCallExpression(
3740+
tstl.createTableIndexExpression(
3741+
tstl.createIdentifier("debug"),
3742+
tstl.createStringLiteral("traceback")),
3743+
[stringFormatCall]
3744+
);
3745+
return tstl.createCallExpression(
3746+
tstl.createIdentifier("print"),
3747+
[debugTracebackCall]
3748+
);
3749+
}
3750+
// print(debug.traceback([arguments])))
3751+
const debugTracebackCall = tstl.createCallExpression(
3752+
tstl.createTableIndexExpression(
3753+
tstl.createIdentifier("debug"),
3754+
tstl.createStringLiteral("traceback")),
3755+
this.transformArguments(expression.arguments)
3756+
);
3757+
return tstl.createCallExpression(
3758+
tstl.createIdentifier("print"),
3759+
[debugTracebackCall]
3760+
);
3761+
default:
3762+
throw TSTLErrors.UnsupportedForTarget(
3763+
`console property ${methodName}`,
3764+
this.options.luaTarget,
3765+
expression
3766+
);
3767+
}
3768+
}
3769+
3770+
private isStringFormatTemplate(expression: ts.Expression): boolean {
3771+
return ts.isStringLiteral(expression) && expression.text.match(/\%/g) !== null;
3772+
}
3773+
36793774
// Transpile a Symbol._ property
36803775
public transformSymbolCallExpression(expression: ts.CallExpression): tstl.CallExpression {
36813776
const method = expression.expression as ts.PropertyAccessExpression;

test/unit/console.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Expect, Test, TestCase, IgnoreTest, FocusTest } from "alsatian";
2+
import * as util from "../src/util";
3+
4+
export class ConsoleTests {
5+
6+
@TestCase("console.log()", "print();")
7+
@TestCase('console.log("Hello")', 'print("Hello");')
8+
@TestCase('console.log("Hello %s", "there")', 'print(string.format("Hello %s", "there"));')
9+
@TestCase('console.log("Hello %%s", "there")', 'print(string.format("Hello %%s", "there"));')
10+
@TestCase('console.log("Hello", "There")', 'print("Hello", "There");')
11+
@Test("console.log")
12+
public testConsoleLog(inp: string, expected: string): void {
13+
// Transpile
14+
const lua = util.transpileString(inp);
15+
16+
// Assert
17+
Expect(lua).toBe(expected);
18+
}
19+
20+
@TestCase("console.trace()", "print(debug.traceback());")
21+
@TestCase('console.trace("message")', 'print(debug.traceback("message"));')
22+
@TestCase('console.trace("Hello %s", "there")', 'print(debug.traceback(string.format("Hello %s", "there")));')
23+
@TestCase('console.trace("Hello %%s", "there")', 'print(debug.traceback(string.format("Hello %%s", "there")));')
24+
@TestCase('console.trace("Hello", "there")', 'print(debug.traceback("Hello", "there"));')
25+
@Test("console.trace")
26+
public testConsoleTrace(inp: string, expected: string): void {
27+
// Transpile
28+
const lua = util.transpileString(inp);
29+
30+
// Assert
31+
Expect(lua).toBe(expected);
32+
}
33+
34+
@TestCase("console.assert(false)", "assert(false);")
35+
@TestCase('console.assert(false, "message")', 'assert(false, "message");')
36+
@TestCase('console.assert(false, "message %s", "info")', 'assert(false, string.format("message %s", "info"));')
37+
@TestCase('console.assert(false, "message %%s", "info")', 'assert(false, string.format("message %%s", "info"));')
38+
@TestCase('console.assert(false, "message", "more")', 'assert(false, "message", "more");')
39+
@Test("console.assert")
40+
public testConsoleAssert(inp: string, expected: string): void {
41+
// Transpile
42+
const lua = util.transpileString(inp);
43+
44+
// Assert
45+
Expect(lua).toBe(expected);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)