Skip to content

Commit c36222f

Browse files
committed
Fixed string concat and added tests for it
1 parent ddccc06 commit c36222f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Transpiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,11 @@ export class LuaTranspiler {
558558
case ts.SyntaxKind.PlusToken:
559559
// Replace string + with ..
560560
const typeLeft = this.checker.getTypeAtLocation(node.left);
561+
const typeRight = this.checker.getTypeAtLocation(node.right);
561562
if (typeLeft.flags & ts.TypeFlags.String || ts.isStringLiteral(node.left))
562563
return lhs + ".." + rhs;
564+
if (typeRight.flags & ts.TypeFlags.String || ts.isStringLiteral(node.right))
565+
return lhs + ".." + rhs;
563566
default:
564567
result = lhs + this.transpileOperator(node.operatorToken) + rhs;
565568
}

test/integration/lua/string.spec.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,30 @@ export class StringTests {
4242
Expect(result).toBe(inp.replace(searchValue, replaceValue));
4343
}
4444

45+
@TestCase(["", ""], "")
46+
@TestCase(["hello", "test"], "hellotest")
47+
@TestCase(["hello", "test", "bye"], "hellotestbye")
48+
@TestCase(["hello", 42], "hello42")
49+
@TestCase([42, "hello"], "42hello")
50+
@Test("string.concat[+]")
51+
public concat(inp: any[], expected: string) {
52+
// Transpile
53+
let lua = util.transpileString(
54+
`return '${inp.join("' + '")}'`,
55+
util.dummyTypes.String
56+
);
57+
58+
// Execute
59+
let result = util.executeLua(lua);
60+
61+
// Assert
62+
Expect(result).toBe(expected);
63+
}
64+
4565
@TestCase("hello test", new RegExp("123", "g"), "")
4666
@IgnoreTest()
4767
@Test("string.replace[Regex]")
48-
public replaceRegex<T>(inp: string, searchValue: string, replaceValue: string) {
68+
public replaceRegex(inp: string, searchValue: string, replaceValue: string) {
4969
// Transpile
5070
let lua = util.transpileString(
5171
`return "${inp}".replace("${searchValue}", "${replaceValue}")`,
@@ -63,7 +83,7 @@ export class StringTests {
6383
@TestCase("hello test", "t")
6484
@TestCase("hello test", "h")
6585
@Test("string.indexOf")
66-
public indexOf<T>(inp: string, searchValue: string) {
86+
public indexOf(inp: string, searchValue: string) {
6787
// Transpile
6888
let lua = util.transpileString(
6989
`return "${inp}".indexOf("${searchValue}")`,
@@ -82,7 +102,7 @@ export class StringTests {
82102
@TestCase("hello test", 1, 2)
83103
@TestCase("hello test", 1, 5)
84104
@Test("string.substring")
85-
public substring<T>(inp: string, start: number, end?: number) {
105+
public substring(inp: string, start: number, end?: number) {
86106
// Transpile
87107
let paramStr = end ? `${start}, ${end}` : `${start}`;
88108
let lua = util.transpileString(

0 commit comments

Comments
 (0)