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
10 changes: 6 additions & 4 deletions src/transformation/visitors/variable-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isTupleReturnCall } from "../utils/annotations";
import { validateAssignment } from "../utils/assignment-validation";
import { unsupportedVarDeclaration } from "../utils/diagnostics";
import { addExportToIdentifier } from "../utils/export";
import { createLocalOrExportedOrGlobalDeclaration, createUnpackCall } from "../utils/lua-ast";
import { createLocalOrExportedOrGlobalDeclaration, createUnpackCall, wrapInTable } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { transformIdentifier } from "./identifier";
import { isMultiReturnCall } from "./language-extensions/multi";
Expand Down Expand Up @@ -158,9 +158,11 @@ export function transformBindingVariableDeclaration(
// Contain the expression in a temporary variable
table = lua.createAnonymousIdentifier();
if (initializer) {
statements.push(
lua.createVariableDeclarationStatement(table, context.transformExpression(initializer))
);
let expression = context.transformExpression(initializer);
if (isTupleReturnCall(context, initializer) || isMultiReturnCall(context, initializer)) {
expression = wrapInTable(expression);
}
statements.push(lua.createVariableDeclarationStatement(table, expression));
}
}
statements.push(...transformBindingPattern(context, bindingPattern, table));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ exports[`invalid $multi call (const [a = 0] = $multi()): diagnostics 1`] = `"mai

exports[`invalid $multi call (const {} = $multi();): code 1`] = `
"local ____ = {
____(_G)
{
____(_G)
}
}"
`;

Expand Down
27 changes: 27 additions & 0 deletions test/unit/language-extensions/multi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ test("multi example use case", () => {
.expectToEqual({ a: "foo", b: 5 });
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/995
test("Destructuring assignment of LuaMultiReturn", () => {
util.testModule`
function multiReturn(): LuaMultiReturn<[number, number, number]> {
return $multi(1, 2, 3);
Comment thread
yancouto marked this conversation as resolved.
}

const [a, ...b] = multiReturn();
export {a, b};
`
.setOptions(multiProjectOptions)
.expectToEqual({ a: 1, b: [2, 3] });
});

test("Destructuring assignment of LuaMultiReturn returning nil", () => {
util.testModule`

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.

Tests should only test one thing, could you split this second testModule into a second test("Destructuring assignment of LuaMultiReturn returning nil", () => {

function multiReturn(): LuaMultiReturn<[number, number, number]> {
return;
}

const [a, ...b] = multiReturn();
export {a, b};
`
.setOptions(multiProjectOptions)
.expectToEqual({ a: undefined, b: [] });
});

test.each<[string, any]>([
["$multi()", undefined],
["$multi(true)", true],
Expand Down