@@ -1889,29 +1889,30 @@ export abstract class LuaTranspiler {
18891889
18901890 public transpileConstructor ( node : ts . ConstructorDeclaration ,
18911891 className : string ) : string {
1892- const extraInstanceFields = [ ] ;
1892+ // Check for field declarations in constructor
1893+ const constructorFieldsDeclarations = node . parameters . filter ( p => p . modifiers !== undefined ) ;
18931894
1894- const parameters = [ "self" ] ;
1895- node . parameters . forEach ( param => {
1896- // If param has decorators, add extra instance field
1897- if ( param . modifiers !== undefined ) {
1898- extraInstanceFields . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1899- }
1900- // Add to parameter list
1901- parameters . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1902- } ) ;
1903-
1904- let result = this . indent + `function ${ className } .constructor(${ parameters . join ( "," ) } )\n` ;
1895+ const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters ) ;
19051896
1906- // Add in instance field declarations
1907- for ( const f of extraInstanceFields ) {
1908- result += this . indent + ` self.${ f } = ${ f } \n` ;
1909- }
1897+ let result = this . indent + `function ${ className } .constructor(${ [ "self" ] . concat ( paramNames ) . join ( "," ) } )\n` ;
19101898
19111899 // Transpile constructor body
19121900 this . pushIndent ( ) ;
19131901 this . classStack . push ( className ) ;
1914- result += this . transpileBlock ( node . body ) ;
1902+
1903+ // Add in instance field declarations
1904+ for ( const declaration of constructorFieldsDeclarations ) {
1905+ const declarationName = this . transpileIdentifier ( declaration . name as ts . Identifier ) ;
1906+ if ( declaration . initializer ) {
1907+ const value = this . transpileExpression ( declaration . initializer ) ;
1908+ result += this . indent + `self.${ declarationName } = ${ declarationName } or ${ value } \n` ;
1909+ } else {
1910+ result += this . indent + `self.${ declarationName } = ${ declarationName } \n` ;
1911+ }
1912+ }
1913+
1914+ result += this . transpileFunctionBody ( node . parameters , node . body , spreadIdentifier ) ;
1915+
19151916 this . classStack . pop ( ) ;
19161917 this . popIndent ( ) ;
19171918
0 commit comments