Skip to content

Commit a07fed5

Browse files
authored
Fix bug when using Array.from with non-array iterable (#1577)
1 parent a2d31c9 commit a07fed5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/lualib/ArrayFrom.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ export function __TS__ArrayFrom(
2828
result.push(v);
2929
}
3030
} else {
31-
for (const [i, v] of arrayLikeIterator(arrayLike)) {
32-
result.push(mapFn.call(thisArg, v, i - 1));
31+
let i = 0;
32+
for (const [, v] of arrayLikeIterator(arrayLike)) {
33+
result.push(mapFn.call(thisArg, v, i++));
3334
}
3435
}
3536
return result;

test/unit/builtins/array.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,14 @@ test.each([
753753
util.testExpression`Array.from(${valueString})`.expectToMatchJsResult();
754754
});
755755

756+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1576
757+
test("array.from with non-array iterable (#1576)", () => {
758+
util.testFunction`
759+
const map = new Map().set(1, 2);
760+
return Array.from(map, ([v,k]) => ({k,v}));
761+
`.expectToMatchJsResult();
762+
});
763+
756764
// Array.of
757765
test.each(["1, 2, 3", "", "...[1, 2, 3], 4, 5, 6"])("Array.of(%p)", valueString => {
758766
util.testExpression`Array.of(${valueString})`.expectToMatchJsResult();

0 commit comments

Comments
 (0)