Skip to content

Fixes to return for TupleReturn functions#232

Merged
Perryvw merged 2 commits intoTypeScriptToLua:masterfrom
tomblind:tuplereturn_forward_fix
Oct 13, 2018
Merged

Fixes to return for TupleReturn functions#232
Perryvw merged 2 commits intoTypeScriptToLua:masterfrom
tomblind:tuplereturn_forward_fix

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

Currently, TupleReturn functions will only correctly unpack values if returning an array literal. Returning a variable containing a tuple or forwarding a tuple from another TupleReturn call don't work:

/** !TupleReturn */
function foo(): [number, string]
{
    const r: [number, string] = [0, "foo"];
    return r;
}
/** !TupleReturn */
function bar(): [number, string]
{
    return foo();
}
function foo()
    local r = {0,"foo"};
    return r --Should be unpacked
end
function bar()
    return ({ foo() }) --Should not be wrapped in table
end

This PR adds handling for both of these cases.

src/TSHelper.ts Outdated

public static isInTupleReturnFunction(node: ts.Node, checker: ts.TypeChecker): boolean {
const declaration = this.findFirstNodeAbove(node, (n): n is ts.Node =>
ts.isFunctionDeclaration(n) || ts.isMethodDeclaration(n));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange formatting

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops - copy/paste bug

@Perryvw Perryvw merged commit 8e9dfe8 into TypeScriptToLua:master Oct 13, 2018
@tomblind tomblind deleted the tuplereturn_forward_fix branch October 13, 2018 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants