Skip to content

Commit ec02d5b

Browse files
committed
feat(31388): allow binding elements starting with an underscore
1 parent 9871b5f commit ec02d5b

16 files changed

+1134
-115
lines changed

src/compiler/checker.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -33362,14 +33362,8 @@ namespace ts {
3336233362
}
3336333363

3336433364
function isValidUnusedLocalDeclaration(declaration: Declaration): boolean {
33365-
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-
);
33370-
}
33371-
3337233365
return isAmbientModule(declaration) ||
33366+
(isBindingElement(declaration) && isIdentifierThatStartsWithUnderscore(declaration.name)) ||
3337333367
(isVariableDeclaration(declaration) && isForInOrOfStatement(declaration.parent.parent) || isImportedDeclaration(declaration)) && isIdentifierThatStartsWithUnderscore(declaration.name!);
3337433368
}
3337533369

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(12,17): error TS6133: 'b1' is declared but its value is never read.
2+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(13,12): error TS6133: 'a2' is declared but its value is never read.
3+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(14,12): error TS6133: 'a3' is declared but its value is never read.
4+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(14,16): error TS6133: 'b3' is declared but its value is never read.
5+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(23,12): error TS6133: 'a3' is declared but its value is never read.
6+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(23,18): error TS6133: 'b3' is declared but its value is never read.
7+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(23,22): error TS6133: 'c3' is declared but its value is never read.
8+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(23,28): error TS6133: 'd3' is declared but its value is never read.
9+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(23,32): error TS6133: 'e3' is declared but its value is never read.
10+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(37,22): error TS6133: 'b1' is declared but its value is never read.
11+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(38,13): error TS6133: 'a2' is declared but its value is never read.
12+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(39,11): error TS6198: All destructured elements are unused.
13+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(68,9): error TS6133: 'a3' is declared but its value is never read.
14+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(70,18): error TS6198: All destructured elements are unused.
15+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(74,9): error TS6133: 'c3' is declared but its value is never read.
16+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(75,9): error TS6133: 'd3' is declared but its value is never read.
17+
18+
19+
==== tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts (16 errors) ====
20+
function t1() {
21+
const [_a1, b1] = [1, 2];
22+
console.log(b1);
23+
24+
const [a2, _b2] = [1, 2];
25+
console.log(a2);
26+
27+
const [_a3, _b3] = [1, 2];
28+
}
29+
30+
function t2() {
31+
const [_a1, b1] = [1, 2];
32+
~~
33+
!!! error TS6133: 'b1' is declared but its value is never read.
34+
const [a2, _b2] = [1, 2];
35+
~~
36+
!!! error TS6133: 'a2' is declared but its value is never read.
37+
const [a3, b3] = [1, 2];
38+
~~
39+
!!! error TS6133: 'a3' is declared but its value is never read.
40+
~~
41+
!!! error TS6133: 'b3' is declared but its value is never read.
42+
}
43+
44+
function t3() {
45+
const [_a1, [[_b1, c1]], d1, _e1] = [1, [[2, 3]], 4, 5];
46+
console.log(c1, d1);
47+
48+
const [_a2, [[_b2, _c2]], _d2, _e2] = [1, [[2, 3]], 4, 5];
49+
50+
const [a3, [[b3, c3]], d3, e3] = [1, [[2, 3]], 4, 5];
51+
~~
52+
!!! error TS6133: 'a3' is declared but its value is never read.
53+
~~
54+
!!! error TS6133: 'b3' is declared but its value is never read.
55+
~~
56+
!!! error TS6133: 'c3' is declared but its value is never read.
57+
~~
58+
!!! error TS6133: 'd3' is declared but its value is never read.
59+
~~
60+
!!! error TS6133: 'e3' is declared but its value is never read.
61+
}
62+
63+
function t4() {
64+
const { a1: _a1, b1 } = { a1: 1, b1: 1 };
65+
console.log(b1);
66+
67+
const { a2, b2: _b2 } = { a2: 1, b2: 1 };
68+
console.log(a2);
69+
70+
const { a3: _a3, b3: _b3 } = { a3: 1, b3: 1 };
71+
}
72+
73+
function t5() {
74+
const { a1: _a1, b1 } = { a1: 1, b1: 1 };
75+
~~
76+
!!! error TS6133: 'b1' is declared but its value is never read.
77+
const { a2, b2: _b2 } = { a2: 1, b2: 1 };
78+
~~
79+
!!! error TS6133: 'a2' is declared but its value is never read.
80+
const { a3, b3 } = { a3: 1, b3: 1 };
81+
~~~~~~~~~~
82+
!!! error TS6198: All destructured elements are unused.
83+
}
84+
85+
function t6() {
86+
const {
87+
a1: _a1,
88+
b1: {
89+
b11: {
90+
b111: _b111,
91+
b112
92+
}
93+
},
94+
c1,
95+
d1: _d1
96+
} = { a1: 1, b1: { b11: { b111: 1, b112: 1 } }, c1: 1, d1: 1 };
97+
console.log(b112, c1);
98+
99+
const {
100+
a2: _a2,
101+
b2: {
102+
b21: {
103+
b211: _b211, b212: _b212
104+
}
105+
},
106+
c2: _c2,
107+
d2: _d2
108+
} = { a2: 1, b2: { b21: { b211: 1, b212: 1 } }, c2: 1, d2: 1 };
109+
110+
const {
111+
a3,
112+
~~
113+
!!! error TS6133: 'a3' is declared but its value is never read.
114+
b3: {
115+
b31: {
116+
~
117+
b311, b312
118+
~~~~~~~~~~~~~~~~~~~~~~~~~~
119+
}
120+
~~~~~~~~~~~~~
121+
!!! error TS6198: All destructured elements are unused.
122+
},
123+
c3,
124+
~~
125+
!!! error TS6133: 'c3' is declared but its value is never read.
126+
d3
127+
~~
128+
!!! error TS6133: 'd3' is declared but its value is never read.
129+
} = { a3: 1, b3: { b31: { b311: 1, b312: 1 } }, c3: 1, d3: 1 };
130+
}
131+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//// [unusedVariablesWithUnderscoreInBindingElement.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+
function t4() {
28+
const { a1: _a1, b1 } = { a1: 1, b1: 1 };
29+
console.log(b1);
30+
31+
const { a2, b2: _b2 } = { a2: 1, b2: 1 };
32+
console.log(a2);
33+
34+
const { a3: _a3, b3: _b3 } = { a3: 1, b3: 1 };
35+
}
36+
37+
function t5() {
38+
const { a1: _a1, b1 } = { a1: 1, b1: 1 };
39+
const { a2, b2: _b2 } = { a2: 1, b2: 1 };
40+
const { a3, b3 } = { a3: 1, b3: 1 };
41+
}
42+
43+
function t6() {
44+
const {
45+
a1: _a1,
46+
b1: {
47+
b11: {
48+
b111: _b111,
49+
b112
50+
}
51+
},
52+
c1,
53+
d1: _d1
54+
} = { a1: 1, b1: { b11: { b111: 1, b112: 1 } }, c1: 1, d1: 1 };
55+
console.log(b112, c1);
56+
57+
const {
58+
a2: _a2,
59+
b2: {
60+
b21: {
61+
b211: _b211, b212: _b212
62+
}
63+
},
64+
c2: _c2,
65+
d2: _d2
66+
} = { a2: 1, b2: { b21: { b211: 1, b212: 1 } }, c2: 1, d2: 1 };
67+
68+
const {
69+
a3,
70+
b3: {
71+
b31: {
72+
b311, b312
73+
}
74+
},
75+
c3,
76+
d3
77+
} = { a3: 1, b3: { b31: { b311: 1, b312: 1 } }, c3: 1, d3: 1 };
78+
}
79+
80+
81+
//// [unusedVariablesWithUnderscoreInBindingElement.js]
82+
function t1() {
83+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
84+
console.log(b1);
85+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
86+
console.log(a2);
87+
var _c = [1, 2], _a3 = _c[0], _b3 = _c[1];
88+
}
89+
function t2() {
90+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
91+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
92+
var _c = [1, 2], a3 = _c[0], b3 = _c[1];
93+
}
94+
function t3() {
95+
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];
96+
console.log(c1, d1);
97+
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];
98+
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];
99+
}
100+
function t4() {
101+
var _a = { a1: 1, b1: 1 }, _a1 = _a.a1, b1 = _a.b1;
102+
console.log(b1);
103+
var _b = { a2: 1, b2: 1 }, a2 = _b.a2, _b2 = _b.b2;
104+
console.log(a2);
105+
var _c = { a3: 1, b3: 1 }, _a3 = _c.a3, _b3 = _c.b3;
106+
}
107+
function t5() {
108+
var _a = { a1: 1, b1: 1 }, _a1 = _a.a1, b1 = _a.b1;
109+
var _b = { a2: 1, b2: 1 }, a2 = _b.a2, _b2 = _b.b2;
110+
var _c = { a3: 1, b3: 1 }, a3 = _c.a3, b3 = _c.b3;
111+
}
112+
function t6() {
113+
var _a = { a1: 1, b1: { b11: { b111: 1, b112: 1 } }, c1: 1, d1: 1 }, _a1 = _a.a1, _b = _a.b1.b11, _b111 = _b.b111, b112 = _b.b112, c1 = _a.c1, _d1 = _a.d1;
114+
console.log(b112, c1);
115+
var _c = { a2: 1, b2: { b21: { b211: 1, b212: 1 } }, c2: 1, d2: 1 }, _a2 = _c.a2, _d = _c.b2.b21, _b211 = _d.b211, _b212 = _d.b212, _c2 = _c.c2, _d2 = _c.d2;
116+
var _e = { a3: 1, b3: { b31: { b311: 1, b312: 1 } }, c3: 1, d3: 1 }, a3 = _e.a3, _f = _e.b3.b31, b311 = _f.b311, b312 = _f.b312, c3 = _e.c3, d3 = _e.d3;
117+
}

0 commit comments

Comments
 (0)