From 20e95204660102f37a2b9efcad9f5f4c257c3a79 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 18 Oct 2018 09:12:34 -0600 Subject: [PATCH 1/2] support for object literal method declarations --- src/Transpiler.ts | 8 +++++++- test/unit/functions.spec.ts | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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..491d7b788 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 = { m(i: number): number { return i * i; } }; return o.m(3);`); + Expect(result).toBe(9); + } } From 3c726fe29fd2c77692fc98ab5a80c42221721b1a Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 18 Oct 2018 09:24:07 -0600 Subject: [PATCH 2/2] updated object literal method test to verify this works correctly --- test/unit/functions.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/functions.spec.ts b/test/unit/functions.spec.ts index 491d7b788..f7209e0d3 100644 --- a/test/unit/functions.spec.ts +++ b/test/unit/functions.spec.ts @@ -286,7 +286,7 @@ export class FunctionTests { @Test("Object method declaration") public objectMethodDeclaration(): void { const result = util.transpileAndExecute( - `let o = { m(i: number): number { return i * i; } }; return o.m(3);`); - Expect(result).toBe(9); + `let o = { v: 4, m(i: number): number { return this.v * i; } }; return o.m(3);`); + Expect(result).toBe(12); } }