@@ -4,6 +4,7 @@ import * as ts from "typescript";
44
55import * as tstl from "./LuaAST" ;
66
7+ import { TcpSocketConnectOpts } from "net" ;
78import { CompilerOptions } from "./CompilerOptions" ;
89import { DecoratorKind } from "./Decorator" ;
910import { TSTLErrors } from "./Errors" ;
@@ -37,7 +38,7 @@ export class LuaTransformer {
3738 switch ( node . kind ) {
3839 // Blocm
3940 case ts . SyntaxKind . Block :
40- return this . transformBlock ( node as ts . Block ) ;
41+ return this . transformScopeBlock ( node as ts . Block ) ;
4142 // Declaration Statements
4243 case ts . SyntaxKind . ImportDeclaration :
4344 return this . transformImportDeclaration ( node as ts . ImportDeclaration ) ;
@@ -89,7 +90,7 @@ export class LuaTransformer {
8990 }
9091 }
9192
92- /** Convers an array of ts.Statements into an array of ts .Statements */
93+ /** Convers an array of ts.Statements into an array of tstl .Statements */
9394 public transformStatements (
9495 statements : ts . Statement [ ] | ReadonlyArray < ts . Statement > ) : tstl . Statement [ ] {
9596
@@ -100,7 +101,11 @@ export class LuaTransformer {
100101 return flat ( statements ) . map ( statement => this . transformStatement ( statement ) ) as tstl . Statement [ ] ;
101102 }
102103
103- public transformBlock ( block : ts . Block ) : tstl . DoStatement {
104+ public transformBlock ( block : ts . Block ) : tstl . Block {
105+ return tstl . createBlock ( this . transformStatements ( block . statements ) , undefined , block ) ;
106+ }
107+
108+ public transformScopeBlock ( block : ts . Block ) : tstl . DoStatement {
104109 return tstl . createDoStatement ( this . transformStatements ( block . statements ) , undefined , block ) ;
105110 }
106111
@@ -200,7 +205,7 @@ export class LuaTransformer {
200205 const staticFields = properties . filter ( isStatic ) ;
201206 const instanceFields = properties . filter ( prop => ! isStatic ( prop ) ) ;
202207
203- let result : tstl . Statement [ ] = [ ] ;
208+ const result : tstl . Statement [ ] = [ ] ;
204209
205210 // Overwrite the original className with the class we are overriding for extensions
206211 if ( isMetaExtension ) {
@@ -281,26 +286,26 @@ export class LuaTransformer {
281286 statement . members . filter ( n => ts . isConstructorDeclaration ( n ) && n . body ) [ 0 ] as ts . ConstructorDeclaration ;
282287 if ( constructor ) {
283288 // Add constructor plus initialization of instance fields
284- result += this . transpileConstructor ( constructor , className ) ;
289+ result . push ( this . transformConstructor ( constructor , className ) ) ;
285290 } else if ( ! isExtension && ! extendsType ) {
286291 // Generate a constructor if none was defined
287- result += this . transpileConstructor ( ts . createConstructor ( [ ] , [ ] , [ ] , ts . createBlock ( [ ] , true ) ) ,
288- className ) ;
292+ result . push ( this . transformConstructor ( ts . createConstructor ( [ ] , [ ] , [ ] , ts . createBlock ( [ ] , true ) ) ,
293+ className ) ) ;
289294 }
290295
291296 // Transpile get accessors
292297 statement . members . filter ( ts . isGetAccessor ) . forEach ( getAccessor => {
293- result += this . transpileGetAccessorDeclaration ( getAccessor , className ) ;
298+ result . push ( this . transformGetAccessorDeclaration ( getAccessor , className ) ) ;
294299 } ) ;
295300
296301 // Transpile set accessors
297302 statement . members . filter ( ts . isSetAccessor ) . forEach ( setAccessor => {
298- result += this . transpileSetAccessorDeclaration ( setAccessor , className ) ;
303+ result . push ( this . transformSetAccessorDeclaration ( setAccessor , className ) ) ;
299304 } ) ;
300305
301306 // Transpile methods
302307 statement . members . filter ( ts . isMethodDeclaration ) . forEach ( method => {
303- result += this . transpileMethodDeclaration ( method , ` ${ className } .` ) ;
308+ result . push ( this . transformMethodDeclaration ( method , className ) ) ;
304309 } ) ;
305310
306311 return result ;
@@ -453,7 +458,7 @@ export class LuaTransformer {
453458 }
454459
455460 public transformConstructor (
456- statement : ts . ConstructorDeclaration , className : tstl . Identifier ) : StatementVisitResult {
461+ statement : ts . ConstructorDeclaration , className : tstl . Identifier ) : tstl . VariableAssignmentStatement {
457462
458463 // Don't transpile methods without body (overload declarations)
459464 if ( ! statement . body ) {
@@ -467,7 +472,6 @@ export class LuaTransformer {
467472 this . classStack . push ( className ) ;
468473
469474 const bodyStatements : tstl . Statement [ ] = [ ] ;
470- const body : tstl . Block = tstl . createBlock ( bodyStatements ) ;
471475
472476 // Add in instance field declarations
473477 for ( const declaration of constructorFieldsDeclarations ) {
@@ -508,6 +512,8 @@ export class LuaTransformer {
508512
509513 bodyStatements . concat ( this . transformFunctionBody ( statement . parameters , statement . body , restParamName ) ) ;
510514
515+ const body : tstl . Block = tstl . createBlock ( bodyStatements ) ;
516+
511517 const result =
512518 tstl . createVariableAssignmentStatement (
513519 tstl . createTableIndexExpression (
@@ -531,9 +537,80 @@ export class LuaTransformer {
531537 return result ;
532538 }
533539
540+ public transformGetAccessorDeclaration (
541+ getAccessor : ts . GetAccessorDeclaration , className : tstl . Identifier ) : tstl . VariableAssignmentStatement {
542+
543+ const name = this . transformIdentifier ( getAccessor . name as ts . Identifier ) ;
544+
545+ const accessorFunction =
546+ tstl . createFunctionExpression (
547+ tstl . createBlock ( this . transformFunctionBody ( getAccessor . parameters , getAccessor . body ) ) ,
548+ [ this . selfIdentifier ]
549+ ) ;
550+
551+ return tstl . createVariableAssignmentStatement (
552+ tstl . createTableIndexExpression ( className , tstl . createIdentifier ( "get__" + name . text ) ) ,
553+ accessorFunction
554+ ) ;
555+
556+ }
557+
558+ public transformSetAccessorDeclaration (
559+ setAccessor : ts . SetAccessorDeclaration , className : tstl . Identifier ) : tstl . VariableAssignmentStatement {
560+
561+ const name = this . transformIdentifier ( setAccessor . name as ts . Identifier ) ;
562+
563+ const [ params , dot , restParam ] = this . transformParameters ( setAccessor . parameters , this . selfIdentifier ) ;
564+
565+ const accessorFunction =
566+ tstl . createFunctionExpression (
567+ tstl . createBlock ( this . transformFunctionBody ( setAccessor . parameters , setAccessor . body , restParam ) ) ,
568+ params ,
569+ dot ,
570+ restParam
571+ ) ;
572+
573+ return tstl . createVariableAssignmentStatement (
574+ tstl . createTableIndexExpression ( className , tstl . createIdentifier ( "set__" + name . text ) ) ,
575+ accessorFunction
576+ ) ;
577+ }
578+
579+ public transformMethodDeclaration (
580+ node : ts . MethodDeclaration , className : tstl . Identifier ) : tstl . VariableAssignmentStatement {
581+
582+ // Don't transpile methods without body (overload declarations)
583+ if ( ! node . body ) {
584+ return undefined ;
585+ }
586+
587+ const methodName = this . transformIdentifier ( node . name as ts . Identifier ) ;
588+ if ( methodName . text === "toString" ) {
589+ methodName . text = "__tostring" ;
590+ }
591+
592+ const type = this . checker . getTypeAtLocation ( node ) ;
593+ const context =
594+ tsHelper . getFunctionContextType ( type , this . checker ) !== ContextType . Void ? this . selfIdentifier : undefined ;
595+ const [ paramNames , dots , restParamName ] = this . transformParameters ( node . parameters , context ) ;
596+
597+ const functionExpression =
598+ tstl . createFunctionExpression (
599+ tstl . createBlock ( this . transformFunctionBody ( node . parameters , node . body , restParamName ) ) ,
600+ paramNames ,
601+ dots ,
602+ restParamName
603+ ) ;
604+
605+ return tstl . createVariableAssignmentStatement (
606+ tstl . createTableIndexExpression ( className , methodName ) ,
607+ functionExpression
608+ ) ;
609+ }
610+
534611 public transformParameters (
535612 parameters : ts . NodeArray < ts . ParameterDeclaration > ,
536- context : tstl . Identifier ) : [ tstl . Identifier [ ] , tstl . DotsLiteral , tstl . Identifier | undefined ] {
613+ context ? : tstl . Identifier ) : [ tstl . Identifier [ ] , tstl . DotsLiteral , tstl . Identifier | undefined ] {
537614
538615 // Build parameter string
539616 const paramNames : tstl . Identifier [ ] = [ ] ;
@@ -585,7 +662,7 @@ export class LuaTransformer {
585662 headerStatements . push ( this . createLocalOrGlobalDeclaration ( spreadIdentifier , spreadTable ) ) ;
586663 }
587664
588- const bodyStatements = body . statements . map ( this . transformStatement ) ;
665+ const bodyStatements = this . transformStatements ( body . statements ) ;
589666
590667 return headerStatements . concat ( bodyStatements ) ;
591668 }
0 commit comments