@@ -4,13 +4,7 @@ import { TransformationContext } from "../context";
44import { createNaN } from "../utils/lua-ast" ;
55import { importLuaLibFeature , LuaLibFeature } from "../utils/lualib" ;
66import { getIdentifierSymbolId } from "../utils/symbols" ;
7- import {
8- isStandardLibraryType ,
9- isStandardLibraryDeclaration ,
10- isStringType ,
11- isArrayType ,
12- isFunctionType ,
13- } from "../utils/typescript" ;
7+ import { isStandardLibraryType , isStringType , isArrayType , isFunctionType } from "../utils/typescript" ;
148import { getCalledExpression } from "../visitors/call" ;
159import { transformArrayConstructorCall , transformArrayProperty , transformArrayPrototypeCall } from "./array" ;
1610import { transformConsoleCall } from "./console" ;
@@ -85,9 +79,7 @@ function tryTransformBuiltinGlobalMethodCall(
8579 calledMethod : ts . PropertyAccessExpression
8680) {
8781 const ownerType = context . checker . getTypeAtLocation ( calledMethod . expression ) ;
88- if ( ! isStandardLibraryType ( context , ownerType , undefined ) ) return ;
89-
90- const ownerSymbol = ownerType . symbol ;
82+ const ownerSymbol = tryGetStandardLibrarySymbolOfType ( context , ownerType ) ;
9183 if ( ! ownerSymbol || ownerSymbol . parent ) return ;
9284
9385 let result : lua . Expression | undefined ;
@@ -129,10 +121,9 @@ function tryTransformBuiltinPropertyCall(
129121 node : ts . CallExpression ,
130122 calledMethod : ts . PropertyAccessExpression
131123) {
132- const signatureDeclaration = context . checker . getResolvedSignature ( node ) ?. declaration ;
133- if ( ! signatureDeclaration || ! isStandardLibraryDeclaration ( context , signatureDeclaration ) ) return ;
134-
135- const callSymbol = context . checker . getTypeAtLocation ( signatureDeclaration ) . symbol ;
124+ const functionType = context . checker . getTypeAtLocation ( node . expression ) ;
125+ const callSymbol = tryGetStandardLibrarySymbolOfType ( context , functionType ) ;
126+ if ( ! callSymbol ) return ;
136127 const ownerSymbol = callSymbol . parent ;
137128 if ( ! ownerSymbol || ownerSymbol . parent ) return ;
138129
@@ -217,3 +208,16 @@ export function checkForLuaLibType(context: TransformationContext, type: ts.Type
217208 importLuaLibFeature ( context , LuaLibFeature . Error ) ;
218209 }
219210}
211+
212+ function tryGetStandardLibrarySymbolOfType ( context : TransformationContext , type : ts . Type ) : ts . Symbol | undefined {
213+ if ( type . isUnionOrIntersection ( ) ) {
214+ for ( const subType of type . types ) {
215+ const symbol = tryGetStandardLibrarySymbolOfType ( context , subType ) ;
216+ if ( symbol ) return symbol ;
217+ }
218+ } else if ( isStandardLibraryType ( context , type , undefined ) ) {
219+ return type . symbol ;
220+ }
221+
222+ return undefined ;
223+ }
0 commit comments