Skip to content

Commit 6da78c5

Browse files
authored
Merge pull request #111 from Perryvw/feature/dot-calls
Added the ability to add dot methods by specifying as lambda properties.
2 parents e4b718b + 2b0f59b commit 6da78c5

4 files changed

Lines changed: 139 additions & 6 deletions

File tree

src/Transpiler.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,10 @@ export class LuaTranspiler {
904904
let params;
905905
let callPath;
906906
if (ts.isPropertyAccessExpression(node.expression)) {
907-
const expType = this.checker.getTypeAtLocation(node.expression.expression);
907+
// If the function being called is of type owner.func, get the type of owner
908+
const ownerType = this.checker.getTypeAtLocation(node.expression.expression);
908909

909-
if (expType.symbol && expType.symbol.escapedName === "Math") {
910+
if (ownerType.symbol && ownerType.symbol.escapedName === "Math") {
910911
params = this.transpileArguments(node.arguments);
911912
return this.transpileMathExpression(node.expression.name) + `(${params})`;
912913
}
@@ -916,18 +917,22 @@ export class LuaTranspiler {
916917
return this.transpileStringExpression(node.expression.name) + `(${params})`;
917918
}
918919

919-
switch (expType.flags) {
920+
switch (ownerType.flags) {
920921
case ts.TypeFlags.String:
921922
case ts.TypeFlags.StringLiteral:
922923
return this.transpileStringCallExpression(node);
923924

924925
}
925-
if (tsHelper.isArrayType(expType, this.checker)) {
926+
if (tsHelper.isArrayType(ownerType, this.checker)) {
926927
return this.transpileArrayCallExpression(node);
927928
}
928929

929-
if (expType.symbol && (expType.symbol.flags & ts.SymbolFlags.Namespace)) {
930-
// Don't replace . with : for namespaces
930+
// Get the type of the function
931+
const functionType = this.checker.getTypeAtLocation(node.expression);
932+
// Don't replace . with : for namespaces
933+
if ((ownerType.symbol && (ownerType.symbol.flags & ts.SymbolFlags.Namespace))
934+
// If function is defined as property with lambda type use . instead of :
935+
|| (functionType.symbol && (functionType.symbol.flags & ts.SymbolFlags.TypeLiteral))) {
931936
callPath = this.transpileExpression(node.expression);
932937
params = this.transpileArguments(node.arguments);
933938
return `${callPath}(${params})`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
classInstance:colonMethod()
2+
classInstance.dotMethod()
3+
interfaceInstance:colonMethod()
4+
interfaceInstance.dotMethod()
5+
TestNameSpace.dotMethod()
6+
TestNameSpace.dotMethod2()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
declare class TestClass {
2+
public dotMethod: () => void;
3+
public colonMethod(): void;
4+
}
5+
6+
declare interface TestInterface {
7+
dotMethod: () => void;
8+
colonMethod(): void;
9+
}
10+
11+
declare namespace TestNameSpace {
12+
var dotMethod: () => void;
13+
function dotMethod2(): void;
14+
}
15+
16+
declare const classInstance: TestClass;
17+
declare const interfaceInstance: TestInterface;
18+
19+
classInstance.colonMethod();
20+
classInstance.dotMethod();
21+
interfaceInstance.colonMethod();
22+
interfaceInstance.dotMethod();
23+
TestNameSpace.dotMethod();
24+
TestNameSpace.dotMethod2();

test/unit/expressions.spec.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,104 @@ export class ExpressionTests {
288288
Expect(result).toBe(expected);
289289
}
290290

291+
@Test("Class method call")
292+
public classMethod() {
293+
const returnValue = 4;
294+
const source = `class TestClass {
295+
public classMethod(): number { return ${returnValue}; }
296+
}
297+
298+
const classInstance = new TestClass();
299+
return classInstance.classMethod();`;
300+
301+
// Transpile
302+
const lua = util.transpileString(source);
303+
304+
// Execute
305+
const result = util.executeLua(lua);
306+
307+
// Assert
308+
Expect(result).toBe(returnValue);
309+
}
310+
311+
@Test("Class dot method call void")
312+
public classDotMethod() {
313+
const returnValue = 4;
314+
const source = `class TestClass {
315+
public dotMethod: () => number = () => ${returnValue};
316+
}
317+
318+
const classInstance = new TestClass();
319+
return classInstance.dotMethod();`;
320+
321+
// Transpile
322+
const lua = util.transpileString(source);
323+
324+
// Execute
325+
const result = util.executeLua(lua);
326+
327+
// Assert
328+
Expect(result).toBe(returnValue);
329+
}
330+
331+
@Test("Class dot method call with parameter")
332+
public classDotMethod2() {
333+
const returnValue = 4;
334+
const source = `class TestClass {
335+
public dotMethod: (x: number) => number = x => 3 * x;
336+
}
337+
338+
const classInstance = new TestClass();
339+
return classInstance.dotMethod(${returnValue});`;
340+
341+
// Transpile
342+
const lua = util.transpileString(source);
343+
344+
// Execute
345+
const result = util.executeLua(lua);
346+
347+
// Assert
348+
Expect(result).toBe(3 * returnValue);
349+
}
350+
351+
@Test("Class static dot method")
352+
public classDotMethodStatic() {
353+
const returnValue = 4;
354+
const source = `class TestClass {
355+
public static dotMethod: () => number = () => ${returnValue};
356+
}
357+
358+
return TestClass.dotMethod();`;
359+
360+
// Transpile
361+
const lua = util.transpileString(source);
362+
363+
// Execute
364+
const result = util.executeLua(lua);
365+
366+
// Assert
367+
Expect(result).toBe(returnValue);
368+
}
369+
370+
@Test("Class static dot method with parameter")
371+
public classDotMethodStaticWithParameter() {
372+
const returnValue = 4;
373+
const source = `class TestClass {
374+
public static dotMethod: (x: number) => number = x => 3 * x;
375+
}
376+
377+
return TestClass.dotMethod(${returnValue});`;
378+
379+
// Transpile
380+
const lua = util.transpileString(source);
381+
382+
// Execute
383+
const result = util.executeLua(lua);
384+
385+
// Assert
386+
Expect(result).toBe(3 * returnValue);
387+
}
388+
291389
// ====================================
292390
// Test expected errors
293391
// ====================================

0 commit comments

Comments
 (0)