@@ -367,26 +367,66 @@ export class LuaTranspiler {
367367 }
368368
369369 transpileCallExpression ( node : ts . CallExpression ) : string {
370+ // Check for calls on primitives to override
371+ if ( ts . isPropertyAccessExpression ( node . expression ) ) {
372+ const type = this . checker . getTypeAtLocation ( node . expression . expression ) ;
373+ switch ( type . flags ) {
374+ case ts . TypeFlags . String :
375+ case ts . TypeFlags . StringLiteral :
376+ return this . transpileStringCallExpression ( node ) ;
377+ case ts . TypeFlags . Object :
378+ if ( tsEx . isArrayType ( type ) )
379+ return this . transpileArrayCallExpression ( node ) ;
380+ }
381+ }
382+
370383 let callPath = this . transpileExpression ( node . expression ) ;
371384 // Remove last . with :
372385 if ( callPath . indexOf ( "." ) > - 1 ) {
373386 callPath = callPath . substr ( 0 , callPath . lastIndexOf ( "." ) ) + ":" + callPath . substr ( callPath . lastIndexOf ( "." ) + 1 ) ;
374387 }
375388
389+ const params = this . transpileArguments ( node . arguments ) ;
390+ return `${ callPath } (${ params } )` ;
391+ }
392+
393+ transpileStringCallExpression ( node : ts . CallExpression ) : string {
394+ const expression = < ts . PropertyAccessExpression > node . expression ;
395+ const params = this . transpileArguments ( node . arguments ) ;
396+ const caller = this . transpileExpression ( expression . expression ) ;
397+ switch ( expression . name . escapedText ) {
398+ case "replace" :
399+ return `${ caller } :sub(${ params } )` ;
400+ default :
401+ throw new TranspileError ( "Unsupported string function: " + expression . name . escapedText , node ) ;
402+ }
403+ }
404+
405+ transpileArrayCallExpression ( node : ts . CallExpression ) : string {
406+ const expression = < ts . PropertyAccessExpression > node . expression ;
407+ const params = this . transpileArguments ( node . arguments ) ;
408+ const caller = this . transpileExpression ( expression . expression ) ;
409+ switch ( expression . name . escapedText ) {
410+ case "push" :
411+ return `table.insert(${ caller } , ${ params } )` ;
412+ default :
413+ throw new TranspileError ( "Unsupported array function: " + expression . name . escapedText , node ) ;
414+ }
415+ }
416+
417+ transpileArguments ( params : ts . NodeArray < ts . Expression > ) : string {
376418 const parameters = [ ] ;
377- node . arguments . forEach ( param => {
419+ params . forEach ( param => {
378420 parameters . push ( this . transpileExpression ( param ) ) ;
379421 } ) ;
380-
381- return `${ callPath } (${ parameters . join ( "," ) } )` ;
422+ return parameters . join ( "," ) ;
382423 }
383424
384425 transpilePropertyAccessExpression ( node : ts . PropertyAccessExpression ) : string {
385426 const property = node . name . text ;
386427
387428 // Check for primitive types to override
388429 const type = this . checker . getTypeAtLocation ( node . expression ) ;
389- console . log ( type . flags ) ;
390430 switch ( type . flags ) {
391431 case ts . TypeFlags . String :
392432 case ts . TypeFlags . StringLiteral :
0 commit comments