Skip to content

Commit 8cd5a64

Browse files
committed
globalThis transformation only for identifier name not identifier type.
1 parent 5d5323e commit 8cd5a64

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/LuaTransformer.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,11 +4695,6 @@ export class LuaTransformer {
46954695
return tstl.createNilLiteral();
46964696
}
46974697

4698-
const identifierType = this.checker.getTypeAtLocation(expression);
4699-
if (identifierType.symbol && identifierType.symbol.escapedName === "globalThis") {
4700-
return tstl.createIdentifier("_G", expression, this.getIdentifierSymbolId(expression));
4701-
}
4702-
47034698
switch (this.getIdentifierText(expression)) {
47044699
case "NaN":
47054700
return tstl.createParenthesizedExpression(
@@ -4715,6 +4710,9 @@ export class LuaTransformer {
47154710
const math = tstl.createIdentifier("math");
47164711
const huge = tstl.createStringLiteral("huge");
47174712
return tstl.createTableIndexExpression(math, huge, expression);
4713+
4714+
case "globalThis":
4715+
return tstl.createIdentifier("_G", expression, this.getIdentifierSymbolId(expression));
47184716
}
47194717

47204718
return identifier;

test/unit/identifiers.spec.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,33 @@ describe("globalThis translation", () => {
831831
expect(util.executeLua(lua)).toBe("bar");
832832
});
833833

834-
test("globalThis to _G (type alias)", () => {
834+
test("globalThis to _G (assign + noImplicitAny)", () => {
835835
const code = `
836-
declare let globalAlias: typeof globalThis;
837-
globalAlias.foo = "bar";
838-
return globalThis.foo;`;
836+
(<any>globalThis).foo = "bar";
837+
return (<any>globalThis).foo;`;
838+
839+
const lua = util.transpileString(code, {noImplicitAny: true});
840+
841+
expect(util.executeLua(lua)).toBe("bar");
842+
});
843+
844+
test("globalThis to _G (var)", () => {
845+
const code = `
846+
var globalFoo = "bar";
847+
return globalThis.globalFoo;`;
839848

840849
const lua = util.transpileString(code);
841850

842851
expect(util.executeLua(lua)).toBe("bar");
843852
});
853+
854+
test("globalThis to _G (let)", () => {
855+
const code = `
856+
let NotAGlobalFoo = "bar";
857+
return (<any>globalThis).NotAGlobalFoo;`;
858+
859+
const lua = util.transpileString(code);
860+
861+
expect(util.executeLua(lua)).toBe(undefined);
862+
});
844863
});

0 commit comments

Comments
 (0)