Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 15 additions & 14 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,37 @@ 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.lshift(a, b)" },
{ input: "a<<b", lua: "bit.lshift(a, b)" },
{ input: "a<<=b", lua: "a = bit.lshift(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 };
expect(util.transpileString(input, options)).toBe(lua);
});

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.lshift(a, b)" },
{ input: "a<<b", lua: "bit32.lshift(a, b)" },
{ input: "a<<=b", lua: "a = bit32.lshift(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)" },
Comment thread
Perryvw marked this conversation as resolved.
{ 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 };
Expand Down Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions test/unit/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)" },
Expand Down