8000 Fix exception when unwrapping parenthesised multireturn calls (#1408) · TypeScriptToLua/TypeScriptToLua@26d7c03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 26d7c03

Browse files
Perryvwhazzard993
andauthored
Fix exception when unwrapping parenthesised multireturn calls (#1408)
* Fix exception when unwrapping parenthesised multireturn calls * reorder * Update test/unit/language-extensions/multi.spec.ts Co-authored-by: hazzard993 <hazzard993@gmail.com> --------- Co-authored-by: hazzard993 <hazzard993@gmail.com>
1 parent 5a758ea commit 26d7c03

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/transformation/visitors/language-extensions/multi.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
IterableExtensionKind,
77
} from "../../utils/language-extensions";
88
import { TransformationContext } from "../../context";
9-
import { findFirstNodeAbove } from "../../utils/typescript";
9+
import { findFirstNodeAbove, findFirstNonOuterParent } from "../../utils/typescript";
1010

1111
const multiReturnExtensionName = "__tstlMultiReturn";
1212
export function isMultiReturnType(type: ts.Type): boolean {
@@ -58,46 +58,48 @@ export function shouldMultiReturnCallBeWrapped(context: TransformationContext, n
5858
return false;
5959
}
6060

61+
const parent = findFirstNonOuterParent(node);
62+
6163
// Variable declaration with destructuring
62-
if (ts.isVariableDeclaration(node.parent) && ts.isArrayBindingPattern(node.parent.name)) {
64+
if (ts.isVariableDeclaration(parent) && ts.isArrayBindingPattern(parent.name)) {
6365
return false;
6466
}
6567

6668
// Variable assignment with destructuring
6769
if (
68-
ts.isBinaryExpression(node.parent) &&
69-
node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
70-
ts.isArrayLiteralExpression(node.parent.left)
70+
ts.isBinaryExpression(parent) &&
71+
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
72+
ts.isArrayLiteralExpression(parent.left)
7173
) {
7274
return false;
7375
}
7476

7577
// Spread operator
76-
if (ts.isSpreadElement(node.parent)) {
78+
if (ts.isSpreadElement(parent)) {
7779
return false;
7880
}
7981

8082
// Stand-alone expression
81-
if (ts.isExpressionStatement(node.parent)) {
83+
if (ts.isExpressionStatement(parent)) {
8284
return false;
8385
}
8486

8587
// Forwarded multi-return call
8688
if (
87-
(ts.isReturnStatement(node.parent) || ts.isArrowFunction(node.parent)) && // Body-less arrow func
89+
(ts.isReturnStatement(parent) || ts.isArrowFunction(parent)) && // Body-less arrow func
8890
isInMultiReturnFunction(context, node)
8991
) {
9092
return false;
9193
}
9294

9395
// Element access expression 'foo()[0]' will be optimized using 'select'
94-
if (ts.isElementAccessExpression(node.parent)) {
96+
if (ts.isElementAccessExpression(parent)) {
9597
return false;
9698
}
9799

98100
// LuaIterable in for...of
99101
if (
100-
ts.isForOfStatement(node.parent) &&
102+
ts.isForOfStatement(parent) &&
101103
getIterableExtensionKindForNode(context, node) === IterableExtensionKind.Iterable
102104
) {
103105
return false;

test/unit/language-extensions/multi.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,14 @@ test("return LuaMultiReturn from catch", () => {
327327
.withLanguageExtensions()
328328
.expectToEqual(2);
329329
});
330+
331+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1404
332+
test("LuaMultiReturn applies after casting a function (#1404)", () => {
333+
util.testFunction`
334+
let swap: any = (a: number, b: number) => $multi(b, a);
335+
let [a, b] = (swap as (...args: any) => LuaMultiReturn<[number, number]>)(4, 3);
336+
return [a, b];
337+
`
338+
.withLanguageExtensions()
339+
.expectToEqual([3, 4]);
340+
});

0 commit comments

Comments
 (0)
0