@@ -3320,7 +3320,7 @@ export class LuaTransformer {
33203320 const expression = this . expectExpression ( this . transformExpression ( element . initializer ) ) ;
33213321 properties . push ( tstl . createTableFieldExpression ( expression , name , element ) ) ;
33223322 } else if ( ts . isShorthandPropertyAssignment ( element ) ) {
3323- const identifier = this . transformIdentifier ( element . name ) ;
3323+ const identifier = this . transformIdentifierExpression ( element . name ) ;
33243324 properties . push ( tstl . createTableFieldExpression ( identifier , name , element ) ) ;
33253325 } else if ( ts . isMethodDeclaration ( element ) ) {
33263326 const expression = this . expectExpression ( this . transformFunctionExpression ( element ) ) ;
@@ -3523,6 +3523,14 @@ export class LuaTransformer {
35233523 ) ;
35243524 }
35253525
3526+ const expressionType = this . checker . getTypeAtLocation ( node . expression ) ;
3527+ if ( tsHelper . isStandardLibraryType ( expressionType , undefined , this . program ) ) {
3528+ const result = this . transformGlobalFunctionCall ( node ) ;
3529+ if ( result ) {
3530+ return result ;
3531+ }
3532+ }
3533+
35263534 const callPath = this . expectExpression ( this . transformExpression ( node . expression ) ) ;
35273535 const signatureDeclaration = signature && signature . getDeclaration ( ) ;
35283536 if ( signatureDeclaration
@@ -3534,15 +3542,35 @@ export class LuaTransformer {
35343542 parameters = this . transformArguments ( node . arguments , signature , context ) ;
35353543 }
35363544
3537- const expressionType = this . checker . getTypeAtLocation ( node . expression ) ;
3538- if ( tsHelper . isStandardLibraryType ( expressionType , "SymbolConstructor" , this . program ) ) {
3539- return this . transformLuaLibFunction ( LuaLibFeature . Symbol , node , ...parameters ) ;
3540- }
3541-
35423545 const callExpression = tstl . createCallExpression ( callPath , parameters , node ) ;
35433546 return wrapResult ? this . wrapInTable ( callExpression ) : callExpression ;
35443547 }
35453548
3549+ private transformGlobalFunctionCall ( node : ts . CallExpression ) : ExpressionVisitResult {
3550+ const signature = this . checker . getResolvedSignature ( node ) ;
3551+ const parameters = this . transformArguments ( node . arguments , signature ) ;
3552+
3553+ const expressionType = this . checker . getTypeAtLocation ( node . expression ) ;
3554+ const name = expressionType . symbol . name ;
3555+ switch ( name ) {
3556+ case "SymbolConstructor" :
3557+ return this . transformLuaLibFunction ( LuaLibFeature . Symbol , node , ...parameters ) ;
3558+ case "NumberConstructor" :
3559+ return this . transformLuaLibFunction ( LuaLibFeature . Number , node , ...parameters ) ;
3560+ case "isNaN" :
3561+ case "isFinite" :
3562+ const numberParameters = tsHelper . isNumberType ( expressionType )
3563+ ? parameters
3564+ : [ this . transformLuaLibFunction ( LuaLibFeature . Number , undefined , ...parameters ) ] ;
3565+
3566+ return this . transformLuaLibFunction (
3567+ name === "isNaN" ? LuaLibFeature . NumberIsNaN : LuaLibFeature . NumberIsFinite ,
3568+ node ,
3569+ ...numberParameters
3570+ ) ;
3571+ }
3572+ }
3573+
35463574 public transformPropertyCall ( node : ts . CallExpression ) : ExpressionVisitResult {
35473575 let parameters : tstl . Expression [ ] = [ ] ;
35483576
@@ -3580,6 +3608,10 @@ export class LuaTransformer {
35803608 return this . transformSymbolCallExpression ( node ) ;
35813609 }
35823610
3611+ if ( tsHelper . isStandardLibraryType ( ownerType , "NumberConstructor" , this . program ) ) {
3612+ return this . transformNumberCallExpression ( node ) ;
3613+ }
3614+
35833615 const classDecorators = tsHelper . getCustomDecorators ( ownerType , this . checker ) ;
35843616
35853617 if ( classDecorators . has ( DecoratorKind . LuaTable ) ) {
@@ -4290,6 +4322,26 @@ export class LuaTransformer {
42904322 }
42914323 }
42924324
4325+ // Transpile a Number._ property
4326+ private transformNumberCallExpression ( expression : ts . CallExpression ) : tstl . CallExpression {
4327+ const method = expression . expression as ts . PropertyAccessExpression ;
4328+ const parameters = this . transformArguments ( expression . arguments ) ;
4329+ const methodName = method . name . escapedText ;
4330+
4331+ switch ( methodName ) {
4332+ case "isNaN" :
4333+ return this . transformLuaLibFunction ( LuaLibFeature . NumberIsNaN , expression , ...parameters ) ;
4334+ case "isFinite" :
4335+ return this . transformLuaLibFunction ( LuaLibFeature . NumberIsFinite , expression , ...parameters ) ;
4336+ default :
4337+ throw TSTLErrors . UnsupportedForTarget (
4338+ `number property ${ methodName } ` ,
4339+ this . luaTarget ,
4340+ expression
4341+ ) ;
4342+ }
4343+ }
4344+
42934345 private validateLuaTableCall (
42944346 expression : ts . CallExpression & { expression : ts . PropertyAccessExpression } ,
42954347 isWithinExpressionStatement : boolean
@@ -4575,18 +4627,13 @@ export class LuaTransformer {
45754627 }
45764628
45774629 private getIdentifierText ( identifier : ts . Identifier ) : string {
4578- let escapedText = identifier . escapedText as string ;
4579- const underScoreCharCode = "_" . charCodeAt ( 0 ) ;
4580- if ( escapedText . length >= 3 && escapedText . charCodeAt ( 0 ) === underScoreCharCode &&
4581- escapedText . charCodeAt ( 1 ) === underScoreCharCode && escapedText . charCodeAt ( 2 ) === underScoreCharCode ) {
4582- escapedText = escapedText . substr ( 1 ) ;
4583- }
4630+ const text = ts . idText ( identifier ) ;
45844631
4585- if ( this . luaKeywords . has ( escapedText ) ) {
4632+ if ( this . luaKeywords . has ( text ) ) {
45864633 throw TSTLErrors . KeywordIdentifier ( identifier ) ;
45874634 }
45884635
4589- return escapedText ;
4636+ return text ;
45904637 }
45914638
45924639 public transformIdentifier ( expression : ts . Identifier ) : tstl . Identifier {
@@ -4597,16 +4644,34 @@ export class LuaTransformer {
45974644 // at some point.
45984645 }
45994646
4600- const escapedText = this . getIdentifierText ( expression ) ;
4647+ const text = this . getIdentifierText ( expression ) ;
46014648 const symbolId = this . getIdentifierSymbolId ( expression ) ;
4602- return tstl . createIdentifier ( escapedText , expression , symbolId ) ;
4649+ return tstl . createIdentifier ( text , expression , symbolId ) ;
46034650 }
46044651
4605- private transformIdentifierExpression ( expression : ts . Identifier ) : tstl . IdentifierOrTableIndexExpression {
4652+ private transformIdentifierExpression ( expression : ts . Identifier ) : tstl . Expression {
46064653 const identifier = this . transformIdentifier ( expression ) ;
46074654 if ( this . isIdentifierExported ( identifier ) ) {
46084655 return this . createExportedIdentifier ( identifier ) ;
46094656 }
4657+
4658+ switch ( this . getIdentifierText ( expression ) ) {
4659+ case "NaN" :
4660+ return tstl . createParenthesizedExpression (
4661+ tstl . createBinaryExpression (
4662+ tstl . createNumericLiteral ( 0 ) ,
4663+ tstl . createNumericLiteral ( 0 ) ,
4664+ tstl . SyntaxKind . DivisionOperator ,
4665+ expression
4666+ )
4667+ ) ;
4668+
4669+ case "Infinity" :
4670+ const math = tstl . createIdentifier ( "math" ) ;
4671+ const huge = tstl . createStringLiteral ( "huge" ) ;
4672+ return tstl . createTableIndexExpression ( math , huge , expression ) ;
4673+ }
4674+
46104675 return identifier ;
46114676 }
46124677
0 commit comments