Skip to content

Commit 26d7338

Browse files
ark120202Perryvw
authored andcommitted
Wrap expression statements based on transformed node (#688)
1 parent 1741286 commit 26d7338

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

src/LuaTransformer.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,14 +2112,6 @@ export class LuaTransformer {
21122112
);
21132113
}
21142114

2115-
if (!ts.isCallLikeExpression(expression)) {
2116-
// Assign expression statements to dummy to make sure they're legal lua
2117-
return tstl.createVariableDeclarationStatement(
2118-
tstl.createAnonymousIdentifier(),
2119-
this.transformExpression(expression)
2120-
);
2121-
}
2122-
21232115
if (ts.isCallExpression(expression) && ts.isPropertyAccessExpression(expression.expression)) {
21242116
const ownerType = this.checker.getTypeAtLocation(expression.expression.expression);
21252117
const classDecorators = tsHelper.getCustomDecorators(ownerType, this.checker);
@@ -2134,7 +2126,11 @@ export class LuaTransformer {
21342126
}
21352127
}
21362128

2137-
return tstl.createExpressionStatement(this.transformExpression(expression));
2129+
const result = this.transformExpression(expression);
2130+
return tstl.isCallExpression(result) || tstl.isMethodCallExpression(result)
2131+
? tstl.createExpressionStatement(result)
2132+
: // Assign expression statements to dummy to make sure they're legal Lua
2133+
tstl.createVariableDeclarationStatement(tstl.createAnonymousIdentifier(), result);
21382134
}
21392135

21402136
public transformYieldExpression(expression: ts.YieldExpression): ExpressionVisitResult {
@@ -4210,10 +4206,7 @@ export class LuaTransformer {
42104206
const logCall1 = tstl.createCallExpression(log1, params);
42114207
const e = tstl.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2);
42124208
const div = tstl.createBinaryExpression(logCall1, e, tstl.SyntaxKind.DivisionOperator);
4213-
return ts.isExpressionStatement(node.parent)
4214-
? // if used as a stand-alone statement, needs to be a call expression to be valid lua
4215-
this.createImmediatelyInvokedFunctionExpression([], div, node)
4216-
: tstl.createParenthesizedExpression(div, node);
4209+
return tstl.createParenthesizedExpression(div, node);
42174210
}
42184211

42194212
// math.log(1 + x)

test/unit/expressions.spec.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,37 @@ test.each([
9797
});
9898

9999
test.each([
100-
{ input: "~a", lua: "local ____ = bit.bnot(a)" },
101-
{ input: "a&b", lua: "local ____ = bit.band(a, b)" },
100+
{ input: "~a", lua: "bit.bnot(a)" },
101+
{ input: "a&b", lua: "bit.band(a, b)" },
102102
{ input: "a&=b", lua: "a = bit.band(a, b)" },
103-
{ input: "a|b", lua: "local ____ = bit.bor(a, b)" },
103+
{ input: "a|b", lua: "bit.bor(a, b)" },
104104
{ input: "a|=b", lua: "a = bit.bor(a, b)" },
105-
{ input: "a^b", lua: "local ____ = bit.bxor(a, b)" },
105+
{ input: "a^b", lua: "bit.bxor(a, b)" },
106106
{ input: "a^=b", lua: "a = bit.bxor(a, b)" },
107-
{ input: "a<<b", lua: "local ____ = bit.lshift(a, b)" },
107+
{ input: "a<<b", lua: "bit.lshift(a, b)" },
108108
{ input: "a<<=b", lua: "a = bit.lshift(a, b)" },
109-
{ input: "a>>b", lua: "local ____ = bit.arshift(a, b)" },
109+
{ input: "a>>b", lua: "bit.arshift(a, b)" },
110110
{ input: "a>>=b", lua: "a = bit.arshift(a, b)" },
111-
{ input: "a>>>b", lua: "local ____ = bit.rshift(a, b)" },
111+
{ input: "a>>>b", lua: "bit.rshift(a, b)" },
112112
{ input: "a>>>=b", lua: "a = bit.rshift(a, b)" },
113113
])("Bitop [JIT] (%p)", ({ input, lua }) => {
114114
const options = { luaTarget: tstl.LuaTarget.LuaJIT, luaLibImport: tstl.LuaLibImportKind.None };
115115
expect(util.transpileString(input, options)).toBe(lua);
116116
});
117117

118118
test.each([
119-
{ input: "~a", lua: "local ____ = bit32.bnot(a)" },
120-
{ input: "a&b", lua: "local ____ = bit32.band(a, b)" },
119+
{ input: "~a", lua: "bit32.bnot(a)" },
120+
{ input: "a&b", lua: "bit32.band(a, b)" },
121121
{ input: "a&=b", lua: "a = bit32.band(a, b)" },
122-
{ input: "a|b", lua: "local ____ = bit32.bor(a, b)" },
122+
{ input: "a|b", lua: "bit32.bor(a, b)" },
123123
{ input: "a|=b", lua: "a = bit32.bor(a, b)" },
124-
{ input: "a^b", lua: "local ____ = bit32.bxor(a, b)" },
124+
{ input: "a^b", lua: "bit32.bxor(a, b)" },
125125
{ input: "a^=b", lua: "a = bit32.bxor(a, b)" },
126-
{ input: "a<<b", lua: "local ____ = bit32.lshift(a, b)" },
126+
{ input: "a<<b", lua: "bit32.lshift(a, b)" },
127127
{ input: "a<<=b", lua: "a = bit32.lshift(a, b)" },
128-
{ input: "a>>b", lua: "local ____ = bit32.arshift(a, b)" },
128+
{ input: "a>>b", lua: "bit32.arshift(a, b)" },
129129
{ input: "a>>=b", lua: "a = bit32.arshift(a, b)" },
130-
{ input: "a>>>b", lua: "local ____ = bit32.rshift(a, b)" },
130+
{ input: "a>>>b", lua: "bit32.rshift(a, b)" },
131131
{ input: "a>>>=b", lua: "a = bit32.rshift(a, b)" },
132132
])("Bitop [5.2] (%p)", ({ input, lua }) => {
133133
const options = { luaTarget: tstl.LuaTarget.Lua52, luaLibImport: tstl.LuaLibImportKind.None };
@@ -518,6 +518,7 @@ test.each([
518518
"foo as Function",
519519
"Math.log2(2)",
520520
"Math.log10(2)",
521+
'"".indexOf("")',
521522
])("Expression statements (%p)", input => {
522523
const code = `
523524
function foo() { return 17; }

test/unit/math.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ test.each([
55
{ inp: "Math.sin()", expected: "math.sin()" },
66
{ inp: "Math.min()", expected: "math.min()" },
77
{ inp: "Math.atan2(2, 3)", expected: "math.atan(2 / 3)" },
8-
{ inp: "Math.log2(3)", expected: `(function() return math.log(3) / ${Math.LN2} end)()` },
9-
{ inp: "Math.log10(3)", expected: `(function() return math.log(3) / ${Math.LN10} end)()` },
8+
{ inp: "Math.log2(3)", expected: `local ____ = (math.log(3) / ${Math.LN2})` },
9+
{ inp: "Math.log10(3)", expected: `local ____ = (math.log(3) / ${Math.LN10})` },
1010
{ inp: "const x = Math.log2(3)", expected: `local x = (math.log(3) / ${Math.LN2})` },
1111
{ inp: "const x = Math.log10(3)", expected: `local x = (math.log(3) / ${Math.LN10})` },
1212
{ inp: "Math.log1p(3)", expected: "math.log(1 + 3)" },

0 commit comments

Comments
 (0)