X Tutup
Skip to content

Commit 3b30b09

Browse files
authored
Spread LuaPairsIterable and LuaPairsKeysIterable (#1409)
* Spread LuaPairsIterable and LuaPairsKeysIterable * Use expect.toContainEqual in tests
1 parent 26d7c03 commit 3b30b09

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/transformation/visitors/spread.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as ts from "typescript";
22
import { LuaTarget } from "../../CompilerOptions";
33
import * as lua from "../../LuaAST";
4+
import { assertNever } from "../../utils";
45
import { FunctionVisitor, TransformationContext } from "../context";
5-
import { isLuaIterable } from "../utils/language-extensions";
6+
import { getIterableExtensionKindForNode, IterableExtensionKind } from "../utils/language-extensions";
67
import { createUnpackCall } from "../utils/lua-ast";
78
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
89
import {
@@ -76,11 +77,22 @@ export const transformSpreadElement: FunctionVisitor<ts.SpreadElement> = (node,
7677
const innerExpression = context.transformExpression(node.expression);
7778
if (isMultiReturnCall(context, tsInnerExpression)) return innerExpression;
7879

79-
const type = context.checker.getTypeAtLocation(node.expression); // not ts-inner expression, in case of casts
80-
if (isLuaIterable(context, type)) {
81-
return transformLuaLibFunction(context, LuaLibFeature.LuaIteratorSpread, node, innerExpression);
80+
const iterableExtensionType = getIterableExtensionKindForNode(context, node.expression);
81+
if (iterableExtensionType) {
82+
if (iterableExtensionType === IterableExtensionKind.Iterable) {
83+
return transformLuaLibFunction(context, LuaLibFeature.LuaIteratorSpread, node, innerExpression);
84+
} else if (iterableExtensionType === IterableExtensionKind.Pairs) {
85+
const objectEntries = transformLuaLibFunction(context, LuaLibFeature.ObjectEntries, node, innerExpression);
86+
return createUnpackCall(context, objectEntries, node);
87+
} else if (iterableExtensionType === IterableExtensionKind.PairsKey) {
88+
const objectKeys = transformLuaLibFunction(context, LuaLibFeature.ObjectKeys, node, innerExpression);
89+
return createUnpackCall(context, objectKeys, node);
90+
} else {
91+
assertNever(iterableExtensionType);
92+
}
8293
}
8394

95+
const type = context.checker.getTypeAtLocation(node.expression); // not ts-inner expression, in case of casts
8496
if (isArrayType(context, type)) {
8597
return createUnpackCall(context, innerExpression, node);
8698
}

test/unit/spread.spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ test.each(["pairs", "ipairs"])("can spread %s (#1244)", func => {
509509
});
510510

511511
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1244
512-
test.each(["LuaTable", "LuaMap"])("can spread %s (#1244)", type => {
512+
test.each(["LuaTable", "LuaMap"])("can spread %s with pairs (#1244)", type => {
513513
const result: Array<[string, string]> = util.testFunction`
514514
const tbl = new ${type}();
515515
tbl.set("foo", "bar");
@@ -524,6 +524,40 @@ test.each(["LuaTable", "LuaMap"])("can spread %s (#1244)", type => {
524524

525525
// We don't know the order so match like this
526526
expect(result).toHaveLength(2);
527-
expect(result.some(([k, v]) => k === "foo" && v === "bar")).toBe(true);
528-
expect(result.some(([k, v]) => k === "fizz" && v === "buzz")).toBe(true);
527+
expect(result).toContainEqual(["foo", "bar"]);
528+
expect(result).toContainEqual(["fizz", "buzz"]);
529+
});
530+
531+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1384
532+
test.each(["LuaTable", "LuaMap"])("can spread %s (#1384)", type => {
533+
const result: Array<[string, string]> = util.testFunction`
534+
const tbl = new ${type}();
535+
tbl.set("foo", "bar");
536+
tbl.set("fizz", "buzz");
537+
return [...tbl];
538+
`
539+
.withLanguageExtensions()
540+
.getLuaExecutionResult();
541+
542+
// We don't know the order so match like this
543+
expect(result).toHaveLength(2);
544+
expect(result).toContainEqual(["foo", "bar"]);
545+
expect(result).toContainEqual(["fizz", "buzz"]);
546+
});
547+
548+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1384
549+
test("can spread LuaSet (#1384)", () => {
550+
const result: Array<[string, string]> = util.testFunction`
551+
const tbl = new LuaSet();
552+
tbl.add("foo");
553+
tbl.add("bar");
554+
return [...tbl];
555+
`
556+
.withLanguageExtensions()
557+
.getLuaExecutionResult();
558+
559+
// We don't know the order so match like this
560+
expect(result).toHaveLength(2);
561+
expect(result).toContain("foo");
562+
expect(result).toContain("bar");
529563
});

0 commit comments

Comments
 (0)
X Tutup