diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 3cda3f54e..5bcd49b5c 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -895,9 +895,10 @@ export class LuaTranspiler { let params; let callPath; if (ts.isPropertyAccessExpression(node.expression)) { - const expType = this.checker.getTypeAtLocation(node.expression.expression); + // If the function being called is of type owner.func, get the type of owner + const ownerType = this.checker.getTypeAtLocation(node.expression.expression); - if (expType.symbol && expType.symbol.escapedName === "Math") { + if (ownerType.symbol && ownerType.symbol.escapedName === "Math") { params = this.transpileArguments(node.arguments); return this.transpileMathExpression(node.expression.name) + `(${params})`; } @@ -907,18 +908,22 @@ export class LuaTranspiler { return this.transpileStringExpression(node.expression.name) + `(${params})`; } - switch (expType.flags) { + switch (ownerType.flags) { case ts.TypeFlags.String: case ts.TypeFlags.StringLiteral: return this.transpileStringCallExpression(node); } - if (tsHelper.isArrayType(expType, this.checker)) { + if (tsHelper.isArrayType(ownerType, this.checker)) { return this.transpileArrayCallExpression(node); } - if (expType.symbol && (expType.symbol.flags & ts.SymbolFlags.Namespace)) { - // Don't replace . with : for namespaces + // Get the type of the function + const functionType = this.checker.getTypeAtLocation(node.expression); + // Don't replace . with : for namespaces + if ((ownerType.symbol && (ownerType.symbol.flags & ts.SymbolFlags.Namespace)) + // If function is defined as property with lambda type use . instead of : + || (functionType.symbol && (functionType.symbol.flags & ts.SymbolFlags.TypeLiteral))) { callPath = this.transpileExpression(node.expression); params = this.transpileArguments(node.arguments); return `${callPath}(${params})`; diff --git a/test/translation/lua/dotColonFunctionCalls.lua b/test/translation/lua/dotColonFunctionCalls.lua new file mode 100644 index 000000000..a70d2d0f2 --- /dev/null +++ b/test/translation/lua/dotColonFunctionCalls.lua @@ -0,0 +1,6 @@ +classInstance:colonMethod() +classInstance.dotMethod() +interfaceInstance:colonMethod() +interfaceInstance.dotMethod() +TestNameSpace.dotMethod() +TestNameSpace.dotMethod2() diff --git a/test/translation/ts/dotColonFunctionCalls.ts b/test/translation/ts/dotColonFunctionCalls.ts new file mode 100644 index 000000000..19990cd7a --- /dev/null +++ b/test/translation/ts/dotColonFunctionCalls.ts @@ -0,0 +1,24 @@ +declare class TestClass { + public dotMethod: () => void; + public colonMethod(): void; +} + +declare interface TestInterface { + dotMethod: () => void; + colonMethod(): void; +} + +declare namespace TestNameSpace { + var dotMethod: () => void; + function dotMethod2(): void; +} + +declare const classInstance: TestClass; +declare const interfaceInstance: TestInterface; + +classInstance.colonMethod(); +classInstance.dotMethod(); +interfaceInstance.colonMethod(); +interfaceInstance.dotMethod(); +TestNameSpace.dotMethod(); +TestNameSpace.dotMethod2(); diff --git a/test/unit/assignments.spec.ts b/test/unit/assignments.spec.ts index cfb8d6110..2ee62ea8c 100644 --- a/test/unit/assignments.spec.ts +++ b/test/unit/assignments.spec.ts @@ -1,4 +1,4 @@ -import { Expect, Test, TestCase, FocusTest } from "alsatian"; +import { Expect, Test, TestCase } from "alsatian"; import { TranspileError } from "../../src/Transpiler"; import * as util from "../src/util"; diff --git a/test/unit/expressions.spec.ts b/test/unit/expressions.spec.ts index 311de04d6..95c4ff9cd 100644 --- a/test/unit/expressions.spec.ts +++ b/test/unit/expressions.spec.ts @@ -288,6 +288,104 @@ export class ExpressionTests { Expect(result).toBe(expected); } + @Test("Class method call") + public classMethod() { + const returnValue = 4; + const source = `class TestClass { + public classMethod(): number { return ${returnValue}; } + } + + const classInstance = new TestClass(); + return classInstance.classMethod();`; + + // Transpile + const lua = util.transpileString(source); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(returnValue); + } + + @Test("Class dot method call void") + public classDotMethod() { + const returnValue = 4; + const source = `class TestClass { + public dotMethod: () => number = () => ${returnValue}; + } + + const classInstance = new TestClass(); + return classInstance.dotMethod();`; + + // Transpile + const lua = util.transpileString(source); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(returnValue); + } + + @Test("Class dot method call with parameter") + public classDotMethod2() { + const returnValue = 4; + const source = `class TestClass { + public dotMethod: (x: number) => number = x => 3 * x; + } + + const classInstance = new TestClass(); + return classInstance.dotMethod(${returnValue});`; + + // Transpile + const lua = util.transpileString(source); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(3 * returnValue); + } + + @Test("Class static dot method") + public classDotMethodStatic() { + const returnValue = 4; + const source = `class TestClass { + public static dotMethod: () => number = () => ${returnValue}; + } + + return TestClass.dotMethod();`; + + // Transpile + const lua = util.transpileString(source); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(returnValue); + } + + @Test("Class static dot method with parameter") + public classDotMethodStaticWithParameter() { + const returnValue = 4; + const source = `class TestClass { + public static dotMethod: (x: number) => number = x => 3 * x; + } + + return TestClass.dotMethod(${returnValue});`; + + // Transpile + const lua = util.transpileString(source); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(3 * returnValue); + } + // ==================================== // Test expected errors // ====================================