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
11 changes: 11 additions & 0 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ export class TSHelper {
return "unknown";
}

// Breaks down a mask into all flag names.
static enumNames(mask, haystack) {
let result = [mask];
for (var name in haystack) {
if ((mask & haystack[name]) != 0 && mask >= haystack[name]) {
result.push(name);
}
}
return result;
}

static containsStatement(statements: ts.NodeArray<ts.Statement>, kind: ts.SyntaxKind): boolean {
return statements.some(statement => statement.kind === kind);
}
Expand Down
39 changes: 7 additions & 32 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,6 @@ export class LuaTranspiler {
if (ts.isPropertyAccessExpression(node.expression)) {
const expType = this.checker.getTypeAtLocation(node.expression.expression);

// Don't include instance if the type is a namespace
if (expType.symbol && expType.symbol.flags & ts.SymbolFlags.Namespace) {
const callPath = this.transpileExpression(node.expression);
const params = this.transpileArguments(node.arguments);
return `${callPath}(${params})`;
}

if (expType.symbol && expType.symbol.escapedName == "Math") {
const params = this.transpileArguments(node.arguments);
return this.transpileMathExpression(node.expression.name) + `(${params})`;
Expand All @@ -794,33 +787,15 @@ export class LuaTranspiler {
return this.transpileArrayCallExpression(node);
}

// Include context parameter if present
if (expType && expType.symbol) {
const funcName = node.expression.name.escapedText;
let funcHolder = tsEx.findMemberHolder(expType, funcName, this.checker);

// ===== EXPERIMENTAL https://github.com/Perryvw/TypescriptToLua/issues/56
if (ts.isParenthesizedExpression(node.expression.expression)
&& (ts.isAsExpression(node.expression.expression.expression)
|| ts.isTypeAssertion(node.expression.expression.expression))
&& ts.isTypeReferenceNode(node.expression.expression.expression.type)) {
const castTypeNode = node.expression.expression.expression.type;
if (this.checker.getTypeFromTypeNode(castTypeNode).symbol.name == funcHolder) {
funcHolder = castTypeNode.getText();
}
}
// ===== END EXPERIMENTAL

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

const callPath = `${funcHolder}.${funcName}`;
const params = this.transpileArguments(node.arguments, node.expression.expression);
if (expType.symbol && (expType.symbol.flags & ts.SymbolFlags.Namespace)) {
// Don't replace . with : for namespaces
const callPath = this.transpileExpression(node.expression);
const params = this.transpileArguments(node.arguments);
return `${callPath}(${params})`;
} else {
const callPath = this.transpileExpression(node.expression);
const params = this.transpileArguments(node.arguments, node.expression.expression);
// Replace last . with : here
const callPath = `${this.transpileExpression(node.expression.expression)}:${node.expression.name.escapedText}`;
const params = this.transpileArguments(node.arguments);
return `${callPath}(${params})`;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/classInstanceCalls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ClassInstanceCallTests {
this.ExpectEqualToFile(lua, "test/integration/testfiles/classInstanceCall4.lua");
}

@Test("aliasCastCall")
/*@Test("aliasCastCall")
public aliasCastCall() {
const lua = util.transpileFile("test/integration/testfiles/aliasCastCall.ts");
this.ExpectEqualToFile(lua, "test/integration/testfiles/aliasCall.lua");
Expand All @@ -45,5 +45,5 @@ export class ClassInstanceCallTests {
public aliasAsCall() {
const lua = util.transpileFile("test/integration/testfiles/aliasAsCall.ts");
this.ExpectEqualToFile(lua, "test/integration/testfiles/aliasCall.lua");
}
}*/
}
2 changes: 1 addition & 1 deletion test/integration/testfiles/classInstanceCall.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
local x = ClassA.myFunc(ClassB.new(true))
local x = ClassB.new(true):myFunc()
2 changes: 1 addition & 1 deletion test/integration/testfiles/classInstanceCall4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ function ClassB.new(construct, ...)
if construct and ClassB.constructor then ClassB.constructor(instance, ...) end
return instance
end
local x = ClassA.myFunc(ClassB.new(true))
local x = ClassB.new(true):myFunc()