Skip to content

Commit 8c6f4ef

Browse files
gakadaPerryvw
authored andcommitted
Support shorthand property assignment in object literals (#183)
1 parent f62835e commit 8c6f4ef

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

src/Transpiler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,8 @@ export abstract class LuaTranspiler {
17671767
if (ts.isPropertyAssignment(element)) {
17681768
const expression = this.transpileExpression(element.initializer);
17691769
properties.push(`${name} = ${expression}`);
1770+
} else if (ts.isShorthandPropertyAssignment(element)) {
1771+
properties.push(`${name} = ${name}`);
17701772
} else {
17711773
throw TSTLErrors.UnsupportedKind("object literal element", element.kind, node);
17721774
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local f = function(x) return ({x = x}) end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const f = x => ({ x });

test/unit/objectLiteral.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ export class ObjectLiteralTests {
1010
@TestCase(`{["a"]:3,b:"4"}`, `{["a"] = 3,b = "4"}`)
1111
@TestCase(`{["a"+123]:3,b:"4"}`, `{["a" .. 123] = 3,b = "4"}`)
1212
@TestCase(`{[myFunc()]:3,b:"4"}`, `{[myFunc()] = 3,b = "4"}`)
13+
@TestCase(`{x}`, `{x = x}`)
1314
@Test("Object Literal")
1415
public objectLiteral(inp: string, out: string) {
1516
var lua = util.transpileString(`const myvar = ${inp};`)
1617
Expect(lua).toBe(`local myvar = ${out}`);
1718
}
19+
20+
@TestCase("3", 3)
21+
@Test("Shorthand Property Assignment")
22+
public ShorthandPropertyAssignment(input: string, expected: number): void {
23+
const lua = util.transpileString(`const x = ${input}; const o = {x}; return o.x;`);
24+
const result = util.executeLua(lua);
25+
Expect(result).toBe(expected);
26+
}
1827
}

0 commit comments

Comments
 (0)