Skip to content

Commit 5467e7d

Browse files
authored
Merge pull request #55 from Perryvw/feature/alias-calls
Alias call first draft
2 parents ee4dc68 + c68d722 commit 5467e7d

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/Transpiler.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,19 @@ export class LuaTranspiler {
790790
// Include context parameter if present
791791
if (expType && expType.symbol) {
792792
const funcName = node.expression.name.escapedText;
793-
const funcHolder = tsEx.findMemberHolder(expType, funcName, this.checker);
793+
let funcHolder = tsEx.findMemberHolder(expType, funcName, this.checker);
794+
795+
// ===== EXPERIMENTAL https://github.com/Perryvw/TypescriptToLua/issues/56
796+
if (ts.isParenthesizedExpression(node.expression.expression)
797+
&& (ts.isAsExpression(node.expression.expression.expression)
798+
|| ts.isTypeAssertion(node.expression.expression.expression))
799+
&& ts.isTypeReferenceNode(node.expression.expression.expression.type)) {
800+
const castTypeNode = node.expression.expression.expression.type;
801+
if (this.checker.getTypeFromTypeNode(castTypeNode).symbol.name == funcHolder) {
802+
funcHolder = castTypeNode.getText();
803+
}
804+
}
805+
// ===== END EXPERIMENTAL
794806

795807
if (funcHolder === undefined) {
796808
throw new TranspileError(`Could not find func ${funcName} on ${expType.symbol.name}`, node);

test/integration/classInstanceCalls.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@ export class ClassInstanceCallTests {
3434
const lua = util.transpileFile("test/integration/testfiles/classInstanceCall4.ts");
3535
this.ExpectEqualToFile(lua, "test/integration/testfiles/classInstanceCall4.lua");
3636
}
37+
38+
@Test("aliasCastCall")
39+
public aliasCastCall() {
40+
const lua = util.transpileFile("test/integration/testfiles/aliasCastCall.ts");
41+
this.ExpectEqualToFile(lua, "test/integration/testfiles/aliasCall.lua");
42+
}
43+
44+
@Test("aliasAsCall")
45+
public aliasAsCall() {
46+
const lua = util.transpileFile("test/integration/testfiles/aliasAsCall.ts");
47+
this.ExpectEqualToFile(lua, "test/integration/testfiles/aliasCall.lua");
48+
}
3749
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare class ClassA {
2+
myFunc();
3+
}
4+
declare type MyAlias = ClassA;
5+
declare var x: ClassA;
6+
7+
x.myFunc();
8+
(x as MyAlias).myFunc();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ClassA.myFunc(x)
2+
MyAlias.myFunc((x))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare class ClassA {
2+
myFunc();
3+
}
4+
declare type MyAlias = ClassA;
5+
declare var x: ClassA;
6+
7+
x.myFunc();
8+
(<MyAlias>x).myFunc();

0 commit comments

Comments
 (0)