@@ -18,26 +18,25 @@ import {
1818 invalidMultiFunctionUse ,
1919} from "../../../transformation/utils/diagnostics" ;
2020
21- const isMultiHelperDeclaration = ( declaration : ts . Declaration ) : boolean =>
22- helpers . getHelperFileKind ( declaration ) === helpers . HelperKind . MultiFunction ;
21+ const isMultiFunctionDeclaration = ( declaration : ts . Declaration ) : boolean =>
22+ helpers . getHelperKind ( declaration ) === helpers . HelperKind . MultiFunction ;
2323
24- function isMultiHelperCallSignature ( context : TransformationContext , expression : ts . CallExpression ) : boolean {
24+ const isMultiTypeDeclaration = ( declaration : ts . Declaration ) : boolean =>
25+ helpers . getHelperKind ( declaration ) === helpers . HelperKind . MultiType ;
26+
27+ function isMultiFunction ( context : TransformationContext , expression : ts . CallExpression ) : boolean {
2528 const type = context . checker . getTypeAtLocation ( expression . expression ) ;
26- return type . symbol ?. declarations ?. some ( isMultiHelperDeclaration ) ?? false ;
29+ return type . symbol ?. declarations ?. some ( isMultiFunctionDeclaration ) ?? false ;
2730}
2831
29- export function isMultiReturnCall ( context : TransformationContext , node : ts . Node ) : node is ts . CallExpression {
30- if ( ! ts . isCallExpression ( node ) ) {
31- return false ;
32- }
33-
32+ export function returnsMultiType ( context : TransformationContext , node : ts . CallExpression ) : boolean {
3433 const signature = context . checker . getResolvedSignature ( node ) ;
35- return signature ?. getReturnType ( ) . aliasSymbol ?. declarations ?. some ( isMultiHelperDeclaration ) ?? false ;
34+ return signature ?. getReturnType ( ) . aliasSymbol ?. declarations ?. some ( isMultiTypeDeclaration ) ?? false ;
3635}
3736
38- export function isMultiHelperNode ( context : TransformationContext , node : ts . Node ) : boolean {
37+ export function isMultiFunctionNode ( context : TransformationContext , node : ts . Node ) : boolean {
3938 const type = context . checker . getTypeAtLocation ( node ) ;
40- return type . symbol ?. declarations ?. some ( isMultiHelperDeclaration ) ?? false ;
39+ return type . symbol ?. declarations ?. some ( isMultiFunctionDeclaration ) ?? false ;
4140}
4241
4342export function transformMultiHelperReturnStatement (
@@ -46,17 +45,17 @@ export function transformMultiHelperReturnStatement(
4645) : lua . Statement | undefined {
4746 if ( ! statement . expression ) return ;
4847 if ( ! ts . isCallExpression ( statement . expression ) ) return ;
49- if ( ! isMultiHelperCallSignature ( context , statement . expression ) ) return ;
48+ if ( ! isMultiFunction ( context , statement . expression ) ) return ;
5049
5150 const expressions = transformArguments ( context , statement . expression . arguments ) ;
5251 return lua . createReturnStatement ( expressions , statement ) ;
5352}
5453
55- function transformMultiHelperCallArguments (
54+ function transformMultiFunctionArguments (
5655 context : TransformationContext ,
5756 expression : ts . CallExpression
5857) : lua . Expression [ ] | lua . Expression {
59- if ( ! isMultiHelperCallSignature ( context , expression ) ) {
58+ if ( ! isMultiFunction ( context , expression ) ) {
6059 return context . transformExpression ( expression ) ;
6160 }
6261
@@ -72,7 +71,7 @@ export function transformMultiHelperVariableDeclaration(
7271 declaration : ts . VariableDeclaration
7372) : lua . Statement [ ] | undefined {
7473 if ( ! declaration . initializer ) return ;
75- if ( ! isMultiReturnCall ( context , declaration . initializer ) ) return ;
74+ if ( ! ts . isCallExpression ( declaration . initializer ) || ! returnsMultiType ( context , declaration . initializer ) ) return ;
7675
7776 if ( ! ts . isArrayBindingPattern ( declaration . name ) ) {
7877 context . diagnostics . push ( invalidMultiReturnToNonArrayBindingPattern ( declaration . name ) ) ;
@@ -100,7 +99,7 @@ export function transformMultiHelperVariableDeclaration(
10099 }
101100 }
102101
103- const rightExpressions = transformMultiHelperCallArguments ( context , declaration . initializer ) ;
102+ const rightExpressions = transformMultiFunctionArguments ( context , declaration . initializer ) ;
104103 return createLocalOrExportedOrGlobalDeclaration ( context , leftIdentifiers , rightExpressions , declaration ) ;
105104}
106105
@@ -110,7 +109,8 @@ export function transformMultiHelperDestructuringAssignmentStatement(
110109) : lua . Statement [ ] | undefined {
111110 if ( ! ts . isBinaryExpression ( statement . expression ) ) return ;
112111 if ( statement . expression . operatorToken . kind !== ts . SyntaxKind . EqualsToken ) return ;
113- if ( ! isMultiReturnCall ( context , statement . expression . right ) ) return ;
112+ if ( ! ts . isCallExpression ( statement . expression . right ) ) return ;
113+ if ( ! returnsMultiType ( context , statement . expression . right ) ) return ;
114114
115115 if ( ! ts . isArrayLiteralExpression ( statement . expression . left ) ) {
116116 context . diagnostics . push ( invalidMultiReturnToNonArrayLiteral ( statement . expression . left ) ) ;
@@ -134,7 +134,7 @@ export function transformMultiHelperDestructuringAssignmentStatement(
134134
135135 const leftIdentifiers = statement . expression . left . elements . map ( transformLeft ) ;
136136
137- const rightExpressions = transformMultiHelperCallArguments ( context , statement . expression . right ) ;
137+ const rightExpressions = transformMultiFunctionArguments ( context , statement . expression . right ) ;
138138
139139 const trailingStatements = statement . expression . left . elements . flatMap ( expression => {
140140 const symbol = context . checker . getSymbolAtLocation ( expression ) ;
@@ -158,7 +158,7 @@ export function findMultiHelperAssignmentViolations(
158158 const valueSymbol = context . checker . getShorthandAssignmentValueSymbol ( element ) ;
159159 if ( valueSymbol ) {
160160 const declaration = valueSymbol . valueDeclaration ;
161- if ( declaration && isMultiHelperDeclaration ( declaration ) ) {
161+ if ( declaration && isMultiFunctionDeclaration ( declaration ) ) {
162162 context . diagnostics . push ( invalidMultiFunctionUse ( element ) ) ;
163163 return element ;
164164 }
0 commit comments