From 7ec141e3d7c48bd4d97d8409a113433d95f17636 Mon Sep 17 00:00:00 2001 From: Tom <26638278+tomblind@users.noreply.github.com> Date: Sat, 4 May 2019 08:27:33 -0600 Subject: [PATCH] fixed property shorthand with exported identifier fixes #560 --- src/LuaTransformer.ts | 29 ++++++++++++++++++++--------- test/unit/objectLiteral.spec.ts | 9 +++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index ead94c6dc..0add397d2 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3320,7 +3320,13 @@ export class LuaTransformer { const expression = this.expectExpression(this.transformExpression(element.initializer)); properties.push(tstl.createTableFieldExpression(expression, name, element)); } else if (ts.isShorthandPropertyAssignment(element)) { - const identifier = this.transformIdentifierExpression(element.name); + let identifier = this.transformIdentifierExpression(element.name); + if (tstl.isIdentifier(identifier)) { + const valueSymbol = this.checker.getShorthandAssignmentValueSymbol(element); + if (valueSymbol !== undefined && this.isSymbolExported(valueSymbol)) { + identifier = this.createExportedIdentifier(identifier); + } + } properties.push(tstl.createTableFieldExpression(identifier, name, element)); } else if (ts.isMethodDeclaration(element)) { const expression = this.expectExpression(this.transformFunctionExpression(element)); @@ -4676,12 +4682,16 @@ export class LuaTransformer { } private isIdentifierExported(identifier: tstl.Identifier): boolean { - if (!this.isModule && !this.currentNamespace) { + const symbolInfo = identifier.symbolId && this.symbolInfo.get(identifier.symbolId); + if (!symbolInfo) { return false; } - const symbolInfo = identifier.symbolId && this.symbolInfo.get(identifier.symbolId); - if (!symbolInfo) { + return this.isSymbolExported(symbolInfo.symbol); + } + + private isSymbolExported(symbol: ts.Symbol): boolean { + if (!this.isModule && !this.currentNamespace) { return false; } @@ -4690,9 +4700,10 @@ export class LuaTransformer { throw TSTLErrors.UndefinedScope(); } - const scopeSymbol = this.checker.getSymbolAtLocation(currentScope) - ? this.checker.getSymbolAtLocation(currentScope) - : this.checker.getTypeAtLocation(currentScope).getSymbol(); + let scopeSymbol = this.checker.getSymbolAtLocation(currentScope); + if (scopeSymbol === undefined) { + scopeSymbol = this.checker.getTypeAtLocation(currentScope).getSymbol(); + } if (scopeSymbol === undefined || scopeSymbol.exports === undefined) { return false; @@ -4702,8 +4713,8 @@ export class LuaTransformer { const it: Iterable = { [Symbol.iterator]: () => scopeSymbolExports.values(), // Why isn't ts.SymbolTable.values() iterable? }; - for (const symbol of it) { - if (symbol === symbolInfo.symbol) { + for (const exportedSymbol of it) { + if (exportedSymbol === symbol) { return true; } } diff --git a/test/unit/objectLiteral.spec.ts b/test/unit/objectLiteral.spec.ts index ed31245a7..9c1be2618 100644 --- a/test/unit/objectLiteral.spec.ts +++ b/test/unit/objectLiteral.spec.ts @@ -29,6 +29,15 @@ describe("property shorthand", () => { expect(result).toBe(identifier); }); + + test("should support export property shorthand", () => { + const code = ` + export const x = 1; + const o = { x }; + export const y = o.x; + `; + expect(util.transpileExecuteAndReturnExport(code, "y")).toBe(1); + }); }); test("undefined as object key", () => {