@@ -4,6 +4,7 @@ import { InvalidAmbientIdentifierName } from "./errors";
44import { isAmbientNode } from "./typescript" ;
55import { isSymbolExported } from "./export" ;
66
7+ export const isValidLuaIdentifier = ( name : string ) => ! luaKeywords . has ( name ) && / ^ [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * $ / . test ( name ) ;
78export const luaKeywords : ReadonlySet < string > = new Set ( [
89 "and" ,
910 "break" ,
@@ -28,7 +29,7 @@ export const luaKeywords: ReadonlySet<string> = new Set([
2829 "while" ,
2930] ) ;
3031
31- export const luaBuiltins : ReadonlySet < string > = new Set ( [
32+ const luaBuiltins : ReadonlySet < string > = new Set ( [
3233 "_G" ,
3334 "assert" ,
3435 "coroutine" ,
@@ -51,38 +52,31 @@ export const luaBuiltins: ReadonlySet<string> = new Set([
5152 "unpack" ,
5253] ) ;
5354
54- export const isValidLuaIdentifier = ( str : string ) => / ^ [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * $ / . test ( str ) ;
55-
56- export const isUnsafeName = ( name : string ) =>
57- luaKeywords . has ( name ) || luaBuiltins . has ( name ) || ! isValidLuaIdentifier ( name ) ;
55+ export const isUnsafeName = ( name : string ) => ! isValidLuaIdentifier ( name ) || luaBuiltins . has ( name ) ;
5856
5957export function hasUnsafeSymbolName (
6058 context : TransformationContext ,
6159 symbol : ts . Symbol ,
6260 tsOriginal : ts . Identifier
6361) : boolean {
64- const isLuaKeyword = luaKeywords . has ( symbol . name ) ;
65- const isInvalidIdentifier = ! isValidLuaIdentifier ( symbol . name ) ;
6662 const isAmbient = symbol . declarations && symbol . declarations . some ( d => isAmbientNode ( d ) ) ;
67- if ( ( isLuaKeyword || isInvalidIdentifier ) && isAmbient ) {
63+
64+ if ( ! isValidLuaIdentifier ( symbol . name ) && isAmbient ) {
6865 // Catch ambient declarations of identifiers with bad names
6966 throw InvalidAmbientIdentifierName ( tsOriginal ) ;
7067 }
7168
72- if ( isUnsafeName ( symbol . name ) ) {
73- // only unsafe when non-ambient and not exported
74- return ! isAmbient && ! isSymbolExported ( context , symbol ) ;
75- }
76-
77- return false ;
69+ // only unsafe when non-ambient and not exported
70+ return isUnsafeName ( symbol . name ) && ! isAmbient && ! isSymbolExported ( context , symbol ) ;
7871}
7972
8073export function hasUnsafeIdentifierName ( context : TransformationContext , identifier : ts . Identifier ) : boolean {
8174 const symbol = context . checker . getSymbolAtLocation ( identifier ) ;
82-
83- if ( symbol !== undefined ) {
75+ if ( symbol ) {
8476 return hasUnsafeSymbolName ( context , symbol , identifier ) ;
85- } else if ( luaKeywords . has ( identifier . text ) || ! isValidLuaIdentifier ( identifier . text ) ) {
77+ }
78+
79+ if ( ! isValidLuaIdentifier ( identifier . text ) ) {
8680 throw InvalidAmbientIdentifierName ( identifier ) ;
8781 }
8882
0 commit comments