@@ -1748,29 +1748,30 @@ export abstract class LuaTranspiler {
17481748
17491749 public transpileConstructor ( node : ts . ConstructorDeclaration ,
17501750 className : string ) : string {
1751- const extraInstanceFields = [ ] ;
1751+ // Check for field declarations in constructor
1752+ const constructorFieldsDeclarations = node . parameters . filter ( p => p . modifiers !== undefined ) ;
17521753
1753- const parameters = [ "self" ] ;
1754- node . parameters . forEach ( param => {
1755- // If param has decorators, add extra instance field
1756- if ( param . modifiers !== undefined ) {
1757- extraInstanceFields . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1758- }
1759- // Add to parameter list
1760- parameters . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1761- } ) ;
1762-
1763- let result = this . indent + `function ${ className } .constructor(${ parameters . join ( "," ) } )\n` ;
1754+ const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters ) ;
17641755
1765- // Add in instance field declarations
1766- for ( const f of extraInstanceFields ) {
1767- result += this . indent + ` self.${ f } = ${ f } \n` ;
1768- }
1756+ let result = this . indent + `function ${ className } .constructor(${ [ "self" ] . concat ( paramNames ) . join ( "," ) } )\n` ;
17691757
17701758 // Transpile constructor body
17711759 this . pushIndent ( ) ;
17721760 this . classStack . push ( className ) ;
1773- result += this . transpileBlock ( node . body ) ;
1761+
1762+ // Add in instance field declarations
1763+ for ( const declaration of constructorFieldsDeclarations ) {
1764+ const declarationName = this . transpileIdentifier ( declaration . name as ts . Identifier ) ;
1765+ if ( declaration . initializer ) {
1766+ const value = this . transpileExpression ( declaration . initializer ) ;
1767+ result += this . indent + `self.${ declarationName } = ${ declarationName } or ${ value } \n` ;
1768+ } else {
1769+ result += this . indent + `self.${ declarationName } = ${ declarationName } \n` ;
1770+ }
1771+ }
1772+
1773+ result += this . transpileFunctionBody ( node . parameters , node . body , spreadIdentifier ) ;
1774+
17741775 this . classStack . pop ( ) ;
17751776 this . popIndent ( ) ;
17761777
0 commit comments