diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 07b610cb5..74c1b1ddc 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -1767,6 +1767,8 @@ export abstract class LuaTranspiler { if (ts.isPropertyAssignment(element)) { const expression = this.transpileExpression(element.initializer); properties.push(`${name} = ${expression}`); + } else if (ts.isShorthandPropertyAssignment(element)) { + properties.push(`${name} = ${name}`); } else { throw TSTLErrors.UnsupportedKind("object literal element", element.kind, node); } diff --git a/test/translation/lua/shorthandPropertyAssignment.lua b/test/translation/lua/shorthandPropertyAssignment.lua new file mode 100644 index 000000000..61f501c56 --- /dev/null +++ b/test/translation/lua/shorthandPropertyAssignment.lua @@ -0,0 +1 @@ +local f = function(x) return ({x = x}) end diff --git a/test/translation/ts/shorthandPropertyAssignment.ts b/test/translation/ts/shorthandPropertyAssignment.ts new file mode 100644 index 000000000..4b2aa7e82 --- /dev/null +++ b/test/translation/ts/shorthandPropertyAssignment.ts @@ -0,0 +1 @@ +const f = x => ({ x }); diff --git a/test/unit/objectLiteral.spec.ts b/test/unit/objectLiteral.spec.ts index bad44c6d3..823a998d4 100644 --- a/test/unit/objectLiteral.spec.ts +++ b/test/unit/objectLiteral.spec.ts @@ -10,9 +10,18 @@ export class ObjectLiteralTests { @TestCase(`{["a"]:3,b:"4"}`, `{["a"] = 3,b = "4"}`) @TestCase(`{["a"+123]:3,b:"4"}`, `{["a" .. 123] = 3,b = "4"}`) @TestCase(`{[myFunc()]:3,b:"4"}`, `{[myFunc()] = 3,b = "4"}`) + @TestCase(`{x}`, `{x = x}`) @Test("Object Literal") public objectLiteral(inp: string, out: string) { var lua = util.transpileString(`const myvar = ${inp};`) Expect(lua).toBe(`local myvar = ${out}`); } + + @TestCase("3", 3) + @Test("Shorthand Property Assignment") + public ShorthandPropertyAssignment(input: string, expected: number): void { + const lua = util.transpileString(`const x = ${input}; const o = {x}; return o.x;`); + const result = util.executeLua(lua); + Expect(result).toBe(expected); + } }