Skip to content

Commit 406f130

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

23 files changed

+326
-144
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 4 deletions
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) ||
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [unusedVariablesWithUnderscoreInDestructuringArray1.ts]
2+
function test() {
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+
13+
//// [unusedVariablesWithUnderscoreInDestructuringArray1.js]
14+
function test() {
15+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
16+
console.log(b1);
17+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
18+
console.log(a2);
19+
var _c = [1, 2], _a3 = _c[0], _b3 = _c[1];
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray1.ts ===
2+
function test() {
3+
>test : Symbol(test, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 0, 0))
4+
5+
const [_a1, b1] = [1, 2];
6+
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 1, 11))
7+
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.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(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 1, 15))
14+
15+
const [a2, _b2] = [1, 2];
16+
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 4, 11))
17+
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.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(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 4, 11))
24+
25+
const [_a3, _b3] = [1, 2];
26+
>_a3 : Symbol(_a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 7, 11))
27+
>_b3 : Symbol(_b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 7, 15))
28+
}
29+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray1.ts ===
2+
function test() {
3+
>test : () => 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+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(2,17): error TS6133: 'b1' is declared but its value is never read.
2+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(3,12): error TS6133: 'a2' is declared but its value is never read.
3+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(4,12): error TS6133: 'a3' is declared but its value is never read.
4+
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(4,16): error TS6133: 'b3' is declared but its value is never read.
5+
6+
7+
==== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts (4 errors) ====
8+
function f() {
9+
const [_a1, b1] = [1, 2];
10+
~~
11+
!!! error TS6133: 'b1' is declared but its value is never read.
12+
const [a2, _b2] = [1, 2];
13+
~~
14+
!!! error TS6133: 'a2' is declared but its value is never read.
15+
const [a3, b3] = [1, 2];
16+
~~
17+
!!! error TS6133: 'a3' is declared but its value is never read.
18+
~~
19+
!!! error TS6133: 'b3' is declared but its value is never read.
20+
}
21+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [unusedVariablesWithUnderscoreInDestructuringArray2.ts]
2+
function f() {
3+
const [_a1, b1] = [1, 2];
4+
const [a2, _b2] = [1, 2];
5+
const [a3, b3] = [1, 2];
6+
}
7+
8+
9+
//// [unusedVariablesWithUnderscoreInDestructuringArray2.js]
10+
function f() {
11+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
12+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
13+
var _c = [1, 2], a3 = _c[0], b3 = _c[1];
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts ===
2+
function f() {
3+
>f : Symbol(f, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 0, 0))
4+
5+
const [_a1, b1] = [1, 2];
6+
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 1, 11))
7+
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 1, 15))
8+
9+
const [a2, _b2] = [1, 2];
10+
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 2, 11))
11+
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 2, 14))
12+
13+
const [a3, b3] = [1, 2];
14+
>a3 : Symbol(a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 3, 11))
15+
>b3 : Symbol(b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 3, 14))
16+
}
17+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts ===
2+
function f() {
3+
>f : () => 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+
const [a2, _b2] = [1, 2];
13+
>a2 : number
14+
>_b2 : number
15+
>[1, 2] : [number, number]
16+
>1 : 1
17+
>2 : 2
18+
19+
const [a3, b3] = [1, 2];
20+
>a3 : number
21+
>b3 : number
22+
>[1, 2] : [number, number]
23+
>1 : 1
24+
>2 : 2
25+
}
26+

tests/baselines/reference/unusedVariablesWithUnderscoreInForOfLoop.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/baselines/reference/unusedVariablesWithUnderscoreInForOfLoop.symbols

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/baselines/reference/unusedVariablesWithUnderscoreInForOfLoop.types

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
//// [unusedVariablesWithUnderscoreInForOfLoop1.ts]
22
function f() {
3-
for (const [_a, b] of [['key', 1]]) {}
3+
for (const [_a, b] of [['key', 1]]) {
4+
console.log(b);
5+
}
46

5-
for (const [a, _b] of [['key', 1]]) {}
7+
for (const [a, _b] of [['key', 1]]) {
8+
console.log(a);
9+
}
610

7-
for (const [a, b] of [['key', 1]]) {}
11+
for (const [_a, _b] of [['key', 1]]) {}
812
}
913

1014

1115
//// [unusedVariablesWithUnderscoreInForOfLoop1.js]
1216
function f() {
1317
for (var _i = 0, _c = [['key', 1]]; _i < _c.length; _i++) {
1418
var _d = _c[_i], _a = _d[0], b = _d[1];
19+
console.log(b);
1520
}
1621
for (var _e = 0, _f = [['key', 1]]; _e < _f.length; _e++) {
1722
var _g = _f[_e], a = _g[0], _b = _g[1];
23+
console.log(a);
1824
}
1925
for (var _h = 0, _j = [['key', 1]]; _h < _j.length; _h++) {
20-
var _k = _j[_h], a = _k[0], b = _k[1];
26+
var _k = _j[_h], _a = _k[0], _b = _k[1];
2127
}
2228
}

tests/baselines/reference/unusedVariablesWithUnderscoreInForOfLoop1.symbols

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22
function f() {
33
>f : Symbol(f, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 0, 0))
44

5-
for (const [_a, b] of [['key', 1]]) {}
5+
for (const [_a, b] of [['key', 1]]) {
66
>_a : Symbol(_a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 1, 16))
77
>b : Symbol(b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 1, 19))
88

9-
for (const [a, _b] of [['key', 1]]) {}
10-
>a : Symbol(a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 3, 16))
11-
>_b : Symbol(_b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 3, 18))
9+
console.log(b);
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+
>b : Symbol(b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 1, 19))
14+
}
15+
16+
for (const [a, _b] of [['key', 1]]) {
17+
>a : Symbol(a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 16))
18+
>_b : Symbol(_b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 18))
1219

13-
for (const [a, b] of [['key', 1]]) {}
20+
console.log(a);
21+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
22+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
23+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
1424
>a : Symbol(a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 16))
15-
>b : Symbol(b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 18))
25+
}
26+
27+
for (const [_a, _b] of [['key', 1]]) {}
28+
>_a : Symbol(_a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 9, 16))
29+
>_b : Symbol(_b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 9, 19))
1630
}
1731

0 commit comments

Comments
 (0)