Skip to content

Commit e279922

Browse files
tomblindPerryvw
authored andcommitted
Object literal method declaration support (#249)
* support for object literal method declarations * updated object literal method test to verify this works correctly
1 parent e08753a commit e279922

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Transpiler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,9 @@ export abstract class LuaTranspiler {
18611861
properties.push(`${name} = ${expression}`);
18621862
} else if (ts.isShorthandPropertyAssignment(element)) {
18631863
properties.push(`${name} = ${name}`);
1864+
} else if (ts.isMethodDeclaration(element)) {
1865+
const expression = this.transpileFunctionExpression(element);
1866+
properties.push(`${name} = ${expression}`);
18641867
} else {
18651868
throw TSTLErrors.UnsupportedKind("object literal element", element.kind, node);
18661869
}
@@ -1869,9 +1872,12 @@ export abstract class LuaTranspiler {
18691872
return "{" + properties.join(",") + "}";
18701873
}
18711874

1872-
public transpileFunctionExpression(node: ts.ArrowFunction): string {
1875+
public transpileFunctionExpression(node: ts.FunctionLikeDeclaration): string {
18731876
// Build parameter string
18741877
const paramNames: string[] = [];
1878+
if (ts.isMethodDeclaration(node)) {
1879+
paramNames.push("self");
1880+
}
18751881
node.parameters.forEach(param => {
18761882
paramNames.push(this.transpileIdentifier(param.name as ts.Identifier));
18771883
});

test/unit/functions.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,11 @@ export class FunctionTests {
282282

283283
Expect(result).toBe("function");
284284
}
285+
286+
@Test("Object method declaration")
287+
public objectMethodDeclaration(): void {
288+
const result = util.transpileAndExecute(
289+
`let o = { v: 4, m(i: number): number { return this.v * i; } }; return o.m(3);`);
290+
Expect(result).toBe(12);
291+
}
285292
}

0 commit comments

Comments
 (0)