Skip to content

Commit 1cec443

Browse files
authored
String concat fix (#508)
* wrapping values in concat ops in tostring, when needed * test format fix * removed accidental import and changed strings in test
1 parent dccea87 commit 1cec443

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/LuaTransformer.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,10 @@ export class LuaTransformer {
23962396
return this.transformBinaryBitOperation(tsOriginal, left, right, operator);
23972397
default:
23982398
const luaOperator = this.transformBinaryOperator(operator, tsOriginal);
2399+
if (luaOperator === tstl.SyntaxKind.ConcatOperator) {
2400+
left = this.wrapInToStringForConcat(left);
2401+
right = this.wrapInToStringForConcat(right);
2402+
}
23992403
return tstl.createBinaryExpression(left, right, luaOperator, tsOriginal);
24002404
}
24012405
}
@@ -4091,8 +4095,7 @@ export class LuaTransformer {
40914095
}
40924096

40934097
expression.templateSpans.forEach(span => {
4094-
const expr = this.transformExpression(span.expression);
4095-
parts.push(tstl.createCallExpression(tstl.createIdentifier("tostring"), [expr]));
4098+
parts.push(this.wrapInToStringForConcat(this.transformExpression(span.expression)));
40964099

40974100
const text = tsHelper.escapeString(span.literal.text);
40984101
if (text.length > 0) {
@@ -4487,14 +4490,30 @@ export class LuaTransformer {
44874490

44884491
private wrapInFunctionCall(expression: tstl.Expression): tstl.FunctionExpression {
44894492
const returnStatement = tstl.createReturnStatement([expression]);
4490-
return tstl.createFunctionExpression(tstl.createBlock([returnStatement]));
4493+
return tstl.createFunctionExpression(
4494+
tstl.createBlock([returnStatement]),
4495+
undefined,
4496+
undefined,
4497+
undefined,
4498+
tstl.FunctionExpressionFlags.Inline
4499+
);
44914500
}
44924501

44934502
private wrapInTable(...expressions: tstl.Expression[]): tstl.ParenthesizedExpression {
44944503
const fields = expressions.map(e => tstl.createTableFieldExpression(e));
44954504
return tstl.createParenthesizedExpression(tstl.createTableExpression(fields));
44964505
}
44974506

4507+
private wrapInToStringForConcat(expression: tstl.Expression): tstl.Expression {
4508+
if (tstl.isStringLiteral(expression)
4509+
|| tstl.isNumericLiteral(expression)
4510+
|| (tstl.isBinaryExpression(expression) && expression.operator === tstl.SyntaxKind.ConcatOperator))
4511+
{
4512+
return expression;
4513+
}
4514+
return tstl.createCallExpression(tstl.createIdentifier("tostring"), [expression]);
4515+
}
4516+
44984517
private expressionPlusOne(expression: tstl.Expression): tstl.BinaryExpression {
44994518
if (tstl.isBinaryExpression(expression)) {
45004519
expression = tstl.createParenthesizedExpression(expression);

test/translation/__snapshots__/transformation.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local backQuoteInTemplateString = \\"\` \` \`\\";
1414
local escapedCharsInQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" \\\\' \`\\";
1515
local escapedCharsInDoubleQUotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" \\\\'\\";
1616
local escapedCharsInTemplateString = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" \\\\' \`\\";
17-
local nonEmptyTemplateString = \\"Level 0: \\\\n\\\\t \\" .. tostring(\\"Level 1: \\\\n\\\\t\\\\t \\" .. tostring(\\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. tostring(\\"Last level \\\\n --\\") .. \\" \\\\n --\\") .. \\" \\\\n --\\") .. \\" \\\\n --\\";"
17+
local nonEmptyTemplateString = \\"Level 0: \\\\n\\\\t \\" .. \\"Level 1: \\\\n\\\\t\\\\t \\" .. \\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. \\"Last level \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\";"
1818
`;
1919

2020
exports[`Transformation (classExtension1) 1`] = `

test/unit/string.spec.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,41 @@ test.each([
3434
{ a: "test", b: "hello", c: "bye" },
3535
{ a: "test", b: 42, c: "bye" },
3636
{ a: "test", b: 42, c: 12 },
37+
{ a: "test", b: 42, c: true },
38+
{ a: false, b: 42, c: 12 },
3739
])("Template Strings (%p)", ({ a, b, c }) => {
38-
const a1 = typeof a === "string" ? "'" + a + "'" : a;
39-
const b1 = typeof b === "string" ? "'" + b + "'" : b;
40-
const c1 = typeof c === "string" ? "'" + c + "'" : c;
40+
const a1 = typeof a === "string" ? `'${a}'` : a;
41+
const b1 = typeof b === "string" ? `'${b}'` : b;
42+
const c1 = typeof c === "string" ? `'${c}'` : c;
4143

42-
const result = util.transpileAndExecute(
43-
"let a = " + a1 + "; let b = " + b1 + "; let c = " + c1 + "; return `${a} ${b} test ${c}`;",
44-
);
44+
const result = util.transpileAndExecute(`
45+
let a = ${a1};
46+
let b = ${b1};
47+
let c = ${c1};
48+
return \`${a} ${b} test ${c}\`;
49+
`);
50+
51+
expect(result).toBe(`${a} ${b} test ${c}`);
52+
});
53+
54+
test.each([
55+
{ a: 12, b: 23, c: 43 },
56+
{ a: "test", b: "hello", c: "bye" },
57+
{ a: "test", b: 42, c: "bye" },
58+
{ a: "test", b: 42, c: 12 },
59+
{ a: "test", b: 42, c: true },
60+
{ a: false, b: 42, c: 12 },
61+
])("String Concat Operator (%p)", ({ a, b, c }) => {
62+
const a1 = typeof a === "string" ? `'${a}'` : a;
63+
const b1 = typeof b === "string" ? `'${b}'` : b;
64+
const c1 = typeof c === "string" ? `'${c}'` : c;
65+
66+
const result = util.transpileAndExecute(`
67+
let a = ${a1};
68+
let b = ${b1};
69+
let c = ${c1};
70+
return a + " " + b + " test " + c;
71+
`);
4572

4673
expect(result).toBe(`${a} ${b} test ${c}`);
4774
});

0 commit comments

Comments
 (0)