Skip to content

Commit 9bc2990

Browse files
authored
fix: splice() return type now correctly tracked as array (#397)
Co-authored-by: cs01 <cs01@users.noreply.github.com>
1 parent aa2939e commit 9bc2990

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/codegen/infrastructure/type-inference.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ export class TypeInference {
622622
method: string,
623623
objBase: ExprBase,
624624
): ResolvedType | null {
625-
if (method === "slice" || method === "concat") {
625+
if (method === "slice" || method === "splice" || method === "concat") {
626626
const objResolved = this.resolveExpressionType(expr.object);
627627
if (objResolved) return objResolved;
628628
}
@@ -1329,7 +1329,11 @@ export class TypeInference {
13291329
return true;
13301330
}
13311331
}
1332-
if (methodExpr.method === "slice" || methodExpr.method === "concat") {
1332+
if (
1333+
methodExpr.method === "slice" ||
1334+
methodExpr.method === "splice" ||
1335+
methodExpr.method === "concat"
1336+
) {
13331337
const objBase = methodExpr.object as ExprBase;
13341338
if (objBase.type === "array") {
13351339
return true;
@@ -1532,6 +1536,7 @@ export class TypeInference {
15321536
}
15331537
if (
15341538
methodExpr.method === "slice" ||
1539+
methodExpr.method === "splice" ||
15351540
methodExpr.method === "concat" ||
15361541
methodExpr.method === "filter"
15371542
) {
@@ -2447,6 +2452,7 @@ export class TypeInference {
24472452
if (
24482453
methodExpr.method === "map" ||
24492454
methodExpr.method === "filter" ||
2455+
methodExpr.method === "splice" ||
24502456
methodExpr.method === "slice" ||
24512457
methodExpr.method === "concat"
24522458
) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const nums = [1, 2, 3, 4, 5];
2+
const removedNums = nums.splice(1, 2);
3+
4+
const strs = ["a", "b", "c", "d"];
5+
const removedStrs = strs.splice(1, 2);
6+
7+
if (
8+
removedNums.length === 2 &&
9+
removedNums[0] === 2 &&
10+
removedNums[1] === 3 &&
11+
nums.length === 3 &&
12+
removedStrs.length === 2 &&
13+
removedStrs[0] === "b" &&
14+
removedStrs[1] === "c" &&
15+
strs.length === 2
16+
) {
17+
console.log("TEST_PASSED");
18+
} else {
19+
console.log("FAIL");
20+
}

0 commit comments

Comments
 (0)