@@ -1497,29 +1497,9 @@ export abstract class LuaTranspiler {
14971497 if ( ! node . body ) { return "" ; }
14981498
14991499 let result = "" ;
1500- const identifier = node . name ;
1501- const methodName = this . transpileIdentifier ( identifier ) ;
1502- const parameters = node . parameters ;
1503- const body = node . body ;
1500+ const methodName = this . transpileIdentifier ( node . name ) ;
15041501
1505- // Build parameter string
1506- const paramNames : string [ ] = [ ] ;
1507-
1508- let spreadIdentifier = "" ;
1509-
1510- // Only push parameter name to paramName array if it isn't a spread parameter
1511- for ( const param of parameters ) {
1512- const paramName = this . transpileIdentifier ( param . name as ts . Identifier ) ;
1513-
1514- // This parameter is a spread parameter (...param)
1515- if ( ! param . dotDotDotToken ) {
1516- paramNames . push ( paramName ) ;
1517- } else {
1518- spreadIdentifier = paramName ;
1519- // Push the spread operator into the paramNames array
1520- paramNames . push ( "..." ) ;
1521- }
1522- }
1502+ const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters ) ;
15231503
15241504 let prefix = this . accessPrefix ( node ) ;
15251505
@@ -1531,13 +1511,7 @@ export abstract class LuaTranspiler {
15311511 result += this . indent + prefix + `function ${ methodName } (${ paramNames . join ( "," ) } )\n` ;
15321512
15331513 this . pushIndent ( ) ;
1534-
1535- // Push spread operator here
1536- if ( spreadIdentifier !== "" ) {
1537- result += this . indent + `local ${ spreadIdentifier } = { ... }\n` ;
1538- }
1539-
1540- result += this . transpileBlock ( body ) ;
1514+ result += this . transpileFunctionBody ( node . parameters , node . body , spreadIdentifier ) ;
15411515 this . popIndent ( ) ;
15421516
15431517 // Close function block
@@ -1548,21 +1522,10 @@ export abstract class LuaTranspiler {
15481522 return result ;
15491523 }
15501524
1551- public transpileMethodDeclaration ( node : ts . MethodDeclaration , callPath : string ) : string {
1552- // Don't transpile methods without body (overload declarations)
1553- if ( ! node . body ) { return "" ; }
1554-
1555- let result = "" ;
1556- const identifier = node . name as ts . Identifier ;
1557- let methodName = this . transpileIdentifier ( identifier ) ;
1558- if ( methodName === "toString" ) {
1559- methodName = "__tostring" ;
1560- }
1561- const parameters = node . parameters ;
1562- const body = node . body ;
1563-
1525+ // Transpile a list of parameters, returns a list of transpiled parameters and an optional spread identifier
1526+ public transpileParameters ( parameters : ts . NodeArray < ts . ParameterDeclaration > ) : [ string [ ] , string ] {
15641527 // Build parameter string
1565- const paramNames : string [ ] = [ "self" ] ;
1528+ const paramNames : string [ ] = [ ] ;
15661529
15671530 let spreadIdentifier = "" ;
15681531
@@ -1579,21 +1542,49 @@ export abstract class LuaTranspiler {
15791542 paramNames . push ( "..." ) ;
15801543 }
15811544 }
1582- // Parameters with default values
1583- const defaultValueParams = node . parameters . filter ( declaration => declaration . initializer !== undefined ) ;
15841545
1585- // Build function header
1586- result += this . indent + `function ${ callPath } ${ methodName } ( ${ paramNames . join ( "," ) } )\n` ;
1546+ return [ paramNames , spreadIdentifier ] ;
1547+ }
15871548
1588- this . pushIndent ( ) ;
1549+ public transpileFunctionBody ( parameters : ts . NodeArray < ts . ParameterDeclaration > ,
1550+ body : ts . Block ,
1551+ spreadIdentifier : string = ""
1552+ ) : string {
1553+ let result = "" ;
1554+
1555+ // Add default parameters
1556+ const defaultValueParams = parameters . filter ( declaration => declaration . initializer !== undefined ) ;
1557+ result += this . transpileParameterDefaultValues ( defaultValueParams ) ;
15891558
15901559 // Push spread operator here
15911560 if ( spreadIdentifier !== "" ) {
15921561 result += this . indent + `local ${ spreadIdentifier } = { ... }\n` ;
15931562 }
15941563
1595- result += this . transpileParameterDefaultValues ( defaultValueParams ) ;
15961564 result += this . transpileBlock ( body ) ;
1565+
1566+ return result ;
1567+ }
1568+
1569+ public transpileMethodDeclaration ( node : ts . MethodDeclaration , callPath : string ) : string {
1570+ // Don't transpile methods without body (overload declarations)
1571+ if ( ! node . body ) { return "" ; }
1572+
1573+ let result = "" ;
1574+ let methodName = this . transpileIdentifier ( node . name as ts . Identifier ) ;
1575+ if ( methodName === "toString" ) {
1576+ methodName = "__tostring" ;
1577+ }
1578+
1579+ const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters ) ;
1580+
1581+ const selfParamNames = [ "self" ] . concat ( paramNames ) ;
1582+
1583+ // Build function header
1584+ result += this . indent + `function ${ callPath } ${ methodName } (${ selfParamNames . join ( "," ) } )\n` ;
1585+
1586+ this . pushIndent ( ) ;
1587+ result += this . transpileFunctionBody ( node . parameters , node . body , spreadIdentifier ) ;
15971588 this . popIndent ( ) ;
15981589
15991590 // Close function block
0 commit comments