From 8ebb5a9a5f8c3f0fabbaf7232f92eb560c190343 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Thu, 25 Jul 2019 09:39:44 +0500 Subject: [PATCH] Wrap expression statements based on transformed node --- src/LuaTransformer.ts | 19 ++++++------------- test/unit/expressions.spec.ts | 29 +++++++++++++++-------------- test/unit/math.spec.ts | 4 ++-- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index ac8f49578..4670d5c17 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -2104,14 +2104,6 @@ export class LuaTransformer { ); } - if (!ts.isCallLikeExpression(expression)) { - // Assign expression statements to dummy to make sure they're legal lua - return tstl.createVariableDeclarationStatement( - tstl.createAnonymousIdentifier(), - this.transformExpression(expression) - ); - } - if (ts.isCallExpression(expression) && ts.isPropertyAccessExpression(expression.expression)) { const ownerType = this.checker.getTypeAtLocation(expression.expression.expression); const classDecorators = tsHelper.getCustomDecorators(ownerType, this.checker); @@ -2126,7 +2118,11 @@ export class LuaTransformer { } } - return tstl.createExpressionStatement(this.transformExpression(expression)); + const result = this.transformExpression(expression); + return tstl.isCallExpression(result) || tstl.isMethodCallExpression(result) + ? tstl.createExpressionStatement(result) + : // Assign expression statements to dummy to make sure they're legal Lua + tstl.createVariableDeclarationStatement(tstl.createAnonymousIdentifier(), result); } public transformYieldExpression(expression: ts.YieldExpression): ExpressionVisitResult { @@ -4202,10 +4198,7 @@ export class LuaTransformer { const logCall1 = tstl.createCallExpression(log1, params); const e = tstl.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2); const div = tstl.createBinaryExpression(logCall1, e, tstl.SyntaxKind.DivisionOperator); - return ts.isExpressionStatement(node.parent) - ? // if used as a stand-alone statement, needs to be a call expression to be valid lua - this.createImmediatelyInvokedFunctionExpression([], div, node) - : tstl.createParenthesizedExpression(div, node); + return tstl.createParenthesizedExpression(div, node); } // math.log(1 + x) diff --git a/test/unit/expressions.spec.ts b/test/unit/expressions.spec.ts index 559ff3606..a2e902352 100644 --- a/test/unit/expressions.spec.ts +++ b/test/unit/expressions.spec.ts @@ -97,18 +97,18 @@ test.each([ }); test.each([ - { input: "~a", lua: "local ____ = bit.bnot(a)" }, - { input: "a&b", lua: "local ____ = bit.band(a, b)" }, + { input: "~a", lua: "bit.bnot(a)" }, + { input: "a&b", lua: "bit.band(a, b)" }, { input: "a&=b", lua: "a = bit.band(a, b)" }, - { input: "a|b", lua: "local ____ = bit.bor(a, b)" }, + { input: "a|b", lua: "bit.bor(a, b)" }, { input: "a|=b", lua: "a = bit.bor(a, b)" }, - { input: "a^b", lua: "local ____ = bit.bxor(a, b)" }, + { input: "a^b", lua: "bit.bxor(a, b)" }, { input: "a^=b", lua: "a = bit.bxor(a, b)" }, - { input: "a<>b", lua: "local ____ = bit.arshift(a, b)" }, + { input: "a>>b", lua: "bit.arshift(a, b)" }, { input: "a>>=b", lua: "a = bit.arshift(a, b)" }, - { input: "a>>>b", lua: "local ____ = bit.rshift(a, b)" }, + { input: "a>>>b", lua: "bit.rshift(a, b)" }, { input: "a>>>=b", lua: "a = bit.rshift(a, b)" }, ])("Bitop [JIT] (%p)", ({ input, lua }) => { const options = { luaTarget: tstl.LuaTarget.LuaJIT, luaLibImport: tstl.LuaLibImportKind.None }; @@ -116,18 +116,18 @@ test.each([ }); test.each([ - { input: "~a", lua: "local ____ = bit32.bnot(a)" }, - { input: "a&b", lua: "local ____ = bit32.band(a, b)" }, + { input: "~a", lua: "bit32.bnot(a)" }, + { input: "a&b", lua: "bit32.band(a, b)" }, { input: "a&=b", lua: "a = bit32.band(a, b)" }, - { input: "a|b", lua: "local ____ = bit32.bor(a, b)" }, + { input: "a|b", lua: "bit32.bor(a, b)" }, { input: "a|=b", lua: "a = bit32.bor(a, b)" }, - { input: "a^b", lua: "local ____ = bit32.bxor(a, b)" }, + { input: "a^b", lua: "bit32.bxor(a, b)" }, { input: "a^=b", lua: "a = bit32.bxor(a, b)" }, - { input: "a<>b", lua: "local ____ = bit32.arshift(a, b)" }, + { input: "a>>b", lua: "bit32.arshift(a, b)" }, { input: "a>>=b", lua: "a = bit32.arshift(a, b)" }, - { input: "a>>>b", lua: "local ____ = bit32.rshift(a, b)" }, + { input: "a>>>b", lua: "bit32.rshift(a, b)" }, { input: "a>>>=b", lua: "a = bit32.rshift(a, b)" }, ])("Bitop [5.2] (%p)", ({ input, lua }) => { const options = { luaTarget: tstl.LuaTarget.Lua52, luaLibImport: tstl.LuaLibImportKind.None }; @@ -518,6 +518,7 @@ test.each([ "foo as Function", "Math.log2(2)", "Math.log10(2)", + '"".indexOf("")', ])("Expression statements (%p)", input => { const code = ` function foo() { return 17; } diff --git a/test/unit/math.spec.ts b/test/unit/math.spec.ts index 9ff3442f7..84b1b071f 100644 --- a/test/unit/math.spec.ts +++ b/test/unit/math.spec.ts @@ -5,8 +5,8 @@ test.each([ { inp: "Math.sin()", expected: "math.sin()" }, { inp: "Math.min()", expected: "math.min()" }, { inp: "Math.atan2(2, 3)", expected: "math.atan(2 / 3)" }, - { inp: "Math.log2(3)", expected: `(function() return math.log(3) / ${Math.LN2} end)()` }, - { inp: "Math.log10(3)", expected: `(function() return math.log(3) / ${Math.LN10} end)()` }, + { inp: "Math.log2(3)", expected: `local ____ = (math.log(3) / ${Math.LN2})` }, + { inp: "Math.log10(3)", expected: `local ____ = (math.log(3) / ${Math.LN10})` }, { inp: "const x = Math.log2(3)", expected: `local x = (math.log(3) / ${Math.LN2})` }, { inp: "const x = Math.log10(3)", expected: `local x = (math.log(3) / ${Math.LN10})` }, { inp: "Math.log1p(3)", expected: "math.log(1 + 3)" },