Skip to content

Commit 59de49e

Browse files
committed
moved globalThis check to transformIdentifierExpression
1 parent f45a53c commit 59de49e

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

src/LuaTransformer.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,18 +4630,6 @@ 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-
4637-
if (identifier.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
4638-
// TODO this is a hack that allows use to keep Identifier
4639-
// as return time as changing that would break a lot of stuff.
4640-
// But this should be changed to return tstl.createNilLiteral()
4641-
// at some point.
4642-
return tstl.createIdentifier("nil");
4643-
}
4644-
46454633
const text = this.hasUnsafeIdentifierName(identifier)
46464634
? this.createSafeName(this.getIdentifierText(identifier))
46474635
: this.getIdentifierText(identifier);
@@ -4658,6 +4646,10 @@ export class LuaTransformer {
46584646
return this.createExportedIdentifier(identifier, exportScope);
46594647
}
46604648

4649+
if (expression.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
4650+
return tstl.createNilLiteral();
4651+
}
4652+
46614653
switch (this.getIdentifierText(expression)) {
46624654
case "NaN":
46634655
return tstl.createParenthesizedExpression(
@@ -4673,6 +4665,17 @@ export class LuaTransformer {
46734665
const math = tstl.createIdentifier("math");
46744666
const huge = tstl.createStringLiteral("huge");
46754667
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;
46764679
}
46774680

46784681
return identifier;
@@ -5176,8 +5179,7 @@ export class LuaTransformer {
51765179
protected hasUnsafeSymbolName(symbol: ts.Symbol, tsOriginal?: ts.Identifier): boolean {
51775180
const isLuaKeyword = luaKeywords.has(symbol.name);
51785181
const isInvalidIdentifier = !tsHelper.isValidLuaIdentifier(symbol.name);
5179-
// TODO investigate: declarations will be undefined if "globalThis" is passed, could be a TS bug
5180-
// WORKAROUND: added check for symbol.declarations
5182+
// TODO rework once microsoft/TypeScript#24706 is fixed and maybe remove check for symbol.declarations
51815183
const isAmbient = symbol.declarations && symbol.declarations.some(d => tsHelper.isAmbient(d));
51825184
if ((isLuaKeyword || isInvalidIdentifier) && isAmbient) {
51835185
// Catch ambient declarations of identifiers with bad names

test/unit/identifiers.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -794,19 +794,19 @@ describe("globalThis translation", () => {
794794
const code = `
795795
var foo = "bar";
796796
return globalThis.foo;`;
797-
797+
798798
const lua = util.transpileString(code);
799-
799+
800800
expect(util.executeLua(lua)).toBe("bar");
801801
});
802802

803803
test("globalThis to _G (assign)", () => {
804804
const code = `
805805
globalThis.foo = "bar";
806806
return globalThis.foo;`;
807-
807+
808808
const lua = util.transpileString(code);
809-
809+
810810
expect(util.executeLua(lua)).toBe("bar");
811811
});
812812

@@ -815,19 +815,19 @@ describe("globalThis translation", () => {
815815
globalThis.foo = "bar";
816816
globalThis.foo = "baz";
817817
return globalThis.foo;`;
818-
818+
819819
const lua = util.transpileString(code);
820-
820+
821821
expect(util.executeLua(lua)).toBe("baz");
822822
});
823823

824824
test("globalThis to _G (function)", () => {
825825
const code = `
826826
globalThis.foo = () => "bar";
827827
return globalThis.foo();`;
828-
828+
829829
const lua = util.transpileString(code);
830-
830+
831831
expect(util.executeLua(lua)).toBe("bar");
832832
});
833-
});
833+
});

0 commit comments

Comments
 (0)