@@ -1381,31 +1381,42 @@ export class LuaTransformer {
13811381
13821382 const headerStatements = [ ] ;
13831383
1384- // Add default parameters
1385- const defaultValueDeclarations = parameters
1386- . filter ( declaration => declaration . initializer !== undefined )
1387- . map ( declaration => this . transformParameterDefaultValueDeclaration ( declaration ) ) ;
1384+ // Add default parameters and object binding patterns
1385+ const bindingPatternDeclarations : tstl . Statement [ ] = [ ] ;
1386+ let bindPatternIndex = 0 ;
1387+ for ( const declaration of parameters ) {
1388+ if ( ts . isObjectBindingPattern ( declaration . name ) || ts . isArrayBindingPattern ( declaration . name ) ) {
1389+ const identifier = tstl . createIdentifier ( `____TS_bindingPattern${ bindPatternIndex ++ } ` ) ;
1390+ if ( declaration . initializer !== undefined ) {
1391+ // Default binding parameter
1392+ headerStatements . push (
1393+ this . transformParameterDefaultValueDeclaration ( identifier , declaration . initializer )
1394+ ) ;
1395+ }
1396+
1397+ // Binding pattern
1398+ bindingPatternDeclarations . push ( ...this . statementVisitResultToArray (
1399+ this . transformBindingPattern ( declaration . name , identifier )
1400+ ) ) ;
13881401
1389- headerStatements . push ( ...defaultValueDeclarations ) ;
1402+ } else if ( declaration . initializer !== undefined ) {
1403+ // Default parameter
1404+ headerStatements . push (
1405+ this . transformParameterDefaultValueDeclaration (
1406+ this . transformIdentifier ( declaration . name ) ,
1407+ declaration . initializer
1408+ )
1409+ ) ;
1410+ }
1411+ }
13901412
13911413 // Push spread operator here
13921414 if ( spreadIdentifier ) {
13931415 const spreadTable = this . wrapInTable ( tstl . createDotsLiteral ( ) ) ;
13941416 headerStatements . push ( tstl . createVariableDeclarationStatement ( spreadIdentifier , spreadTable ) ) ;
13951417 }
13961418
1397- // Add object binding patterns
1398- let identifierIndex = 0 ;
1399- const bindingPatternDeclarations : tstl . Statement [ ] = [ ] ;
1400- parameters . forEach ( binding => {
1401- if ( ts . isObjectBindingPattern ( binding . name ) || ts . isArrayBindingPattern ( binding . name ) ) {
1402- const identifier = tstl . createIdentifier ( `____TS_bindingPattern${ identifierIndex ++ } ` ) ;
1403- bindingPatternDeclarations . push ( ...this . statementVisitResultToArray (
1404- this . transformBindingPattern ( binding . name , identifier )
1405- ) ) ;
1406- }
1407- } ) ;
1408-
1419+ // Binding pattern statements need to be after spread table is declared
14091420 headerStatements . push ( ...bindingPatternDeclarations ) ;
14101421
14111422 const bodyStatements = this . performHoisting ( this . transformStatements ( body . statements ) ) ;
@@ -1415,9 +1426,13 @@ export class LuaTransformer {
14151426 return [ headerStatements . concat ( bodyStatements ) , scope ] ;
14161427 }
14171428
1418- private transformParameterDefaultValueDeclaration ( declaration : ts . ParameterDeclaration ) : tstl . Statement {
1419- const parameterName = this . transformIdentifier ( declaration . name as ts . Identifier ) ;
1420- const parameterValue = declaration . initializer ? this . transformExpression ( declaration . initializer ) : undefined ;
1429+ private transformParameterDefaultValueDeclaration (
1430+ parameterName : tstl . Identifier ,
1431+ value ?: ts . Expression ,
1432+ tsOriginal ?: ts . Node
1433+ ) : tstl . Statement
1434+ {
1435+ const parameterValue = value ? this . transformExpression ( value ) : undefined ;
14211436 const assignment = tstl . createAssignmentStatement ( parameterName , parameterValue ) ;
14221437
14231438 const nilCondition = tstl . createBinaryExpression (
@@ -1428,7 +1443,7 @@ export class LuaTransformer {
14281443
14291444 const ifBlock = tstl . createBlock ( [ assignment ] ) ;
14301445
1431- return tstl . createIfStatement ( nilCondition , ifBlock , undefined , declaration ) ;
1446+ return tstl . createIfStatement ( nilCondition , ifBlock , undefined , tsOriginal ) ;
14321447 }
14331448
14341449 public transformBindingPattern (
0 commit comments