X Tutup
Skip to content

Commit 512c457

Browse files
tomblindPerryvw
authored andcommitted
fixed property shorthand with exported identifier (#562)
fixes #560
1 parent 1fc5f26 commit 512c457

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/LuaTransformer.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,7 +3331,13 @@ export class LuaTransformer {
33313331
const expression = this.expectExpression(this.transformExpression(element.initializer));
33323332
properties.push(tstl.createTableFieldExpression(expression, name, element));
33333333
} else if (ts.isShorthandPropertyAssignment(element)) {
3334-
const identifier = this.transformIdentifierExpression(element.name);
3334+
let identifier = this.transformIdentifierExpression(element.name);
3335+
if (tstl.isIdentifier(identifier)) {
3336+
const valueSymbol = this.checker.getShorthandAssignmentValueSymbol(element);
3337+
if (valueSymbol !== undefined && this.isSymbolExported(valueSymbol)) {
3338+
identifier = this.createExportedIdentifier(identifier);
3339+
}
3340+
}
33353341
properties.push(tstl.createTableFieldExpression(identifier, name, element));
33363342
} else if (ts.isMethodDeclaration(element)) {
33373343
const expression = this.expectExpression(this.transformFunctionExpression(element));
@@ -4692,12 +4698,16 @@ export class LuaTransformer {
46924698
}
46934699

46944700
protected isIdentifierExported(identifier: tstl.Identifier): boolean {
4695-
if (!this.isModule && !this.currentNamespace) {
4701+
const symbolInfo = identifier.symbolId && this.symbolInfo.get(identifier.symbolId);
4702+
if (!symbolInfo) {
46964703
return false;
46974704
}
46984705

4699-
const symbolInfo = identifier.symbolId && this.symbolInfo.get(identifier.symbolId);
4700-
if (!symbolInfo) {
4706+
return this.isSymbolExported(symbolInfo.symbol);
4707+
}
4708+
4709+
protected isSymbolExported(symbol: ts.Symbol): boolean {
4710+
if (!this.isModule && !this.currentNamespace) {
47014711
return false;
47024712
}
47034713

@@ -4706,9 +4716,10 @@ export class LuaTransformer {
47064716
throw TSTLErrors.UndefinedScope();
47074717
}
47084718

4709-
const scopeSymbol = this.checker.getSymbolAtLocation(currentScope)
4710-
? this.checker.getSymbolAtLocation(currentScope)
4711-
: this.checker.getTypeAtLocation(currentScope).getSymbol();
4719+
let scopeSymbol = this.checker.getSymbolAtLocation(currentScope);
4720+
if (scopeSymbol === undefined) {
4721+
scopeSymbol = this.checker.getTypeAtLocation(currentScope).getSymbol();
4722+
}
47124723

47134724
if (scopeSymbol === undefined || scopeSymbol.exports === undefined) {
47144725
return false;
@@ -4718,8 +4729,8 @@ export class LuaTransformer {
47184729
const it: Iterable<ts.Symbol> = {
47194730
[Symbol.iterator]: () => scopeSymbolExports.values(), // Why isn't ts.SymbolTable.values() iterable?
47204731
};
4721-
for (const symbol of it) {
4722-
if (symbol === symbolInfo.symbol) {
4732+
for (const exportedSymbol of it) {
4733+
if (exportedSymbol === symbol) {
47234734
return true;
47244735
}
47254736
}

test/unit/objectLiteral.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ describe("property shorthand", () => {
2929

3030
expect(result).toBe(identifier);
3131
});
32+
33+
test("should support export property shorthand", () => {
34+
const code = `
35+
export const x = 1;
36+
const o = { x };
37+
export const y = o.x;
38+
`;
39+
expect(util.transpileExecuteAndReturnExport(code, "y")).toBe(1);
40+
});
3241
});
3342

3443
test("undefined as object key", () => {

0 commit comments

Comments
 (0)
X Tutup