Skip to content

Commit 2a8846a

Browse files
authored
enabled support for lua string instance methods (#449)
1 parent 714854e commit 2a8846a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/LuaTransformer.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,35 @@ export class LuaTransformer {
35133513
const firstParamPlusOne = this.expressionPlusOne(params[0]);
35143514
return this.createStringCall("byte", node, caller, firstParamPlusOne);
35153515
}
3516+
case "byte":
3517+
case "char":
3518+
case "dump":
3519+
case "find":
3520+
case "format":
3521+
case "gmatch":
3522+
case "gsub":
3523+
case "len":
3524+
case "lower":
3525+
case "match":
3526+
case "pack":
3527+
case "packsize":
3528+
case "rep":
3529+
case "reverse":
3530+
case "sub":
3531+
case "unpack":
3532+
case "upper":
3533+
// Allow lua's string instance methods
3534+
let stringVariable = this.transformExpression(expression.expression);
3535+
if (ts.isStringLiteral(expression.expression)) {
3536+
// "foo":method() needs to be ("foo"):method()
3537+
stringVariable = tstl.createParenthesizedExpression(stringVariable);
3538+
}
3539+
return tstl.createMethodCallExpression(
3540+
stringVariable,
3541+
this.transformIdentifier(expression.name),
3542+
params,
3543+
node
3544+
);
35163545
default:
35173546
throw TSTLErrors.UnsupportedProperty("string", expressionName, node);
35183547
}

test/unit/string.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ export class StringTests
1414
}).toThrowError(TranspileError, "Unsupported property on string: testThisIsNoMember");
1515
}
1616

17+
@Test("Suported lua string function")
18+
public stringSuportedLuaFunction(): void {
19+
Expect(util.transpileAndExecute(
20+
`return "test".upper()`,
21+
undefined,
22+
undefined,
23+
`interface String { upper(): string; }`
24+
)
25+
).toBe("TEST");
26+
}
27+
1728
@TestCase([])
1829
@TestCase([65])
1930
@TestCase([65, 66])

0 commit comments

Comments
 (0)