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
6 changes: 5 additions & 1 deletion src/transformation/visitors/language-extensions/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export function isMultiReturnType(type: ts.Type): boolean {
}

export function canBeMultiReturnType(type: ts.Type): boolean {
return isMultiReturnType(type) || (type.isUnion() && type.types.some(t => canBeMultiReturnType(t)));
return (
(type.flags & ts.TypeFlags.Any) !== 0 ||
isMultiReturnType(type) ||
(type.isUnion() && type.types.some(t => canBeMultiReturnType(t)))
);
}

export function isMultiFunctionCall(context: TransformationContext, expression: ts.CallExpression): boolean {
Expand Down
22 changes: 15 additions & 7 deletions src/transformation/visitors/return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,37 @@ function transformExpressionsInReturn(
): lua.Expression[] {
const expressionType = context.checker.getTypeAtLocation(node);

if (ts.isCallExpression(node)) {
// skip type assertions
// don't skip parenthesis as it may arise confusion with lua behavior (where parenthesis are significant)
const innerNode = ts.skipOuterExpressions(node, ts.OuterExpressionKinds.Assertions);

if (ts.isCallExpression(innerNode)) {
// $multi(...)
if (isMultiFunctionCall(context, node)) {
if (isMultiFunctionCall(context, innerNode)) {
// Don't allow $multi to be implicitly cast to something other than LuaMultiReturn
const type = context.checker.getContextualType(node);
if (type && !canBeMultiReturnType(type)) {
context.diagnostics.push(invalidMultiFunctionReturnType(node));
context.diagnostics.push(invalidMultiFunctionReturnType(innerNode));
}

let returnValues = transformArguments(context, node.arguments);
let returnValues = transformArguments(context, innerNode.arguments);
if (insideTryCatch) {
returnValues = [wrapInTable(...returnValues)]; // Wrap results when returning inside try/catch
}
return returnValues;
}

// Force-wrap LuaMultiReturn when returning inside try/catch
if (insideTryCatch && returnsMultiType(context, node) && !shouldMultiReturnCallBeWrapped(context, node)) {
if (
insideTryCatch &&
returnsMultiType(context, innerNode) &&
!shouldMultiReturnCallBeWrapped(context, innerNode)
) {
return [wrapInTable(context.transformExpression(node))];
}
} else if (isInMultiReturnFunction(context, node) && isMultiReturnType(expressionType)) {
} else if (isInMultiReturnFunction(context, innerNode) && isMultiReturnType(expressionType)) {
// Unpack objects typed as LuaMultiReturn
return [createUnpackCall(context, context.transformExpression(node), node)];
return [createUnpackCall(context, context.transformExpression(innerNode), innerNode)];
}

return [context.transformExpression(node)];
Expand Down
30 changes: 30 additions & 0 deletions test/unit/language-extensions/multi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,36 @@ test("forward $multi call in ArrowFunction body", () => {
.expectToEqual([1, 2]);
});

test("$multi call in function typed as any", () => {
util.testFunction`
function foo(): any { return $multi(1, 2); }
return foo()
`
.withLanguageExtensions()
.expectToHaveNoDiagnostics()
.expectToEqual(1);
});

test("$multi call cast to any", () => {
util.testFunction`
function foo() { return $multi(1, 2) as any; }
return foo()
`
.withLanguageExtensions()
.expectToHaveNoDiagnostics()
.expectToEqual(1);
});

test("$multi call cast to MultiReturn type", () => {
util.testFunction`
function foo() { return $multi(1, 2) as unknown as LuaMultiReturn<number[]>; }
return foo()
`
.withLanguageExtensions()
.expectToHaveNoDiagnostics()
.expectToEqual(1);
});

test.each(["0", "i"])("allow LuaMultiReturn numeric access (%s)", expression => {
util.testFunction`
${multiFunction}
Expand Down