Skip to content

Commit a6e5486

Browse files
committed
skip the error in binding elements that refer to the same destructuring
1 parent f279dd5 commit a6e5486

11 files changed

Lines changed: 125 additions & 6 deletions

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26117,7 +26117,7 @@ namespace ts {
2611726117
return convertAutoToAny(flowType);
2611826118
}
2611926119
}
26120-
else if (!assumeInitialized && !containsUndefinedType(type) && containsUndefinedType(flowType)) {
26120+
else if (!assumeInitialized && !containsUndefinedType(type) && containsUndefinedType(flowType) && !(isBindingElement(declaration) && findAncestor(node, isBindingElement))) {
2612126121
error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol));
2612226122
// Return the declared type to reduce follow-on errors
2612326123
return type;

tests/baselines/reference/controlFlowDestructuringVariablesInTryCatch.errors.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(13,1): error TS2454: Variable 'a' is used before being assigned.
2-
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(14,1): error TS2454: Variable 'b' is used before being assigned.
3-
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(15,1): error TS2454: Variable 'c' is used before being assigned.
1+
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(16,1): error TS2454: Variable 'a' is used before being assigned.
2+
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(17,1): error TS2454: Variable 'b' is used before being assigned.
3+
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(18,1): error TS2454: Variable 'c' is used before being assigned.
4+
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(19,1): error TS2454: Variable 'd' is used before being assigned.
5+
tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(20,1): error TS2454: Variable 'e' is used before being assigned.
46

57

6-
==== tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts (3 errors) ====
8+
==== tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts (5 errors) ====
79
declare function f1(): string;
810
declare function f2(): [b: string];
911
declare function f3(): { c: string };
@@ -12,6 +14,9 @@ tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(15,1): error
1214
var a = f1();
1315
var [b] = f2();
1416
var { c } = f3();
17+
18+
var [d = 1] = [];
19+
var { e = 1 } = { };
1520
} catch {
1621
console.error("error");
1722
}
@@ -25,4 +30,10 @@ tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts(15,1): error
2530
c;
2631
~
2732
!!! error TS2454: Variable 'c' is used before being assigned.
33+
d;
34+
~
35+
!!! error TS2454: Variable 'd' is used before being assigned.
36+
e;
37+
~
38+
!!! error TS2454: Variable 'e' is used before being assigned.
2839

tests/baselines/reference/controlFlowDestructuringVariablesInTryCatch.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ try {
77
var a = f1();
88
var [b] = f2();
99
var { c } = f3();
10+
11+
var [d = 1] = [];
12+
var { e = 1 } = { };
1013
} catch {
1114
console.error("error");
1215
}
1316

1417
a;
1518
b;
1619
c;
20+
d;
21+
e;
1722

1823

1924
//// [controlFlowDestructuringVariablesInTryCatch.js]
@@ -22,10 +27,14 @@ try {
2227
var a = f1();
2328
var b = f2()[0];
2429
var c = f3().c;
30+
var _a = [][0], d = _a === void 0 ? 1 : _a;
31+
var _b = {}.e, e = _b === void 0 ? 1 : _b;
2532
}
26-
catch (_a) {
33+
catch (_c) {
2734
console.error("error");
2835
}
2936
a;
3037
b;
3138
c;
39+
d;
40+
e;

tests/baselines/reference/controlFlowDestructuringVariablesInTryCatch.symbols

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ try {
2222
>c : Symbol(c, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 7, 9))
2323
>f3 : Symbol(f3, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 1, 35))
2424

25+
var [d = 1] = [];
26+
>d : Symbol(d, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 9, 9))
27+
28+
var { e = 1 } = { };
29+
>e : Symbol(e, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 10, 9))
30+
2531
} catch {
2632
console.error("error");
2733
>console.error : Symbol(Console.error, Decl(lib.dom.d.ts, --, --))
@@ -38,3 +44,9 @@ b;
3844
c;
3945
>c : Symbol(c, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 7, 9))
4046

