@@ -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
0 commit comments