11import * as ts from "typescript" ;
22import * as lua from "../../LuaAST" ;
3- import { assume } from "../../utils" ;
43import { TransformationContext } from "../context" ;
54import { createNaN } from "../utils/lua-ast" ;
65import { importLuaLibFeature , LuaLibFeature } from "../utils/lualib" ;
@@ -14,7 +13,7 @@ import {
1413 isStandardLibraryType ,
1514 isStringType ,
1615} from "../utils/typescript" ;
17- import { PropertyCallExpression } from "../visitors/call" ;
16+ import { getCalledExpression } from "../visitors/call" ;
1817import { transformArrayConstructorCall , transformArrayProperty , transformArrayPrototypeCall } from "./array" ;
1918import { transformConsoleCall } from "./console" ;
2019import { transformFunctionPrototypeCall , transformFunctionProperty } from "./function" ;
@@ -74,79 +73,78 @@ export function transformBuiltinCallExpression(
7473 }
7574 }
7675
77- const expression = ts . getOriginalNode ( node . expression ) ;
78- if ( ! ts . isPropertyAccessExpression ( expression ) ) {
76+ const calledMethod = ts . getOriginalNode ( getCalledExpression ( node ) ) ;
77+ if ( ! ts . isPropertyAccessExpression ( calledMethod ) ) {
7978 return ;
8079 }
8180
82- const isOptionalAccess = expression . questionDotToken ;
83- assume < PropertyCallExpression > ( node ) ;
81+ const isOptionalAccess = calledMethod . questionDotToken ;
8482 // If the function being called is of type owner.func, get the type of owner
85- const ownerType = context . checker . getTypeAtLocation ( expression . expression ) ;
83+ const ownerType = context . checker . getTypeAtLocation ( calledMethod . expression ) ;
8684
8785 if ( isStandardLibraryType ( context , ownerType , undefined ) ) {
8886 const symbol = ownerType . getSymbol ( ) ;
8987 switch ( symbol ?. name ) {
9088 case "ArrayConstructor" :
9189 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
92- return transformArrayConstructorCall ( context , node ) ;
90+ return transformArrayConstructorCall ( context , node , calledMethod ) ;
9391 case "Console" :
9492 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
95- return transformConsoleCall ( context , node ) ;
93+ return transformConsoleCall ( context , node , calledMethod ) ;
9694 case "Math" :
9795 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
98- return transformMathCall ( context , node ) ;
96+ return transformMathCall ( context , node , calledMethod ) ;
9997 case "StringConstructor" :
10098 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
101- return transformStringConstructorCall ( context , node ) ;
99+ return transformStringConstructorCall ( context , node , calledMethod ) ;
102100 case "ObjectConstructor" :
103101 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
104- return transformObjectConstructorCall ( context , node ) ;
102+ return transformObjectConstructorCall ( context , node , calledMethod ) ;
105103 case "SymbolConstructor" :
106104 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
107- return transformSymbolConstructorCall ( context , node ) ;
105+ return transformSymbolConstructorCall ( context , node , calledMethod ) ;
108106 case "NumberConstructor" :
109107 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
110- return transformNumberConstructorCall ( context , node ) ;
108+ return transformNumberConstructorCall ( context , node , calledMethod ) ;
111109 case "PromiseConstructor" :
112110 if ( isOptionalCall || isOptionalAccess ) return unsupportedOptionalCall ( ) ;
113- return transformPromiseConstructorCall ( context , node ) ;
111+ return transformPromiseConstructorCall ( context , node , calledMethod ) ;
114112 }
115113 }
116114
117115 const isStringFunction =
118116 isStringType ( context , ownerType ) ||
119- ( expression . questionDotToken && isNullableType ( context , ownerType , isStringType ) ) ;
117+ ( calledMethod . questionDotToken && isNullableType ( context , ownerType , isStringType ) ) ;
120118 if ( isStringFunction && hasStandardLibrarySignature ( context , node ) ) {
121119 if ( isOptionalCall ) return unsupportedOptionalCall ( ) ;
122- return transformStringPrototypeCall ( context , node ) ;
120+ return transformStringPrototypeCall ( context , node , calledMethod ) ;
123121 }
124122
125123 const isNumberFunction =
126124 isNumberType ( context , ownerType ) ||
127- ( expression . questionDotToken && isNullableType ( context , ownerType , isNumberType ) ) ;
125+ ( calledMethod . questionDotToken && isNullableType ( context , ownerType , isNumberType ) ) ;
128126 if ( isNumberFunction && hasStandardLibrarySignature ( context , node ) ) {
129127 if ( isOptionalCall ) return unsupportedOptionalCall ( ) ;
130- return transformNumberPrototypeCall ( context , node ) ;
128+ return transformNumberPrototypeCall ( context , node , calledMethod ) ;
131129 }
132130
133131 const isArrayFunction =
134132 isArrayType ( context , ownerType ) ||
135- ( expression . questionDotToken && isNullableType ( context , ownerType , isArrayType ) ) ;
133+ ( calledMethod . questionDotToken && isNullableType ( context , ownerType , isArrayType ) ) ;
136134 if ( isArrayFunction && hasStandardLibrarySignature ( context , node ) ) {
137135 if ( isOptionalCall ) return unsupportedOptionalCall ( ) ;
138- return transformArrayPrototypeCall ( context , node ) ;
136+ return transformArrayPrototypeCall ( context , node , calledMethod ) ;
139137 }
140138
141139 const isFunctionFunction =
142140 isFunctionType ( ownerType ) ||
143- ( expression . questionDotToken && isNullableType ( context , ownerType , ( _ , t ) => isFunctionType ( t ) ) ) ;
141+ ( calledMethod . questionDotToken && isNullableType ( context , ownerType , ( _ , t ) => isFunctionType ( t ) ) ) ;
144142 if ( isFunctionFunction && hasStandardLibrarySignature ( context , node ) ) {
145143 if ( isOptionalCall ) return unsupportedOptionalCall ( ) ;
146- return transformFunctionPrototypeCall ( context , node ) ;
144+ return transformFunctionPrototypeCall ( context , node , calledMethod ) ;
147145 }
148146
149- const objectResult = transformObjectPrototypeCall ( context , node , expression ) ;
147+ const objectResult = transformObjectPrototypeCall ( context , node , calledMethod ) ;
150148 if ( objectResult ) {
151149 if ( isOptionalCall ) return unsupportedOptionalCall ( ) ;
152150 return objectResult ;
0 commit comments