47+
d;
48+
>d : Symbol(d, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 9, 9))
49+
50+
e;
51+
>e : Symbol(e, Decl(controlFlowDestructuringVariablesInTryCatch.ts, 10, 9))
52+

tests/baselines/reference/controlFlowDestructuringVariablesInTryCatch.types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ try {
2525
>f3() : { c: string; }
2626
>f3 : () => { c: string; }
2727

28+
var [d = 1] = [];
29+
>d : number
30+
>1 : 1
31+
>[] : []
32+
33+
var { e = 1 } = { };
34+
>e : number
35+
>1 : 1
36+
>{ } : { e?: number | undefined; }
37+
2838
} catch {
2939
console.error("error");
3040
>console.error("error") : void
@@ -43,3 +53,9 @@ b;
4353
c;
4454
>c : string
4555

56+
d;
57+
>d : number
58+
59+
e;
60+
>e : number
61+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/controlFlowInitializedDestructuringVariables.ts(4,10): error TS2532: Object is possibly 'undefined'.
2+
3+
4+
==== tests/cases/compiler/controlFlowInitializedDestructuringVariables.ts (1 errors) ====
5+
declare const obj: { a?: string, b?: number };
6+
const {
7+
a = "0",
8+
b = +a,
9+
~
10+
!!! error TS2532: Object is possibly 'undefined'.
11+
} = obj;
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [controlFlowInitializedDestructuringVariables.ts]
2+
declare const obj: { a?: string, b?: number };
3+
const {
4+
a = "0",
5+
b = +a,
6+
} = obj;
7+
8+
9+
//// [controlFlowInitializedDestructuringVariables.js]
10+
"use strict";
11+
var _a = obj.a, a = _a === void 0 ? "0" : _a, _b = obj.b, b = _b === void 0 ? +a : _b;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/controlFlowInitializedDestructuringVariables.ts ===
2+
declare const obj: { a?: string, b?: number };
3+
>obj : Symbol(obj, Decl(controlFlowInitializedDestructuringVariables.ts, 0, 13))
4+
>a : Symbol(a, Decl(controlFlowInitializedDestructuringVariables.ts, 0, 20))
5+
>b : Symbol(b, Decl(controlFlowInitializedDestructuringVariables.ts, 0, 32))
6+
7+
const {
8+
a = "0",
9+
>a : Symbol(a, Decl(controlFlowInitializedDestructuringVariables.ts, 1, 7))
10+
11+
b = +a,
12+
>b : Symbol(b, Decl(controlFlowInitializedDestructuringVariables.ts, 2, 12))
13+
>a : Symbol(a, Decl(controlFlowInitializedDestructuringVariables.ts, 1, 7))
14+
15+
} = obj;
16+
>obj : Symbol(obj, Decl(controlFlowInitializedDestructuringVariables.ts, 0, 13))
17+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/controlFlowInitializedDestructuringVariables.ts ===
2+
declare const obj: { a?: string, b?: number };
3+
>obj : { a?: string | undefined; b?: number | undefined; }
4+
>a : string | undefined
5+
>b : number | undefined
6+
7+
const {
8+
a = "0",
9+
>a : string
10+
>"0" : "0"
11+
12+
b = +a,
13+
>b : number
14+
>+a : number
15+
>a : string | undefined
16+
17+
} = obj;
18+
>obj : { a?: string | undefined; b?: number | undefined; }
19+

tests/cases/compiler/controlFlowDestructuringVariablesInTryCatch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ try {
88
var a = f1();
99
var [b] = f2();
1010
var { c } = f3();
11+
12+
var [d = 1] = [];
13+
var { e = 1 } = { };
1114
} catch {
1215
console.error("error");
1316
}
1417

1518
a;
1619
b;
1720
c;
21+
d;
22+
e;

0 commit comments

Comments
 (0)