Skip to content

Commit 6e532a3

Browse files
committed
Merge branch 'master' into ts-lualib
2 parents b40178a + 1dc962e commit 6e532a3

File tree

3 files changed

+220
-182
lines changed

3 files changed

+220
-182
lines changed

src/Transpiler.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,18 @@ export abstract class LuaTranspiler {
637637
return "return " + node.expression.elements.map(elem => this.transpileExpression(elem)).join(",");
638638
}
639639

640-
// Otherwise just do a normal return
640+
// If the expression is an assignment transpile as 2 lines, one containing expression, one returning
641+
// the left-hand side of the expression.
642+
if (ts.isBinaryExpression(node.expression)
643+
&& (tsHelper.isBinaryAssignmentToken(node.expression.operatorToken.kind)[0]
644+
|| node.expression.operatorToken.kind === ts.SyntaxKind.EqualsToken)) {
645+
return this.transpileExpression(node.expression) + "\n"
646+
+ this.indent + " return " + this.transpileExpression(node.expression.left);
647+
}
648+
641649
return "return " + this.transpileExpression(node.expression);
642650
} else {
651+
// Empty return
643652
return "return";
644653
}
645654
}
@@ -1599,7 +1608,9 @@ export abstract class LuaTranspiler {
15991608
this.popIndent();
16001609
return result + this.indent + "end\n";
16011610
} else {
1602-
return `function(${paramNames.join(",")}) return ` + this.transpileExpression(node.body) + " end";
1611+
// Transpile as return value
1612+
const returnVal = this.transpileReturn(ts.createReturn(node.body));
1613+
return `function(${paramNames.join(",")}) ${returnVal} end`;
16031614
}
16041615
}
16051616

test/unit/expressions.spec.ts

Lines changed: 0 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -188,53 +188,6 @@ export class ExpressionTests {
188188
Expect(util.transpileString(input)).toBe(lua);
189189
}
190190

