diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 1bfb9d093..5381527c7 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -1861,6 +1861,9 @@ export abstract class LuaTranspiler { properties.push(`${name} = ${expression}`); } else if (ts.isShorthandPropertyAssignment(element)) { properties.push(`${name} = ${name}`); + } else if (ts.isMethodDeclaration(element)) { + const expression = this.transpileFunctionExpression(element); + properties.push(`${name} = ${expression}`); } else { throw TSTLErrors.UnsupportedKind("object literal element", element.kind, node); } @@ -1869,9 +1872,12 @@ export abstract class LuaTranspiler { return "{" + properties.join(",") + "}"; } - public transpileFunctionExpression(node: ts.ArrowFunction): string { + public transpileFunctionExpression(node: ts.FunctionLikeDeclaration): string { // Build parameter string const paramNames: string[] = []; + if (ts.isMethodDeclaration(node)) { + paramNames.push("self"); + } node.parameters.forEach(param => { paramNames.push(this.transpileIdentifier(param.name as ts.Identifier)); }); diff --git a/test/unit/functions.spec.ts b/test/unit/functions.spec.ts index 15b6462a2..f7209e0d3 100644 --- a/test/unit/functions.spec.ts +++ b/test/unit/functions.spec.ts @@ -282,4 +282,11 @@ export class FunctionTests { Expect(result).toBe("function"); } + + @Test("Object method declaration") + public objectMethodDeclaration(): void { + const result = util.transpileAndExecute( + `let o = { v: 4, m(i: number): number { return this.v * i; } }; return o.m(3);`); + Expect(result).toBe(12); + } }