Skip to content

Commit d692b81

Browse files
committed
support for "typeof globalThis" WIP
1 parent 59de49e commit d692b81

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

src/LuaTransformer.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4650,6 +4650,18 @@ export class LuaTransformer {
46504650
return tstl.createNilLiteral();
46514651
}
46524652

4653+
const identifierType = this.checker.getTypeAtLocation(expression);
4654+
if (identifierType.symbol && identifierType.symbol.escapedName === "globalThis") {
4655+
const isIdentifierStandardLibraryType = !tsHelper.isStandardLibraryType(
4656+
this.checker.getTypeAtLocation(expression),
4657+
undefined,
4658+
this.program
4659+
);
4660+
if (isIdentifierStandardLibraryType) {
4661+
return tstl.createIdentifier("_G", expression, this.getIdentifierSymbolId(expression));
4662+
}
4663+
}
4664+
46534665
switch (this.getIdentifierText(expression)) {
46544666
case "NaN":
46554667
return tstl.createParenthesizedExpression(
@@ -4665,17 +4677,6 @@ export class LuaTransformer {
46654677
const math = tstl.createIdentifier("math");
46664678
const huge = tstl.createStringLiteral("huge");
46674679
return tstl.createTableIndexExpression(math, huge, expression);
4668-
4669-
case "globalThis":
4670-
const isIdentifierStandardLibraryType = !tsHelper.isStandardLibraryType(
4671-
this.checker.getTypeAtLocation(expression),
4672-
undefined,
4673-
this.program
4674-
);
4675-
if (isIdentifierStandardLibraryType) {
4676-
return tstl.createIdentifier("_G", expression, this.getIdentifierSymbolId(expression));
4677-
}
4678-
break;
46794680
}
46804681

46814682
return identifier;

test/unit/identifiers.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,19 @@ describe("globalThis translation", () => {
830830

831831
expect(util.executeLua(lua)).toBe("bar");
832832
});
833+
834+
test("globalThis to _G (type alias)", () => {
835+
const code = `
836+
declare let globalAlias: typeof globalThis;
837+
globalAlias.foo = "bar";
838+
return globalThis.foo;`;
839+
840+
const lua = util.transpileString(code);
841+
842+
expect(util.executeLua(lua)).toBe("bar");
843+
});
844+
845+
// TODO globalThis type tests for union and intersection
833846
});
847+
848+

0 commit comments

Comments
 (0)