@@ -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` ) ;
@@ -1820,29 +1820,30 @@ export abstract class LuaTranspiler {
18201820
18211821 public transpileConstructor ( node : ts . ConstructorDeclaration ,
18221822 className : string ) : string {
1823- const extraInstanceFields = [ ] ;
1823+ // Check for field declarations in constructor
1824+ const constructorFieldsDeclarations = node . parameters . filter ( p => p . modifiers !== undefined ) ;
18241825
1825- const parameters = [ "self" ] ;
1826- node . parameters . forEach ( param => {
1827- // If param has decorators, add extra instance field
1828- if ( param . modifiers !== undefined ) {
1829- extraInstanceFields . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1830- }
1831- // Add to parameter list
1832- parameters . push ( this . transpileIdentifier ( param . name as ts . Identifier ) ) ;
1833- } ) ;
1834-
1835- let result = this . indent + `function ${ className } .constructor(${ parameters . join ( "," ) } )\n` ;
1826+ const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters ) ;
18361827
1837- // Add in instance field declarations
1838- for ( const f of extraInstanceFields ) {
1839- result += this . indent + ` self.${ f } = ${ f } \n` ;
1840- }
1828+ let result = this . indent + `function ${ className } .constructor(${ [ "self" ] . concat ( paramNames ) . join ( "," ) } )\n` ;
18411829
18421830 // Transpile constructor body
18431831 this . pushIndent ( ) ;
18441832 this . classStack . push ( className ) ;
1845- result += this . transpileBlock ( node . body ) ;
1833+
1834+ // Add in instance field declarations
1835+ for ( const declaration of constructorFieldsDeclarations ) {
1836+ const declarationName = this . transpileIdentifier ( declaration . name as ts . Identifier ) ;
1837+ if ( declaration . initializer ) {
1838+ const value = this . transpileExpression ( declaration . initializer ) ;
1839+ result += this . indent + `self.${ declarationName } = ${ declarationName } or ${ value } \n` ;
1840+ } else {
1841+ result += this . indent + `self.${ declarationName } = ${ declarationName } \n` ;
1842+ }
1843+ }
1844+
1845+ result += this . transpileFunctionBody ( node . parameters , node . body , spreadIdentifier ) ;
1846+
18461847 this . classStack . pop ( ) ;
18471848 this . popIndent ( ) ;
18481849
0 commit comments