From c3cf5736f8f340ad3dbd3d8053a5db168659ace2 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 26 Feb 2019 16:07:59 +1000 Subject: [PATCH 1/5] Added console call expression translations --- src/LuaTransformer.ts | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 7676bf2ff..8189c4d0d 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3138,6 +3138,10 @@ export class LuaTransformer { return this.transformObjectCallExpression(node); } + if (ownerType.symbol && ownerType.symbol.escapedName === "Console") { + return this.transformObjectCallExpression(node); + } + if (ownerType.symbol && ownerType.symbol.escapedName === "SymbolConstructor") { return this.transformSymbolCallExpression(node); } @@ -3666,6 +3670,91 @@ export class LuaTransformer { } } + public transformConsoleCallExpression(expression: ts.CallExpression): ExpressionVisitResult { + const method = expression.expression as ts.PropertyAccessExpression; + const methodName = method.name.escapedText; + + switch (methodName) { + case "log": + if (expression.arguments.length > 0) { + if (this.isStringFormatTemplate(expression.arguments[0])) { + // print(string.format([arguments])) + return tstl.createCallExpression( + tstl.createIdentifier("print"), + [tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + this.transformArguments(expression.arguments))] + ); + } + } + // print([arguments]) + return tstl.createCallExpression( + tstl.createIdentifier("print"), + this.transformArguments(expression.arguments) + ); + case "assert": + const args = this.transformArguments(expression.arguments); + if (expression.arguments.length > 1) { + if (this.isStringFormatTemplate(expression.arguments[1])) { + // assert([condition], string.format([arguments])) + return tstl.createCallExpression( + tstl.createIdentifier("assert"), + [args[0], + tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + args.slice(1))] + ); + } + } + // assert() + return tstl.createCallExpression( + tstl.createIdentifier("assert"), + args + ); + case "trace": + if (expression.arguments.length > 0) { + if (this.isStringFormatTemplate(expression.arguments[0])) { + // print(debug.traceback(string.format([arguments]))) + return tstl.createCallExpression( + tstl.createIdentifier("print"), + [tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("debug"), + tstl.createStringLiteral("traceback")), + [tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + this.transformArguments(expression.arguments))])] + ); + } + } + // print(debug.traceback([arguments]))) + return tstl.createCallExpression( + tstl.createIdentifier("print"), + [tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("debug"), + tstl.createStringLiteral("traceback")), + this.transformArguments(expression.arguments))] + ); + default: + throw TSTLErrors.UnsupportedForTarget( + `console property ${methodName}`, + this.options.luaTarget, + expression + ); + } + } + + private isStringFormatTemplate(expression: ts.Expression): boolean { + return ts.isStringLiteral(expression) && expression.text.match(/\%/g) !== null; + } + // Transpile a Symbol._ property public transformSymbolCallExpression(expression: ts.CallExpression): tstl.CallExpression { const method = expression.expression as ts.PropertyAccessExpression; From 5484e4dbbafd8b7b9b0cd607bd33db781f3bc4f7 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 26 Feb 2019 16:08:10 +1000 Subject: [PATCH 2/5] Added console translation tests --- test/unit/console.spec.ts | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/unit/console.spec.ts diff --git a/test/unit/console.spec.ts b/test/unit/console.spec.ts new file mode 100644 index 000000000..b7d0a689e --- /dev/null +++ b/test/unit/console.spec.ts @@ -0,0 +1,48 @@ +import { Expect, Test, TestCase, IgnoreTest, FocusTest } from "alsatian"; +import * as util from "../src/util"; + +export class ConsoleTests { + + @TestCase("console.log()", "print();") + @TestCase('console.log("Hello")', 'print("Hello");') + @TestCase('console.log("Hello %s", "there")', 'print(string.format("Hello %s", "there"));') + @TestCase('console.log("Hello %%s", "there")', 'print(string.format("Hello %%s", "there"));') + @TestCase('console.log("Hello", "There")', 'print("Hello", "There");') + @Test("console.log") + public testConsoleLog(inp: string, expected: string): void { + // Transpile + const lua = util.transpileString(inp); + + // Assert + Expect(lua).toBe(expected); + } + + @TestCase("console.trace()", "print(debug.traceback());") + @TestCase('console.trace("message")', 'print(debug.traceback("message"));') + @TestCase('console.trace("Hello %s", "there")', 'print(debug.traceback(string.format("Hello %s", "there")));') + @TestCase('console.trace("Hello %%s", "there")', 'print(debug.traceback(string.format("Hello %%s", "there")));') + @TestCase('console.trace("Hello", "there")', 'print(debug.traceback("Hello", "there"));') + @Test("console.trace") + public testConsoleTrace(inp: string, expected: string): void { + // Transpile + const lua = util.transpileString(inp); + + // Assert + Expect(lua).toBe(expected); + } + + @TestCase("console.assert(false)", "assert(false);") + @TestCase('console.assert(false, "message")', 'assert(false, "message");') + @TestCase('console.assert(false, "message %s", "info")', 'assert(false, string.format("message %s", "info"));') + @TestCase('console.assert(false, "message %%s", "info")', 'assert(false, string.format("message %%s", "info"));') + @TestCase('console.assert(false, "message", "more")', 'assert(false, "message", "more");') + @Test("console.assert") + public testConsoleAssert(inp: string, expected: string): void { + // Transpile + const lua = util.transpileString(inp); + + // Assert + Expect(lua).toBe(expected); + } + +} \ No newline at end of file From 9c08988f3c9d820d6dcc8ec219c624d118508034 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 26 Feb 2019 16:27:14 +1000 Subject: [PATCH 3/5] Fixed tests --- src/LuaTransformer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 8189c4d0d..ee49d77c1 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3139,7 +3139,7 @@ export class LuaTransformer { } if (ownerType.symbol && ownerType.symbol.escapedName === "Console") { - return this.transformObjectCallExpression(node); + return this.transformConsoleCallExpression(node); } if (ownerType.symbol && ownerType.symbol.escapedName === "SymbolConstructor") { From bd123ae7b1f96427a5436b1a9a4e24dba4c8d938 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Wed, 27 Feb 2019 08:24:03 +1000 Subject: [PATCH 4/5] Formatting and readability changes --- src/LuaTransformer.ts | 96 ++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index ee49d77c1..f39fc1afc 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3676,18 +3676,17 @@ export class LuaTransformer { switch (methodName) { case "log": - if (expression.arguments.length > 0) { - if (this.isStringFormatTemplate(expression.arguments[0])) { - // print(string.format([arguments])) - return tstl.createCallExpression( - tstl.createIdentifier("print"), - [tstl.createCallExpression( - tstl.createTableIndexExpression( - tstl.createIdentifier("string"), - tstl.createStringLiteral("format")), - this.transformArguments(expression.arguments))] - ); - } + if (expression.arguments.length > 0 + && this.isStringFormatTemplate(expression.arguments[0])) { + // print(string.format([arguments])) + return tstl.createCallExpression( + tstl.createIdentifier("print"), + [tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + this.transformArguments(expression.arguments))] + ); } // print([arguments]) return tstl.createCallExpression( @@ -3696,19 +3695,19 @@ export class LuaTransformer { ); case "assert": const args = this.transformArguments(expression.arguments); - if (expression.arguments.length > 1) { - if (this.isStringFormatTemplate(expression.arguments[1])) { - // assert([condition], string.format([arguments])) - return tstl.createCallExpression( - tstl.createIdentifier("assert"), - [args[0], - tstl.createCallExpression( - tstl.createTableIndexExpression( - tstl.createIdentifier("string"), - tstl.createStringLiteral("format")), - args.slice(1))] - ); - } + if (expression.arguments.length > 1 + && this.isStringFormatTemplate(expression.arguments[1])) { + // assert([condition], string.format([arguments])) + const stringFormatCall = tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + args.slice(1) + ); + return tstl.createCallExpression( + tstl.createIdentifier("assert"), + [args[0], stringFormatCall] + ); } // assert() return tstl.createCallExpression( @@ -3716,31 +3715,36 @@ export class LuaTransformer { args ); case "trace": - if (expression.arguments.length > 0) { - if (this.isStringFormatTemplate(expression.arguments[0])) { - // print(debug.traceback(string.format([arguments]))) - return tstl.createCallExpression( - tstl.createIdentifier("print"), - [tstl.createCallExpression( - tstl.createTableIndexExpression( - tstl.createIdentifier("debug"), - tstl.createStringLiteral("traceback")), - [tstl.createCallExpression( - tstl.createTableIndexExpression( - tstl.createIdentifier("string"), - tstl.createStringLiteral("format")), - this.transformArguments(expression.arguments))])] - ); - } + if (expression.arguments.length > 0 + && this.isStringFormatTemplate(expression.arguments[0])) { + // print(debug.traceback(string.format([arguments]))) + const stringFormatCall = tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + this.transformArguments(expression.arguments) + ); + const debugTracebackCall = tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("debug"), + tstl.createStringLiteral("traceback")), + [stringFormatCall] + ); + return tstl.createCallExpression( + tstl.createIdentifier("print"), + [debugTracebackCall] + ); } // print(debug.traceback([arguments]))) + const debugTracebackCall = tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("debug"), + tstl.createStringLiteral("traceback")), + this.transformArguments(expression.arguments) + ); return tstl.createCallExpression( tstl.createIdentifier("print"), - [tstl.createCallExpression( - tstl.createTableIndexExpression( - tstl.createIdentifier("debug"), - tstl.createStringLiteral("traceback")), - this.transformArguments(expression.arguments))] + [debugTracebackCall] ); default: throw TSTLErrors.UnsupportedForTarget( From 9811deedbf03002ba5cc350987eea637d1a5c3ed Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Wed, 27 Feb 2019 08:54:21 +1000 Subject: [PATCH 5/5] Formatting change to stringFormatCall --- src/LuaTransformer.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index f39fc1afc..88f9c3aa4 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3679,13 +3679,15 @@ export class LuaTransformer { if (expression.arguments.length > 0 && this.isStringFormatTemplate(expression.arguments[0])) { // print(string.format([arguments])) + const stringFormatCall = tstl.createCallExpression( + tstl.createTableIndexExpression( + tstl.createIdentifier("string"), + tstl.createStringLiteral("format")), + this.transformArguments(expression.arguments) + ); return tstl.createCallExpression( tstl.createIdentifier("print"), - [tstl.createCallExpression( - tstl.createTableIndexExpression( - tstl.createIdentifier("string"), - tstl.createStringLiteral("format")), - this.transformArguments(expression.arguments))] + [stringFormatCall] ); } // print([arguments])