@@ -264,11 +264,7 @@ export class LuaTransformer {
264264 exportedIdentifier = this . transformIdentifier ( specifier . propertyName ) ;
265265 } else {
266266 const exportedSymbol = this . checker . getExportSpecifierLocalTargetSymbol ( specifier ) ;
267- if ( exportedSymbol !== undefined ) {
268- exportedIdentifier = this . createIdentifierFromSymbol ( exportedSymbol , specifier . name ) ;
269- } else {
270- exportedIdentifier = this . transformIdentifier ( specifier . name ) ;
271- }
267+ exportedIdentifier = this . createShorthandIdentifier ( exportedSymbol , specifier . name ) ;
272268 }
273269
274270 return tstl . createAssignmentStatement (
@@ -483,10 +479,13 @@ export class LuaTransformer {
483479 }
484480
485481 let className : tstl . Identifier ;
482+ let classNameText : string ;
486483 if ( nameOverride !== undefined ) {
487484 className = nameOverride ;
485+ classNameText = nameOverride . text ;
488486 } else if ( statement . name !== undefined ) {
489487 className = this . transformIdentifier ( statement . name ) ;
488+ classNameText = statement . name . text ;
490489 } else {
491490 throw TSTLErrors . MissingClassName ( statement ) ;
492491 }
@@ -593,6 +592,7 @@ export class LuaTransformer {
593592 statement ,
594593 className ,
595594 localClassName ,
595+ classNameText ,
596596 extendsType
597597 ) ;
598598 result . push ( ...classCreationMethods ) ;
@@ -727,6 +727,7 @@ export class LuaTransformer {
727727 statement : ts . ClassLikeDeclarationBase ,
728728 className : tstl . Identifier ,
729729 localClassName : tstl . Identifier ,
730+ classNameText : string ,
730731 extendsType ?: ts . Type
731732 ) : tstl . Statement [ ]
732733 {
@@ -752,7 +753,7 @@ export class LuaTransformer {
752753 result . push (
753754 tstl . createAssignmentStatement (
754755 tstl . createTableIndexExpression ( tstl . cloneIdentifier ( localClassName ) , tstl . createStringLiteral ( "name" ) ) ,
755- tstl . createStringLiteral ( localClassName . text ) ,
756+ tstl . createStringLiteral ( classNameText ) ,
756757 statement
757758 )
758759 ) ;
@@ -3440,15 +3441,8 @@ export class LuaTransformer {
34403441 properties . push ( tstl . createTableFieldExpression ( expression , name , element ) ) ;
34413442
34423443 } else if ( ts . isShorthandPropertyAssignment ( element ) ) {
3443- let identifier : tstl . Expression | undefined ;
34443444 const valueSymbol = this . checker . getShorthandAssignmentValueSymbol ( element ) ;
3445- if ( valueSymbol !== undefined
3446- // Ignore declarations for things like NaN
3447- && ! tsHelper . isStandardLibraryDeclaration ( valueSymbol . valueDeclaration , this . program ) ) {
3448- identifier = this . createIdentifierFromSymbol ( valueSymbol , element . name ) ;
3449- } else {
3450- identifier = this . transformIdentifierExpression ( element . name ) ;
3451- }
3445+ let identifier = this . createShorthandIdentifier ( valueSymbol , element . name ) ;
34523446 if ( tstl . isIdentifier ( identifier ) && valueSymbol !== undefined && this . isSymbolExported ( valueSymbol ) ) {
34533447 identifier = this . createExportedIdentifier ( identifier ) ;
34543448 }
@@ -5265,34 +5259,62 @@ export class LuaTransformer {
52655259 return tstl . createBinaryExpression ( expression , tstl . createNumericLiteral ( 1 ) , tstl . SyntaxKind . AdditionOperator ) ;
52665260 }
52675261
5268- protected createIdentifierFromSymbol ( symbol : ts . Symbol , tsOriginal ?: ts . Node ) : tstl . Identifier {
5269- const name = this . hasUnsafeSymbolName ( symbol )
5270- ? this . createSafeName ( symbol . name )
5271- : symbol . name ;
5272- return tstl . createIdentifier ( name , tsOriginal , this . symbolIds . get ( symbol ) ) ;
5262+ protected createShorthandIdentifier (
5263+ valueSymbol : ts . Symbol | undefined ,
5264+ propertyIdentifier : ts . Identifier
5265+ ) : tstl . Expression
5266+ {
5267+ let name : string ;
5268+ if ( valueSymbol !== undefined ) {
5269+ name = this . hasUnsafeSymbolName ( valueSymbol , propertyIdentifier )
5270+ ? this . createSafeName ( valueSymbol . name )
5271+ : valueSymbol . name ;
5272+
5273+ } else {
5274+ const propertyName = this . getIdentifierText ( propertyIdentifier ) ;
5275+ if ( luaKeywords . has ( propertyName ) || ! tsHelper . isValidLuaIdentifier ( propertyName ) ) {
5276+ // Catch ambient declarations of identifiers with bad names
5277+ throw TSTLErrors . InvalidAmbientIdentifierName ( propertyIdentifier ) ;
5278+ }
5279+ name = this . hasUnsafeIdentifierName ( propertyIdentifier )
5280+ ? this . createSafeName ( propertyName )
5281+ : propertyName ;
5282+ }
5283+
5284+ const identifier = this . transformIdentifierExpression ( ts . createIdentifier ( name ) ) ;
5285+ tstl . setNodeOriginal ( identifier , propertyIdentifier ) ;
5286+ if ( valueSymbol !== undefined && tstl . isIdentifier ( identifier ) ) {
5287+ identifier . symbolId = this . symbolIds . get ( valueSymbol ) ;
5288+ }
5289+ return identifier ;
52735290 }
52745291
52755292 protected isUnsafeName ( name : string ) : boolean {
52765293 return luaKeywords . has ( name ) || luaBuiltins . has ( name ) || ! tsHelper . isValidLuaIdentifier ( name ) ;
52775294 }
52785295
5279- protected hasUnsafeSymbolName ( symbol : ts . Symbol ) : boolean {
5280- if ( luaKeywords . has ( symbol . name ) || luaBuiltins . has ( symbol . name ) ) {
5281- // lua keywords are only unsafe when non-ambient and not exported
5282- const isNonAmbient = symbol . declarations . find ( d => ! tsHelper . isAmbient ( d ) ) !== undefined ;
5283- return isNonAmbient && ! this . isSymbolExported ( symbol ) ;
5296+ protected hasUnsafeSymbolName ( symbol : ts . Symbol , tsOriginal ?: ts . Identifier ) : boolean {
5297+ const isLuaKeyword = luaKeywords . has ( symbol . name ) ;
5298+ const isInvalidIdentifier = ! tsHelper . isValidLuaIdentifier ( symbol . name ) ;
5299+ const isAmbient = symbol . declarations . some ( d => tsHelper . isAmbient ( d ) ) ;
5300+ if ( ( isLuaKeyword || isInvalidIdentifier ) && isAmbient ) {
5301+ // Catch ambient declarations of identifiers with bad names
5302+ throw TSTLErrors . InvalidAmbientIdentifierName ( tsOriginal || ts . createIdentifier ( symbol . name ) ) ;
5303+ }
5304+
5305+ if ( this . isUnsafeName ( symbol . name ) ) {
5306+ // only unsafe when non-ambient and not exported
5307+ return ! isAmbient && ! this . isSymbolExported ( symbol ) ;
52845308 }
5285- return this . isUnsafeName ( symbol . name ) ;
5309+ return false ;
52865310 }
52875311
52885312 protected hasUnsafeIdentifierName ( identifier : ts . Identifier ) : boolean {
52895313 const symbol = this . checker . getSymbolAtLocation ( identifier ) ;
52905314 if ( symbol !== undefined ) {
5291- if ( luaKeywords . has ( symbol . name ) && symbol . declarations . find ( d => ! tsHelper . isAmbient ( d ) ) === undefined ) {
5292- // Catch ambient declarations of identifiers with lua keyword names
5293- throw TSTLErrors . InvalidAmbientLuaKeywordIdentifier ( identifier ) ;
5294- }
5295- return this . hasUnsafeSymbolName ( symbol ) ;
5315+ return this . hasUnsafeSymbolName ( symbol , identifier ) ;
5316+ } else if ( luaKeywords . has ( identifier . text ) || ! tsHelper . isValidLuaIdentifier ( identifier . text ) ) {
5317+ throw TSTLErrors . InvalidAmbientIdentifierName ( identifier ) ;
52965318 }
52975319 return false ;
52985320 }
@@ -5556,7 +5578,7 @@ export class LuaTransformer {
55565578 declaration : ts . ClassLikeDeclaration
55575579 ) : tstl . AssignmentStatement | undefined {
55585580 const className = declaration . name !== undefined
5559- ? this . transformIdentifier ( declaration . name )
5581+ ? this . addExportToIdentifier ( this . transformIdentifier ( declaration . name ) )
55605582 : tstl . createAnonymousIdentifier ( ) ;
55615583
55625584 const decorators = declaration . decorators ;
@@ -5585,3 +5607,4 @@ export class LuaTransformer {
55855607 ) ;
55865608 }
55875609}
5610+
0 commit comments