Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
}
Expand All @@ -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})`;
Expand Down
6 changes: 6 additions & 0 deletions test/translation/lua/dotColonFunctionCalls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
classInstance:colonMethod()
classInstance.dotMethod()
interfaceInstance:colonMethod()
interfaceInstance.dotMethod()
TestNameSpace.dotMethod()
TestNameSpace.dotMethod2()
24 changes: 24 additions & 0 deletions test/translation/ts/dotColonFunctionCalls.ts
Original file line number Diff line number Diff line change
@@ -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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how much sense it makes but maybe add some functional tests too. Probably not that important.

2 changes: 1 addition & 1 deletion test/unit/assignments.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
98 changes: 98 additions & 0 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ====================================
Expand Down