191-
@Test("Arrow Function Expression")
192-
public arrowFunctionExpression() {
193-
// Transpile
194-
const lua = util.transpileString(`let add = (a, b) => a+b; return add(1,2);`);
195-
196-
// Execute
197-
const result = util.executeLua(lua);
198-
199-
// Assert
200-
Expect(result).toBe(3);
201-
}
202-
203-
@TestCase([])
204-
@TestCase([5])
205-
@TestCase([1, 2])
206-
@Test("Arrow Default Values")
207-
public arrowFunctionDefaultValues(inp: number[]) {
208-
// Default value is 3 for v1
209-
const v1 = inp.length > 0 ? inp[0] : 3;
210-
// Default value is 4 for v2
211-
const v2 = inp.length > 1 ? inp[1] : 4;
212-
213-
const callArgs = inp.join(",");
214-
215-
// Transpile
216-
const lua = util.transpileString(`let add = (a: number = 3, b: number = 4) => { return a+b; }`
217-
+ `return add(${callArgs});`);
218-
219-
// Execute
220-
const result = util.executeLua(lua);
221-
222-
// Assert
223-
Expect(result).toBe(v1 + v2);
224-
}
225-
226-
@Test("Function Expression")
227-
public functionExpression() {
228-
// Transpile
229-
const lua = util.transpileString(`let add = function(a, b) {return a+b}; return add(1,2);`);
230-
231-
// Execute
232-
const result = util.executeLua(lua);
233-
234-
// Assert
235-
Expect(result).toBe(3);
236-
}
237-
238191
@Test("Null Expression")
239192
public nullExpression() {
240193
Expect(util.transpileString("null")).toBe("nil");
@@ -245,29 +198,6 @@ export class ExpressionTests {
245198
Expect(util.transpileString("undefined")).toBe("nil");
246199
}
247200

248-
@TestCase([], 7)
249-
@TestCase([5], 9)
250-
@TestCase([1, 2], 3)
251-
@Test("Arrow Default Values")
252-
public functionExpressionDefaultValues(inp: number[]) {
253-
// Default value is 3 for v1
254-
const v1 = inp.length > 0 ? inp[0] : 3;
255-
// Default value is 4 for v2
256-
const v2 = inp.length > 1 ? inp[1] : 4;
257-
258-
const callArgs = inp.join(",");
259-
260-
// Transpile
261-
const lua = util.transpileString(`let add = function(a: number = 3, b: number = 4) { return a+b; }`
262-
+ `return add(${callArgs});`);
263-
264-
// Execute
265-
const result = util.executeLua(lua);
266-
267-
// Assert
268-
Expect(result).toBe(v1 + v2);
269-
}
270-
271201
@TestCase("inst.field", 8)
272202
@TestCase("inst.field + 3", 8 + 3)
273203
@TestCase("inst.field * 3", 8 * 3)
@@ -330,104 +260,6 @@ export class ExpressionTests {
330260
Expect(result).toBe(expected);
331261
}
332262

333-
@Test("Class method call")
334-
public classMethod() {
335-
const returnValue = 4;
336-
const source = `class TestClass {
337-
public classMethod(): number { return ${returnValue}; }
338-
}
339-
340-
const classInstance = new TestClass();
341-
return classInstance.classMethod();`;
342-
343-
// Transpile
344-
const lua = util.transpileString(source);
345-
346-
// Execute
347-
const result = util.executeLua(lua);
348-
349-
// Assert
350-
Expect(result).toBe(returnValue);
351-
}
352-
353-
@Test("Class dot method call void")
354-
public classDotMethod() {
355-
const returnValue = 4;
356-
const source = `class TestClass {
357-
public dotMethod: () => number = () => ${returnValue};
358-
}
359-
360-
const classInstance = new TestClass();
361-
return classInstance.dotMethod();`;
362-
363-
// Transpile
364-
const lua = util.transpileString(source);
365-
366-
// Execute
367-
const result = util.executeLua(lua);
368-
369-
// Assert
370-
Expect(result).toBe(returnValue);
371-
}
372-
373-
@Test("Class dot method call with parameter")
374-
public classDotMethod2() {
375-
const returnValue = 4;
376-
const source = `class TestClass {
377-
public dotMethod: (x: number) => number = x => 3 * x;
378-
}
379-
380-
const classInstance = new TestClass();
381-
return classInstance.dotMethod(${returnValue});`;
382-
383-
// Transpile
384-
const lua = util.transpileString(source);
385-
386-
// Execute
387-
const result = util.executeLua(lua);
388-
389-
// Assert
390-
Expect(result).toBe(3 * returnValue);
391-
}
392-
393-
@Test("Class static dot method")
394-
public classDotMethodStatic() {
395-
const returnValue = 4;
396-
const source = `class TestClass {
397-
public static dotMethod: () => number = () => ${returnValue};
398-
}
399-
400-
return TestClass.dotMethod();`;
401-
402-
// Transpile
403-
const lua = util.transpileString(source);
404-
405-
// Execute
406-
const result = util.executeLua(lua);
407-
408-
// Assert
409-
Expect(result).toBe(returnValue);
410-
}
411-
412-
@Test("Class static dot method with parameter")
413-
public classDotMethodStaticWithParameter() {
414-
const returnValue = 4;
415-
const source = `class TestClass {
416-
public static dotMethod: (x: number) => number = x => 3 * x;
417-
}
418-
419-
return TestClass.dotMethod(${returnValue});`;
420-
421-
// Transpile
422-
const lua = util.transpileString(source);
423-
424-
// Execute
425-
const result = util.executeLua(lua);
426-
427-
// Assert
428-
Expect(result).toBe(3 * returnValue);
429-
}
430-
431263
// ====================================
432264
// Test expected errors
433265
// ====================================
@@ -534,16 +366,4 @@ export class ExpressionTests {
534366
Expect(() => transpiler.transpileObjectLiteral(mockObject as ts.ObjectLiteralExpression))
535367
.toThrowError(Error, "Encountered unsupported object literal element: FalseKeyword.");
536368
}
537-
538-
@Test("Invalid property access call transpilation")
539-
public invalidPropertyCall() {
540-
const transpiler = util.makeTestTranspiler();
541-
542-
const mockObject: any = {
543-
expression: ts.createLiteral("abc"),
544-
};
545-
546-
Expect(() => transpiler.transpilePropertyCall(mockObject as ts.CallExpression))
547-
.toThrowError(Error, "Tried to transpile a non-property call as property call.");
548-
}
549369
}

0 commit comments

Comments
 (0)