Skip to content

Commit f45a53c

Browse files
committed
globalThis to _G
Closes #592
1 parent 46482c7 commit f45a53c

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/LuaTransformer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4630,6 +4630,10 @@ export class LuaTransformer {
46304630
}
46314631

46324632
public transformIdentifier(identifier: ts.Identifier): tstl.Identifier {
4633+
if (identifier.escapedText === "globalThis") {
4634+
return tstl.createIdentifier("_G", identifier, this.getIdentifierSymbolId(identifier));
4635+
}
4636+
46334637
if (identifier.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
46344638
// TODO this is a hack that allows use to keep Identifier
46354639
// as return time as changing that would break a lot of stuff.
@@ -5172,7 +5176,9 @@ export class LuaTransformer {
51725176
protected hasUnsafeSymbolName(symbol: ts.Symbol, tsOriginal?: ts.Identifier): boolean {
51735177
const isLuaKeyword = luaKeywords.has(symbol.name);
51745178
const isInvalidIdentifier = !tsHelper.isValidLuaIdentifier(symbol.name);
5175-
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbient(d));
5179+
// TODO investigate: declarations will be undefined if "globalThis" is passed, could be a TS bug
5180+
// WORKAROUND: added check for symbol.declarations
5181+
const isAmbient = symbol.declarations && symbol.declarations.some(d => tsHelper.isAmbient(d));
51765182
if ((isLuaKeyword || isInvalidIdentifier) && isAmbient) {
51775183
// Catch ambient declarations of identifiers with bad names
51785184
throw TSTLErrors.InvalidAmbientIdentifierName(tsOriginal || ts.createIdentifier(symbol.name));

test/unit/identifiers.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,46 @@ test("exported variable with lua keyword as name is not renamed", () => {
788788

789789
expect(util.transpileExecuteAndReturnExport(code, "print")).toBe("foobar");
790790
});
791+
792+
describe("globalThis translation", () => {
793+
test("globalThis to _G (expression)", () => {
794+
const code = `
795+
var foo = "bar";
796+
return globalThis.foo;`;
797+
798+
const lua = util.transpileString(code);
799+
800+
expect(util.executeLua(lua)).toBe("bar");
801+
});
802+
803+
test("globalThis to _G (assign)", () => {
804+
const code = `
805+
globalThis.foo = "bar";
806+
return globalThis.foo;`;
807+
808+
const lua = util.transpileString(code);
809+
810+
expect(util.executeLua(lua)).toBe("bar");
811+
});
812+
813+
test("globalThis to _G (reassign)", () => {
814+
const code = `
815+
globalThis.foo = "bar";
816+
globalThis.foo = "baz";
817+
return globalThis.foo;`;
818+
819+
const lua = util.transpileString(code);
820+
821+
expect(util.executeLua(lua)).toBe("baz");
822+
});
823+
824+
test("globalThis to _G (function)", () => {
825+
const code = `
826+
globalThis.foo = () => "bar";
827+
return globalThis.foo();`;
828+
829+
const lua = util.transpileString(code);
830+
831+
expect(util.executeLua(lua)).toBe("bar");
832+
});
833+
});

0 commit comments

Comments
 (0)