Skip to content

Commit 491ce13

Browse files
committed
feat(31388): allow variables starting with an underscore in array destructuring
1 parent 9871b5f commit 491ce13

16 files changed

+543
-112
lines changed

src/compiler/checker.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -33363,10 +33363,7 @@ namespace ts {
3336333363

3336433364
function isValidUnusedLocalDeclaration(declaration: Declaration): boolean {
3336533365
if (isBindingElement(declaration) && isIdentifierThatStartsWithUnderscore(declaration.name)) {
33366-
return !!findAncestor(declaration.parent, ancestor =>
33367-
isArrayBindingPattern(ancestor) || isVariableDeclaration(ancestor) || isVariableDeclarationList(ancestor) ? false :
33368-
isForOfStatement(ancestor) ? true : "quit"
33369-
);
33366+
return isArrayBindingPattern(declaration.parent);
3337033367
}
3337133368

3337233369
return isAmbientModule(declaration) ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(12,17): error TS6133: 'b1' is declared but its value is never read.
2+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(13,12): error TS6133: 'a2' is declared but its value is never read.
3+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(14,12): error TS6133: 'a3' is declared but its value is never read.
4+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(14,16): error TS6133: 'b3' is declared but its value is never read.
5+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(23,12): error TS6133: 'a3' is declared but its value is never read.
6+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(23,18): error TS6133: 'b3' is declared but its value is never read.
7+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(23,22): error TS6133: 'c3' is declared but its value is never read.
8+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(23,28): error TS6133: 'd3' is declared but its value is never read.
9+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts(23,32): error TS6133: 'e3' is declared but its value is never read.
10+
11+
12+
==== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts (9 errors) ====
13+
function t1() {
14+
const [_a1, b1] = [1, 2];
15+
console.log(b1);
16+
17+
const [a2, _b2] = [1, 2];
18+
console.log(a2);
19+
20+
const [_a3, _b3] = [1, 2];
21+
}
22+
23+
function t2() {
24+
const [_a1, b1] = [1, 2];
25+
~~
26+
!!! error TS6133: 'b1' is declared but its value is never read.
27+
const [a2, _b2] = [1, 2];
28+
~~
29+
!!! error TS6133: 'a2' is declared but its value is never read.
30+
const [a3, b3] = [1, 2];
31+
~~
32+
!!! error TS6133: 'a3' is declared but its value is never read.
33+
~~
34+
!!! error TS6133: 'b3' is declared but its value is never read.
35+
}
36+
37+
function t3() {
38+
const [_a1, [[_b1, c1]], d1, _e1] = [1, [[2, 3]], 4, 5];
39+
console.log(c1, d1);
40+
41+
const [_a2, [[_b2, _c2]], _d2, _e2] = [1, [[2, 3]], 4, 5];
42+
43+
const [a3, [[b3, c3]], d3, e3] = [1, [[2, 3]], 4, 5];
44+
~~
45+
!!! error TS6133: 'a3' is declared but its value is never read.
46+
~~
47+
!!! error TS6133: 'b3' is declared but its value is never read.
48+
~~
49+
!!! error TS6133: 'c3' is declared but its value is never read.
50+
~~
51+
!!! error TS6133: 'd3' is declared but its value is never read.
52+
~~
53+
!!! error TS6133: 'e3' is declared but its value is never read.
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [unusedVariablesWithUnderscoreInDestructuringArray.ts]
2+
function t1() {
3+
const [_a1, b1] = [1, 2];
4+
console.log(b1);
5+
6+
const [a2, _b2] = [1, 2];
7+
console.log(a2);
8+
9+
const [_a3, _b3] = [1, 2];
10+
}
11+
12+
function t2() {
13+
const [_a1, b1] = [1, 2];
14+
const [a2, _b2] = [1, 2];
15+
const [a3, b3] = [1, 2];
16+
}
17+
18+
function t3() {
19+
const [_a1, [[_b1, c1]], d1, _e1] = [1, [[2, 3]], 4, 5];
20+
console.log(c1, d1);
21+
22+
const [_a2, [[_b2, _c2]], _d2, _e2] = [1, [[2, 3]], 4, 5];
23+
24+
const [a3, [[b3, c3]], d3, e3] = [1, [[2, 3]], 4, 5];
25+
}
26+
27+
28+
//// [unusedVariablesWithUnderscoreInDestructuringArray.js]
29+
function t1() {
30+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
31+
console.log(b1);
32+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
33+
console.log(a2);
34+
var _c = [1, 2], _a3 = _c[0], _b3 = _c[1];
35+
}
36+
function t2() {
37+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
38+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
39+
var _c = [1, 2], a3 = _c[0], b3 = _c[1];
40+
}
41+
function t3() {
42+
var _a = [1, [[2, 3]], 4, 5], _a1 = _a[0], _b = _a[1][0], _b1 = _b[0], c1 = _b[1], d1 = _a[2], _e1 = _a[3];
43+
console.log(c1, d1);
44+
var _c = [1, [[2, 3]], 4, 5], _a2 = _c[0], _d = _c[1][0], _b2 = _d[0], _c2 = _d[1], _d2 = _c[2], _e2 = _c[3];
45+
var _e = [1, [[2, 3]], 4, 5], a3 = _e[0], _f = _e[1][0], b3 = _f[0], c3 = _f[1], d3 = _e[2], e3 = _e[3];
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts ===
2+
function t1() {
3+
>t1 : Symbol(t1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 0, 0))
4+
5+
const [_a1, b1] = [1, 2];
6+
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 1, 11))
7+
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 1, 15))
8+
9+
console.log(b1);
10+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
11+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
12+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
13+
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 1, 15))
14+
15+
const [a2, _b2] = [1, 2];
16+
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 4, 11))
17+
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 4, 14))
18+
19+
console.log(a2);
20+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
21+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
22+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
23+
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 4, 11))
24+
25+
const [_a3, _b3] = [1, 2];
26+
>_a3 : Symbol(_a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 7, 11))
27+
>_b3 : Symbol(_b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 7, 15))
28+
}
29+
30+
function t2() {
31+
>t2 : Symbol(t2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 8, 1))
32+
33+
const [_a1, b1] = [1, 2];
34+
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 11, 11))
35+
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 11, 15))
36+
37+
const [a2, _b2] = [1, 2];
38+
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 12, 11))
39+
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 12, 14))
40+
41+
const [a3, b3] = [1, 2];
42+
>a3 : Symbol(a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 13, 11))
43+
>b3 : Symbol(b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 13, 14))
44+
}
45+
46+
function t3() {
47+
>t3 : Symbol(t3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 14, 1))
48+
49+
const [_a1, [[_b1, c1]], d1, _e1] = [1, [[2, 3]], 4, 5];
50+
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 11))
51+
>_b1 : Symbol(_b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 18))
52+
>c1 : Symbol(c1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 22))
53+
>d1 : Symbol(d1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 28))
54+
>_e1 : Symbol(_e1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 32))
55+
56+
console.log(c1, d1);
57+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
58+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
59+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
60+
>c1 : Symbol(c1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 22))
61+
>d1 : Symbol(d1, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 17, 28))
62+
63+
const [_a2, [[_b2, _c2]], _d2, _e2] = [1, [[2, 3]], 4, 5];
64+
>_a2 : Symbol(_a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 20, 11))
65+
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 20, 18))
66+
>_c2 : Symbol(_c2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 20, 22))
67+
>_d2 : Symbol(_d2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 20, 29))
68+
>_e2 : Symbol(_e2, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 20, 34))
69+
70+
const [a3, [[b3, c3]], d3, e3] = [1, [[2, 3]], 4, 5];
71+
>a3 : Symbol(a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 22, 11))
72+
>b3 : Symbol(b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 22, 17))
73+
>c3 : Symbol(c3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 22, 20))
74+
>d3 : Symbol(d3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 22, 26))
75+
>e3 : Symbol(e3, Decl(unusedVariablesWithUnderscoreInDestructuringArray.ts, 22, 30))
76+
}
77+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray.ts ===
2+
function t1() {
3+
>t1 : () => void
4+
5+
const [_a1, b1] = [1, 2];
6+
>_a1 : number
7+
>b1 : number
8+
>[1, 2] : [number, number]
9+
>1 : 1
10+
>2 : 2
11+
12+
console.log(b1);
13+
>console.log(b1) : void
14+
>console.log : (...data: any[]) => void
15+
>console : Console
16+
>log : (...data: any[]) => void
17+
>b1 : number
18+
19+
const [a2, _b2] = [1, 2];
20+
>a2 : number
21+
>_b2 : number
22+
>[1, 2] : [number, number]
23+
>1 : 1
24+
>2 : 2
25+
26+
console.log(a2);
27+
>console.log(a2) : void
28+
>console.log : (...data: any[]) => void
29+
>console : Console
30+
>log : (...data: any[]) => void
31+
>a2 : number
32+
33+
const [_a3, _b3] = [1, 2];
34+
>_a3 : number
35+
>_b3 : number
36+
>[1, 2] : [number, number]
37+
>1 : 1
38+
>2 : 2
39+
}
40+
41+
function t2() {
42+
>t2 : () => void
43+
44+
const [_a1, b1] = [1, 2];
45+
>_a1 : number
46+
>b1 : number
47+
>[1, 2] : [number, number]
48+
>1 : 1
49+
>2 : 2
50+
51+
const [a2, _b2] = [1, 2];
52+
>a2 : number
53+
>_b2 : number
54+
>[1, 2] : [number, number]
55+
>1 : 1
56+
>2 : 2
57+
58+
const [a3, b3] = [1, 2];
59+
>a3 : number
60+
>b3 : number
61+
>[1, 2] : [number, number]
62+
>1 : 1
63+
>2 : 2
64+
}
65+
66+
function t3() {
67+
>t3 : () => void
68+
69+
const [_a1, [[_b1, c1]], d1, _e1] = [1, [[2, 3]], 4, 5];
70+
>_a1 : number
71+
>_b1 : number
72+
>c1 : number
73+
>d1 : number
74+
>_e1 : number
75+
>[1, [[2, 3]], 4, 5] : [number, [[number, number]], number, number]
76+
>1 : 1
77+
>[[2, 3]] : [[number, number]]
78+
>[2, 3] : [number, number]
79+
>2 : 2
80+
>3 : 3
81+
>4 : 4
82+
>5 : 5
83+
84+
console.log(c1, d1);
85+
>console.log(c1, d1) : void
86+
>console.log : (...data: any[]) => void
87+
>console : Console
88+
>log : (...data: any[]) => void
89+
>c1 : number
90+
>d1 : number
91+
92+
const [_a2, [[_b2, _c2]], _d2, _e2] = [1, [[2, 3]], 4, 5];
93+
>_a2 : number
94+
>_b2 : number
95+
>_c2 : number
96+
>_d2 : number
97+
>_e2 : number
98+
>[1, [[2, 3]], 4, 5] : [number, [[number, number]], number, number]
99+
>1 : 1
100+
>[[2, 3]] : [[number, number]]
101+
>[2, 3] : [number, number]
102+
>2 : 2
103+
>3 : 3
104+
>4 : 4
105+
>5 : 5
106+
107+
const [a3, [[b3, c3]], d3, e3] = [1, [[2, 3]], 4, 5];
108+
>a3 : number
109+
>b3 : number
110+
>c3 : number
111+
>d3 : number
112+
>e3 : number
113+
>[1, [[2, 3]], 4, 5] : [number, [[number, number]], number, number]
114+
>1 : 1
115+
>[[2, 3]] : [[number, number]]
116+
>[2, 3] : [number, number]
117+
>2 : 2
118+
>3 : 3
119+
>4 : 4
120+
>5 : 5
121+
}
122+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(15,21): error TS6133: 'b' is declared but its value is never read.
2+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(17,17): error TS6133: 'a' is declared but its value is never read.
3+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(19,17): error TS6133: 'a' is declared but its value is never read.
4+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(19,20): error TS6133: 'b' is declared but its value is never read.
5+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(23,23): error TS6133: 'b' is declared but its value is never read.
6+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(25,19): error TS6133: 'a' is declared but its value is never read.
7+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(27,19): error TS6133: 'a' is declared but its value is never read.
8+
tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts(27,22): error TS6133: 'b' is declared but its value is never read.
9+
10+
11+
==== tests/cases/compiler/unusedVariablesWithUnderscoreInForOfLoop.ts (8 errors) ====
12+
function t1() {
13+
for (const [_a, b] of [['key', 1]]) {
14+
console.log(b);
15+
}
16+
17+
for (const [a, _b] of [['key', 1]]) {
18+
console.log(a);
19+
}
20+
21+
for (const [_a, _b] of [['key', 1]]) {}
22+
}
23+
24+
25+
function t2() {
26+
for (const [_a, b] of [['key', 1]]) {}
27+
~
28+
!!! error TS6133: 'b' is declared but its value is never read.
29+
30+
for (const [a, _b] of [['key', 1]]) {}
31+
~
32+
!!! error TS6133: 'a' is declared but its value is never read.
33+
34+
for (const [a, b] of [['key', 1]]) {}
35+
~
36+
!!! error TS6133: 'a' is declared but its value is never read.
37+
~
38+
!!! error TS6133: 'b' is declared but its value is never read.
39+
}
40+
41+
function t3() {
42+
for (const [[[_a, b]]] of [[[['key', 1]]]]) {}
43+
~
44+
!!! error TS6133: 'b' is declared but its value is never read.
45+
46+
for (const [[[a, _b]]] of [[[['key', 1]]]]) {}
47+
~
48+
!!! error TS6133: 'a' is declared but its value is never read.
49+
50+
for (const [[[a, b]]] of [[[['key', 1]]]]) {}
51+
~
52+
!!! error TS6133: 'a' is declared but its value is never read.
53+
~
54+
!!! error TS6133: 'b' is declared but its value is never read.
55+
}
56+

0 commit comments

Comments
 (0)