From 659c50272d1bc03ba17b18650f44b82b83461137 Mon Sep 17 00:00:00 2001 From: Tom <26638278+tomblind@users.noreply.github.com> Date: Thu, 4 Apr 2019 17:15:43 -0600 Subject: [PATCH 1/3] wrapping values in concat ops in tostring, when needed --- src/LuaTransformer.ts | 26 +++++++++++++-- .../__snapshots__/transformation.spec.ts.snap | 2 +- test/unit/string.spec.ts | 33 +++++++++++++++++-- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 0df3f10ed..46fab464c 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -6,6 +6,7 @@ import * as tstl from "./LuaAST"; import { LuaLibFeature } from "./LuaLib"; import { ContextType, TSHelper as tsHelper } from "./TSHelper"; import { TSTLErrors } from "./TSTLErrors"; +import { expression } from "@babel/template"; export type StatementVisitResult = tstl.Statement | tstl.Statement[] | undefined; export type ExpressionVisitResult = tstl.Expression | undefined; @@ -2396,6 +2397,10 @@ export class LuaTransformer { return this.transformBinaryBitOperation(tsOriginal, left, right, operator); default: const luaOperator = this.transformBinaryOperator(operator, tsOriginal); + if (luaOperator === tstl.SyntaxKind.ConcatOperator) { + left = this.wrapInToStringForConcat(left); + right = this.wrapInToStringForConcat(right); + } return tstl.createBinaryExpression(left, right, luaOperator, tsOriginal); } } @@ -4091,8 +4096,7 @@ export class LuaTransformer { } expression.templateSpans.forEach(span => { - const expr = this.transformExpression(span.expression); - parts.push(tstl.createCallExpression(tstl.createIdentifier("tostring"), [expr])); + parts.push(this.wrapInToStringForConcat(this.transformExpression(span.expression))); const text = tsHelper.escapeString(span.literal.text); if (text.length > 0) { @@ -4487,7 +4491,13 @@ export class LuaTransformer { private wrapInFunctionCall(expression: tstl.Expression): tstl.FunctionExpression { const returnStatement = tstl.createReturnStatement([expression]); - return tstl.createFunctionExpression(tstl.createBlock([returnStatement])); + return tstl.createFunctionExpression( + tstl.createBlock([returnStatement]), + undefined, + undefined, + undefined, + tstl.FunctionExpressionFlags.Inline + ); } private wrapInTable(...expressions: tstl.Expression[]): tstl.ParenthesizedExpression { @@ -4495,6 +4505,16 @@ export class LuaTransformer { return tstl.createParenthesizedExpression(tstl.createTableExpression(fields)); } + private wrapInToStringForConcat(expression: tstl.Expression): tstl.Expression { + if (tstl.isStringLiteral(expression) + || tstl.isNumericLiteral(expression) + || (tstl.isBinaryExpression(expression) && expression.operator === tstl.SyntaxKind.ConcatOperator)) + { + return expression; + } + return tstl.createCallExpression(tstl.createIdentifier("tostring"), [expression]); + } + private expressionPlusOne(expression: tstl.Expression): tstl.BinaryExpression { if (tstl.isBinaryExpression(expression)) { expression = tstl.createParenthesizedExpression(expression); diff --git a/test/translation/__snapshots__/transformation.spec.ts.snap b/test/translation/__snapshots__/transformation.spec.ts.snap index b0baaa3dd..2a8c00c78 100644 --- a/test/translation/__snapshots__/transformation.spec.ts.snap +++ b/test/translation/__snapshots__/transformation.spec.ts.snap @@ -14,7 +14,7 @@ local backQuoteInTemplateString = \\"\` \` \`\\"; local escapedCharsInQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" \\\\' \`\\"; local escapedCharsInDoubleQUotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" \\\\'\\"; local escapedCharsInTemplateString = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" \\\\' \`\\"; -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 --\\";" +local nonEmptyTemplateString = \\"Level 0: \\\\n\\\\t \\" .. \\"Level 1: \\\\n\\\\t\\\\t \\" .. \\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. \\"Last level \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\";" `; exports[`Transformation (classExtension1) 1`] = ` diff --git a/test/unit/string.spec.ts b/test/unit/string.spec.ts index 2b7cd4362..7b35cb921 100644 --- a/test/unit/string.spec.ts +++ b/test/unit/string.spec.ts @@ -34,13 +34,42 @@ test.each([ { a: "test", b: "hello", c: "bye" }, { a: "test", b: 42, c: "bye" }, { a: "test", b: 42, c: 12 }, + { a: "test", b: 42, c: true }, + { a: false, b: 42, c: 12 }, ])("Template Strings (%p)", ({ a, b, c }) => { const a1 = typeof a === "string" ? "'" + a + "'" : a; const b1 = typeof b === "string" ? "'" + b + "'" : b; const c1 = typeof c === "string" ? "'" + c + "'" : c; - const result = util.transpileAndExecute( - "let a = " + a1 + "; let b = " + b1 + "; let c = " + c1 + "; return `${a} ${b} test ${c}`;", + const result = util.transpileAndExecute(` + let a = ${a1}; + let b = ${b1}; + let c = ${c1}; + return \`${a} ${b} test ${c}\`; + ` + ); + + expect(result).toBe(`${a} ${b} test ${c}`); +}); + +test.each([ + { a: 12, b: 23, c: 43 }, + { a: "test", b: "hello", c: "bye" }, + { a: "test", b: 42, c: "bye" }, + { a: "test", b: 42, c: 12 }, + { a: "test", b: 42, c: true }, + { a: false, b: 42, c: 12 }, +])("String Concat Operator (%p)", ({ a, b, c }) => { + const a1 = typeof a === "string" ? "'" + a + "'" : a; + const b1 = typeof b === "string" ? "'" + b + "'" : b; + const c1 = typeof c === "string" ? "'" + c + "'" : c; + + const result = util.transpileAndExecute(` + let a = ${a1}; + let b = ${b1}; + let c = ${c1}; + return a + " " + b + " test " + c; + ` ); expect(result).toBe(`${a} ${b} test ${c}`); From ec676b979fc7d781ec155562fd3cc03ed078d68b Mon Sep 17 00:00:00 2001 From: Tom <26638278+tomblind@users.noreply.github.com> Date: Thu, 4 Apr 2019 17:22:40 -0600 Subject: [PATCH 2/3] test format fix --- test/unit/string.spec.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/unit/string.spec.ts b/test/unit/string.spec.ts index 7b35cb921..0e42f01d5 100644 --- a/test/unit/string.spec.ts +++ b/test/unit/string.spec.ts @@ -46,8 +46,7 @@ test.each([ let b = ${b1}; let c = ${c1}; return \`${a} ${b} test ${c}\`; - ` - ); + `); expect(result).toBe(`${a} ${b} test ${c}`); }); @@ -69,8 +68,7 @@ test.each([ let b = ${b1}; let c = ${c1}; return a + " " + b + " test " + c; - ` - ); + `); expect(result).toBe(`${a} ${b} test ${c}`); }); From 747a3d8599f3ad41a962cbb91020665f748af97f Mon Sep 17 00:00:00 2001 From: Tom <26638278+tomblind@users.noreply.github.com> Date: Fri, 5 Apr 2019 06:15:47 -0600 Subject: [PATCH 3/3] removed accidental import and changed strings in test --- src/LuaTransformer.ts | 1 - test/unit/string.spec.ts | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 46fab464c..86f680583 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -6,7 +6,6 @@ import * as tstl from "./LuaAST"; import { LuaLibFeature } from "./LuaLib"; import { ContextType, TSHelper as tsHelper } from "./TSHelper"; import { TSTLErrors } from "./TSTLErrors"; -import { expression } from "@babel/template"; export type StatementVisitResult = tstl.Statement | tstl.Statement[] | undefined; export type ExpressionVisitResult = tstl.Expression | undefined; diff --git a/test/unit/string.spec.ts b/test/unit/string.spec.ts index 0e42f01d5..dfb494c62 100644 --- a/test/unit/string.spec.ts +++ b/test/unit/string.spec.ts @@ -37,9 +37,9 @@ test.each([ { a: "test", b: 42, c: true }, { a: false, b: 42, c: 12 }, ])("Template Strings (%p)", ({ a, b, c }) => { - const a1 = typeof a === "string" ? "'" + a + "'" : a; - const b1 = typeof b === "string" ? "'" + b + "'" : b; - const c1 = typeof c === "string" ? "'" + c + "'" : c; + const a1 = typeof a === "string" ? `'${a}'` : a; + const b1 = typeof b === "string" ? `'${b}'` : b; + const c1 = typeof c === "string" ? `'${c}'` : c; const result = util.transpileAndExecute(` let a = ${a1}; @@ -59,9 +59,9 @@ test.each([ { a: "test", b: 42, c: true }, { a: false, b: 42, c: 12 }, ])("String Concat Operator (%p)", ({ a, b, c }) => { - const a1 = typeof a === "string" ? "'" + a + "'" : a; - const b1 = typeof b === "string" ? "'" + b + "'" : b; - const c1 = typeof c === "string" ? "'" + c + "'" : c; + const a1 = typeof a === "string" ? `'${a}'` : a; + const b1 = typeof b === "string" ? `'${b}'` : b; + const c1 = typeof c === "string" ? `'${c}'` : c; const result = util.transpileAndExecute(` let a = ${a1};