From dd7b3dd978b0fc7d7b3ce54923ded2c2424ca4d6 Mon Sep 17 00:00:00 2001 From: Tom <26638278+tomblind@users.noreply.github.com> Date: Sun, 24 Feb 2019 07:43:40 -0700 Subject: [PATCH] enabled support for lua string instance methods --- src/LuaTransformer.ts | 29 +++++++++++++++++++++++++++++ test/unit/string.spec.ts | 11 +++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index e04c594da..fc7c81012 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3515,6 +3515,35 @@ export class LuaTransformer { const firstParamPlusOne = this.expressionPlusOne(params[0]); return this.createStringCall("byte", node, caller, firstParamPlusOne); } + case "byte": + case "char": + case "dump": + case "find": + case "format": + case "gmatch": + case "gsub": + case "len": + case "lower": + case "match": + case "pack": + case "packsize": + case "rep": + case "reverse": + case "sub": + case "unpack": + case "upper": + // Allow lua's string instance methods + let stringVariable = this.transformExpression(expression.expression); + if (ts.isStringLiteral(expression.expression)) { + // "foo":method() needs to be ("foo"):method() + stringVariable = tstl.createParenthesizedExpression(stringVariable); + } + return tstl.createMethodCallExpression( + stringVariable, + this.transformIdentifier(expression.name), + params, + node + ); default: throw TSTLErrors.UnsupportedProperty("string", expressionName, node); } diff --git a/test/unit/string.spec.ts b/test/unit/string.spec.ts index b3326ec78..d82fb0b9c 100644 --- a/test/unit/string.spec.ts +++ b/test/unit/string.spec.ts @@ -14,6 +14,17 @@ export class StringTests }).toThrowError(TranspileError, "Unsupported property on string: testThisIsNoMember"); } + @Test("Suported lua string function") + public stringSuportedLuaFunction(): void { + Expect(util.transpileAndExecute( + `return "test".upper()`, + undefined, + undefined, + `interface String { upper(): string; }` + ) + ).toBe("TEST"); + } + @TestCase([]) @TestCase([65]) @TestCase([65, 66])