@@ -16,7 +16,7 @@ import {
1616} from "./compound" ;
1717import { assert } from "../../../utils" ;
1818import { transformOrderedExpressions } from "../expression-list" ;
19- import { transformInPrecedingStatementScope } from "../../utils/preceding-statements" ;
19+ import { transformInPrecedingStatementScope , WithPrecedingStatements } from "../../utils/preceding-statements" ;
2020
2121type ShortCircuitOperator =
2222 | ts . SyntaxKind . AmpersandAmpersandToken
@@ -100,7 +100,7 @@ export function createShortCircuitBinaryExpressionPrecedingStatements(
100100 rightPrecedingStatements : lua . Statement [ ] ,
101101 operator : ShortCircuitOperator ,
102102 node ?: ts . BinaryExpression
103- ) : [ lua . Statement [ ] , lua . Expression ] {
103+ ) : WithPrecedingStatements < lua . Expression > {
104104 const conditionIdentifier = context . createTempNameForLuaExpression ( lhs ) ;
105105 const assignmentStatement = lua . createVariableDeclarationStatement ( conditionIdentifier , lhs , node ?. left ) ;
106106
@@ -132,19 +132,19 @@ export function createShortCircuitBinaryExpressionPrecedingStatements(
132132 undefined ,
133133 node ?. left
134134 ) ;
135- return [ [ assignmentStatement , ifStatement ] , conditionIdentifier ] ;
135+ return { precedingStatements : [ assignmentStatement , ifStatement ] , result : conditionIdentifier } ;
136136}
137137
138138function transformShortCircuitBinaryExpression (
139139 context : TransformationContext ,
140140 node : ts . BinaryExpression ,
141141 operator : ShortCircuitOperator
142- ) : [ lua . Statement [ ] , lua . Expression ] {
142+ ) : WithPrecedingStatements < lua . Expression > {
143143 const lhs = context . transformExpression ( node . left ) ;
144- const [ rightPrecedingStatements , rhs ] = transformInPrecedingStatementScope ( context , ( ) =>
144+ const { precedingStatements , result } = transformInPrecedingStatementScope ( context , ( ) =>
145145 context . transformExpression ( node . right )
146146 ) ;
147- return transformBinaryOperation ( context , lhs , rhs , rightPrecedingStatements , operator , node ) ;
147+ return transformBinaryOperation ( context , lhs , result , precedingStatements , operator , node ) ;
148148}
149149
150150export function transformBinaryOperation (
@@ -154,7 +154,7 @@ export function transformBinaryOperation(
154154 rightPrecedingStatements : lua . Statement [ ] ,
155155 operator : BitOperator | SimpleOperator | ts . SyntaxKind . QuestionQuestionToken ,
156156 node : ts . Node
157- ) : [ lua . Statement [ ] , lua . Expression ] {
157+ ) : WithPrecedingStatements < lua . Expression > {
158158 if ( rightPrecedingStatements . length > 0 && isShortCircuitOperator ( operator ) ) {
159159 assert ( ts . isBinaryExpression ( node ) ) ;
160160 return createShortCircuitBinaryExpressionPrecedingStatements (
@@ -167,10 +167,10 @@ export function transformBinaryOperation(
167167 ) ;
168168 }
169169
170- return [
171- rightPrecedingStatements ,
172- transformBinaryOperationWithNoPrecedingStatements ( context , left , right , operator , node ) ,
173- ] ;
170+ return {
171+ precedingStatements : rightPrecedingStatements ,
172+ result : transformBinaryOperationWithNoPrecedingStatements ( context , left , right , operator , node ) ,
173+ } ;
174174}
175175
176176export const transformBinaryExpression : FunctionVisitor < ts . BinaryExpression > = ( node , context ) => {
@@ -216,7 +216,7 @@ export const transformBinaryExpression: FunctionVisitor<ts.BinaryExpression> = (
216216
217217 case ts . SyntaxKind . CommaToken : {
218218 const statements = context . transformStatements ( ts . factory . createExpressionStatement ( node . left ) ) ;
219- const [ precedingStatements , result ] = transformInPrecedingStatementScope ( context , ( ) =>
219+ const { precedingStatements, result } = transformInPrecedingStatementScope ( context , ( ) =>
220220 context . transformExpression ( node . right )
221221 ) ;
222222 statements . push ( ...precedingStatements ) ;
@@ -227,17 +227,27 @@ export const transformBinaryExpression: FunctionVisitor<ts.BinaryExpression> = (
227227 case ts . SyntaxKind . QuestionQuestionToken :
228228 case ts . SyntaxKind . AmpersandAmpersandToken :
229229 case ts . SyntaxKind . BarBarToken : {
230- const [ precedingStatements , result ] = transformShortCircuitBinaryExpression ( context , node , operator ) ;
230+ const { precedingStatements, result } = transformShortCircuitBinaryExpression ( context , node , operator ) ;
231231 context . addPrecedingStatements ( precedingStatements ) ;
232232 return result ;
233233 }
234234 }
235235
236- let [ precedingStatements , [ lhs , rhs ] ] = transformInPrecedingStatementScope ( context , ( ) =>
236+ const {
237+ precedingStatements : orderedExpressionPrecedingStatements ,
238+ result : [ lhs , rhs ] ,
239+ } = transformInPrecedingStatementScope ( context , ( ) =>
237240 transformOrderedExpressions ( context , [ node . left , node . right ] )
238241 ) ;
239- let result : lua . Expression ;
240- [ precedingStatements , result ] = transformBinaryOperation ( context , lhs , rhs , precedingStatements , operator , node ) ;
242+
243+ const { precedingStatements, result } = transformBinaryOperation (
244+ context ,
245+ lhs ,
246+ rhs ,
247+ orderedExpressionPrecedingStatements ,
248+ operator ,
249+ node
250+ ) ;
241251 context . addPrecedingStatements ( precedingStatements ) ;
242252 return result ;
243253} ;
@@ -277,7 +287,7 @@ function transformNullishCoalescingOperationNoPrecedingStatements(
277287 // Check if we can take a shortcut to 'lhs or rhs' if the left-hand side cannot be 'false'.
278288 if ( canBeFalsyWhenNotNull ( context , lhsType ) ) {
279289 // reuse logic from case with preceding statements
280- const [ precedingStatements , result ] = createShortCircuitBinaryExpressionPrecedingStatements (
290+ const { precedingStatements, result } = createShortCircuitBinaryExpressionPrecedingStatements (
281291 context ,
282292 transformedLeft ,
283293 transformedRight ,
0 commit comments