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
20 changes: 20 additions & 0 deletions src/transformation/utils/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ function isExplicitArrayType(context: TransformationContext, type: ts.Type): boo
return false;
}

function isAlwaysExplicitArrayType(context: TransformationContext, type: ts.Type): boolean {
if (context.checker.isArrayType(type) || context.checker.isTupleType(type)) return true;
if (type.symbol) {
const baseConstraint = context.checker.getBaseConstraintOfType(type);
if (baseConstraint && baseConstraint !== type) {
return isAlwaysExplicitArrayType(context, baseConstraint);
}
}

if (type.isUnionOrIntersection()) {
return type.types.every(t => isAlwaysExplicitArrayType(context, t));
}

return false;
}

/**
* Iterate over a type and its bases until the callback returns true.
*/
Expand All @@ -95,6 +111,10 @@ export function isArrayType(context: TransformationContext, type: ts.Type): bool
return forTypeOrAnySupertype(context, type, t => isExplicitArrayType(context, t));
}

export function isAlwaysArrayType(context: TransformationContext, type: ts.Type): boolean {
return forTypeOrAnySupertype(context, type, t => isAlwaysExplicitArrayType(context, t));
}

export function isFunctionType(type: ts.Type): boolean {
return type.getCallSignatures().length > 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/transformation/visitors/spread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
isFunctionScopeWithDefinition,
ScopeType,
} from "../utils/scope";
import { isArrayType, findFirstNonOuterParent } from "../utils/typescript";
import { findFirstNonOuterParent, isAlwaysArrayType } from "../utils/typescript";
import { isMultiReturnCall } from "./language-extensions/multi";
import { isGlobalVarargConstant } from "./language-extensions/vararg";

Expand Down Expand Up @@ -96,7 +96,8 @@ export const transformSpreadElement: FunctionVisitor<ts.SpreadElement> = (node,
}

const type = context.checker.getTypeAtLocation(node.expression); // not ts-inner expression, in case of casts
if (isArrayType(context, type)) {
if (isAlwaysArrayType(context, type)) {
// All union members must be arrays to be able to shortcut to unpack call
return createUnpackCall(context, innerExpression, node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ f(_G, ____)"
exports[`$vararg invalid use ("function f(s: string[]) {} f($vararg);"): diagnostics 1`] = `"main.ts(2,38): error TSTL: $vararg can only be used in a spread element ('...$vararg') in global scope."`;

exports[`$vararg invalid use ("function foo(...args: string[]) {} function bar() { foo(...$vararg); }"): code 1`] = `
"function foo(self, ...)
"local ____lualib = require("lualib_bundle")
local __TS__Spread = ____lualib.__TS__Spread
function foo(self, ...)
end
function bar(self)
foo(
_G,
table.unpack(____)
__TS__Spread(____)
)
end"
`;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/spread.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,15 @@ test("can spread LuaSet (#1384)", () => {
expect(result).toContain("foo");
expect(result).toContain("bar");
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1426
test.each([true, false])("spread a decorator or array union type (#1426)", choice => {
util.testFunction`
function* things() {
yield "a";
yield "b"
}
const c: boolean = ${util.formatCode(choice)};
return [...(c ? things() : ["c", "d"])];
`.expectToMatchJsResult();
});