@@ -213,7 +213,7 @@ export abstract class LuaTranspiler {
213213 }
214214
215215 // Inline lualib features
216- if ( this . options . luaLibImport === LuaLibImportKind . Inline ) {
216+ if ( this . options . luaLibImport === LuaLibImportKind . Inline && this . luaLibFeatureSet . size > 0 ) {
217217 result += "\n" + "-- Lua Library Imports\n" ;
218218 for ( const feature of this . luaLibFeatureSet ) {
219219 const featureFile = path . resolve ( __dirname , `../dist/lualib/${ feature } .lua` ) ;
@@ -1789,29 +1789,30 @@ export abstract class LuaTranspiler {
17891789
17901790 public transpileConstructor ( node : ts . ConstructorDeclaration ,
17911791 className : string ) : string {
1792- const extraInstanceFields = [ ] ;
1792+ // Check for field declarations in constructor
1793+ const constructorFieldsDeclarations = node . parameters . filter ( p => p . modifiers !== undefined ) ;
17931794
1794- const parameters = [ "self" ] ;
1795- node . parameters . forEach ( param => {
1796- // If param has decorators, add extra instance field
1797- if ( param . modifiers !== undefined ) {
1798- extraInstanceFields . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1799- }
1800- // Add to parameter list
1801- parameters . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1802- } ) ;
1803-
1804- let result = this . indent + `function ${ className } .constructor(${ parameters . join ( "," ) } )\n` ;
1795+ const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters ) ;
18051796
1806- // Add in instance field declarations
1807- for ( const f of extraInstanceFields ) {
1808- result += this . indent + ` self.${ f } = ${ f } \n` ;
1809- }
1797+ let result = this . indent + `function ${ className } .constructor(${ [ "self" ] . concat ( paramNames ) . join ( "," ) } )\n` ;
18101798
18111799 // Transpile constructor body
18121800 this . pushIndent ( ) ;
18131801 this . classStack . push ( className ) ;
1814- result += this . transpileBlock ( node . body ) ;
1802+
1803+ // Add in instance field declarations
1804+ for ( const declaration of constructorFieldsDeclarations ) {
1805+ const declarationName = this . transpileIdentifier ( declaration . name as ts . Identifier ) ;
1806+ if ( declaration . initializer ) {
1807+ const value = this . transpileExpression ( declaration . initializer ) ;
1808+ result += this . indent + `self.${ declarationName } = ${ declarationName } or ${ value } \n` ;
1809+ } else {
1810+ result += this . indent + `self.${ declarationName } = ${ declarationName } \n` ;
1811+ }
1812+ }
1813+
1814+ result += this . transpileFunctionBody ( node . parameters , node . body , spreadIdentifier ) ;
1815+
18151816 this . classStack . pop ( ) ;
18161817 this . popIndent ( ) ;
18171818
0 commit comments