X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3331,7 +3331,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));
Expand Down Expand Up @@ -4687,12 +4693,16 @@ export class LuaTransformer {
}

protected 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);
}

protected isSymbolExported(symbol: ts.Symbol): boolean {
if (!this.isModule && !this.currentNamespace) {
return false;
}

Expand All @@ -4701,9 +4711,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;
Expand All @@ -4713,8 +4724,8 @@ export class LuaTransformer {
const it: Iterable<ts.Symbol> = {
[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;
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/objectLiteral.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
X Tutup