@@ -7,6 +7,26 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
77import { PropertyCallExpression , transformArguments } from "../visitors/call" ;
88import { transformIdentifier } from "../visitors/identifier" ;
99
10+ const luaStringMethodNames = new Set ( [
11+ "byte" ,
12+ "char" ,
13+ "dump" ,
14+ "find" ,
15+ "format" ,
16+ "gmatch" ,
17+ "gsub" ,
18+ "len" ,
19+ "lower" ,
20+ "match" ,
21+ "pack" ,
22+ "packsize" ,
23+ "rep" ,
24+ "reverse" ,
25+ "sub" ,
26+ "unpack" ,
27+ "upper" ,
28+ ] ) ;
29+
1030function createStringCall ( methodName : string , tsOriginal : ts . Node , ...params : lua . Expression [ ] ) : lua . CallExpression {
1131 const stringIdentifier = lua . createIdentifier ( "string" ) ;
1232 return lua . createCallExpression (
@@ -124,40 +144,29 @@ export function transformStringPrototypeCall(
124144 return transformLuaLibFunction ( context , LuaLibFeature . StringPadStart , node , caller , ...params ) ;
125145 case "padEnd" :
126146 return transformLuaLibFunction ( context , LuaLibFeature . StringPadEnd , node , caller , ...params ) ;
147+ }
127148
128- case "byte" :
129- case "char" :
130- case "dump" :
131- case "find" :
132- case "format" :
133- case "gmatch" :
134- case "gsub" :
135- case "len" :
136- case "lower" :
137- case "match" :
138- case "pack" :
139- case "packsize" :
140- case "rep" :
141- case "reverse" :
142- case "sub" :
143- case "unpack" :
144- case "upper" :
145- // Allow lua's string instance methods
146- let stringVariable = context . transformExpression ( expression . expression ) ;
147- if ( ts . isStringLiteralLike ( expression . expression ) ) {
148- // "foo":method() needs to be ("foo"):method()
149- stringVariable = lua . createParenthesizedExpression ( stringVariable ) ;
150- }
149+ // Allow lua's string instance methods
150+ if ( isLuaStringMethod ( expressionName ) ) {
151+ let stringVariable = context . transformExpression ( expression . expression ) ;
152+ if ( ts . isStringLiteralLike ( expression . expression ) ) {
153+ // "foo":method() needs to be ("foo"):method()
154+ stringVariable = lua . createParenthesizedExpression ( stringVariable ) ;
155+ }
151156
152- return lua . createMethodCallExpression (
153- stringVariable ,
154- transformIdentifier ( context , expression . name ) ,
155- params ,
156- node
157- ) ;
158- default :
159- throw UnsupportedProperty ( "string" , expressionName , node ) ;
157+ return lua . createMethodCallExpression (
158+ stringVariable ,
159+ transformIdentifier ( context , expression . name ) ,
160+ params ,
161+ node
162+ ) ;
160163 }
164+
165+ throw UnsupportedProperty ( "string" , expressionName , node ) ;
166+ }
167+
168+ export function isLuaStringMethod ( method : string ) : boolean {
169+ return luaStringMethodNames . has ( method ) ;
161170}
162171
163172export function transformStringConstructorCall (
0 commit comments