Skip to content

Commit a276a6d

Browse files
authored
feat(31388): allow binding elements starting with an underscore (#41378)
1 parent a894f8a commit a276a6d

16 files changed

+1253
-114
lines changed

Diff for: src/compiler/checker.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -33839,13 +33839,16 @@ namespace ts {
3383933839
}
3384033840

3384133841
function isValidUnusedLocalDeclaration(declaration: Declaration): boolean {
33842-
if (isBindingElement(declaration) && isIdentifierThatStartsWithUnderscore(declaration.name)) {
33843-
return !!findAncestor(declaration.parent, ancestor =>
33844-
isArrayBindingPattern(ancestor) || isVariableDeclaration(ancestor) || isVariableDeclarationList(ancestor) ? false :
33845-
isForOfStatement(ancestor) ? true : "quit"
33846-
);
33842+
if (isBindingElement(declaration)) {
33843+
if (isObjectBindingPattern(declaration.parent)) {
33844+
/**
33845+
* ignore starts with underscore names _
33846+
* const { a: _a } = { a: 1 }
33847+
*/
33848+
return !!(declaration.propertyName && isIdentifierThatStartsWithUnderscore(declaration.name));
33849+
}
33850+
return isIdentifierThatStartsWithUnderscore(declaration.name);
3384733851
}
33848-
3384933852
return isAmbientModule(declaration) ||
3385033853
(isVariableDeclaration(declaration) && isForInOrOfStatement(declaration.parent.parent) || isImportedDeclaration(declaration)) && isIdentifierThatStartsWithUnderscore(declaration.name!);
3385133854
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts(81,11): error TS6198: All destructured elements are unused.
18+
19+
20+
==== tests/cases/compiler/unusedVariablesWithUnderscoreInBindingElement.ts (17 errors) ====
21+
function t1() {
22+
const [_a1, b1] = [1, 2];
23+
console.log(b1);
24+
25+
const [a2, _b2] = [1, 2];
26+
console.log(a2);
27+
28+
const [_a3, _b3] = [1, 2];
29+
}
30+
31+
function t2() {
32+
const [_a1, b1] = [1, 2];
33+
~~
34+
!!! error TS6133: 'b1' is declared but its value is never read.
35+
const [a2, _b2] = [1, 2];
36+
~~
37+
!!! error TS6133: 'a2' is declared but its value is never read.
38+
const [a3, b3] = [1, 2];
39+
~~
40+
!!! error TS6133: 'a3' is declared but its value is never read.
41+
~~
42+
!!! error TS6133: 'b3' is declared but its value is never read.
43+
}
44+
45+
function t3() {
46+
const [_a1, [[_b1, c1]], d1, _e1] = [1, [[2, 3]], 4, 5];
47+
console.log(c1, d1);
48+
49+
const [_a2, [[_b2, _c2]], _d2, _e2] = [1, [[2, 3]], 4, 5];
50+
51+
const [a3, [[b3, c3]], d3, e3] = [1, [[2, 3]], 4, 5];
52+
~~
53+
!!! error TS6133: 'a3' is declared but its value is never read.
54+
~~
55+
!!! error TS6133: 'b3' is declared but its value is never read.
56+
~~
57+
!!! error TS6133: 'c3' is declared but its value is never read.
58+
~~
59+
!!! error TS6133: 'd3' is declared but its value is never read.
60+
~~
61+
!!! error TS6133: 'e3' is declared but its value is never read.
62+
}
63+
64+
function t4() {
65+
const { a1: _a1, b1 } = { a1: 1, b1: 1 };
66+
console.log(b1);
67+
68+
const { a2, b2: _b2 } = { a2: 1, b2: 1 };
69+
console.log(a2);
70+
71+
const { a3: _a3, b3: _b3 } = { a3: 1, b3: 1 };
72+
}
73+
74+
function t5() {
75+
const { a1: _a1, b1 } = { a1: 1, b1: 1 };
76+
~~
77+
!!! error TS6133: 'b1' is declared but its value is never read.
78+
const { a2, b2: _b2 } = { a2: 1, b2: 1 };
79+
~~
80+
!!! error TS6133: 'a2' is declared but its value is never read.
81+
const { a3, b3 } = { a3: 1, b3: 1 };
82+
~~~~~~~~~~
83+
!!! error TS6198: All destructured elements are unused.
84+
}
85+
86+
function t6() {
87+
const {
88+
a1: _a1,
89+
b1: {
90+
b11: {
91+
b111: _b111,
92+
b112
93+
}
94+
},
95+
c1,
96+
d1: _d1
97+
} = { a1: 1, b1: { b11: { b111: 1, b112: 1 } }, c1: 1, d1: 1 };
98+
console.log(b112, c1);
99+
100+
const {
101+
a2: _a2,
102+
b2: {
103+
b21: {
104+
b211: _b211, b212: _b212
105+
}
106+
},
107+
c2: _c2,
108+
d2: _d2
109+
} = { a2: 1, b2: { b21: { b211: 1, b212: 1 } }, c2: 1, d2: 1 };
110+
111+
const {
112+
a3,
113+
~~
114+
!!! error TS6133: 'a3' is declared but its value is never read.
115+
b3: {
116+
b31: {
117+
~
118+
b311, b312
119+
~~~~~~~~~~~~~~~~~~~~~~~~~~
120+
}
121+
~~~~~~~~~~~~~
122+
!!! error TS6198: All destructured elements are unused.
123+
},
124+
c3,
125+
~~
126+
!!! error TS6133: 'c3' is declared but its value is never read.
127+
d3
128+
~~
129+
!!! error TS6133: 'd3' is declared but its value is never read.
130+
} = { a3: 1, b3: { b31: { b311: 1, b312: 1 } }, c3: 1, d3: 1 };
131+
}
132+
133+
function t7() {
134+
// error
135+
const { _a1, _b1 } = { _a1: 1, _b1: 1 };
136+
~~~~~~~~~~~~
137+
!!! error TS6198: All destructured elements are unused.
138+
139+
// ok
140+
const { a2: _a2, b2: _b2 } = { a2: 1, b2: 1 };
141+
142+
// ok
143+
const { _a3: _ignoreA3, _b3: _ignoreB3 } = { _a3: 1, _b3: 1 };
144+
}
145+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
function t7() {
81+
// error
82+
const { _a1, _b1 } = { _a1: 1, _b1: 1 };
83+
84+
// ok
85+
const { a2: _a2, b2: _b2 } = { a2: 1, b2: 1 };
86+
87+
// ok
88+
const { _a3: _ignoreA3, _b3: _ignoreB3 } = { _a3: 1, _b3: 1 };
89+
}
90+
91+
92+
//// [unusedVariablesWithUnderscoreInBindingElement.js]
93+
function t1() {
94+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
95+
console.log(b1);
96+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
97+
console.log(a2);
98+
var _c = [1, 2], _a3 = _c[0], _b3 = _c[1];
99+
}
100+
function t2() {
101+
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
102+
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
103+
var _c = [1, 2], a3 = _c[0], b3 = _c[1];
104+
}
105+
function t3() {
106+
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];
107+
console.log(c1, d1);
108+
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];
109+
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];
110+
}
111+
function t4() {
112+
var _a = { a1: 1, b1: 1 }, _a1 = _a.a1, b1 = _a.b1;
113+
console.log(b1);
114+
var _b = { a2: 1, b2: 1 }, a2 = _b.a2, _b2 = _b.b2;
115+
console.log(a2);
116+
var _c = { a3: 1, b3: 1 }, _a3 = _c.a3, _b3 = _c.b3;
117+
}
118+
function t5() {
119+
var _a = { a1: 1, b1: 1 }, _a1 = _a.a1, b1 = _a.b1;
120+
var _b = { a2: 1, b2: 1 }, a2 = _b.a2, _b2 = _b.b2;
121+
var _c = { a3: 1, b3: 1 }, a3 = _c.a3, b3 = _c.b3;
122+
}
123+
function t6() {
124+
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;
125+
console.log(b112, c1);
126+
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;
127+
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;
128+
}
129+
function t7() {
130+
// error
131+
var _a = { _a1: 1, _b1: 1 }, _a1 = _a._a1, _b1 = _a._b1;
132+
// ok
133+
var _b = { a2: 1, b2: 1 }, _a2 = _b.a2, _b2 = _b.b2;
134+
// ok
135+
var _c = { _a3: 1, _b3: 1 }, _ignoreA3 = _c._a3, _ignoreB3 = _c._b3;
136+
}

0 commit comments

Comments
 (0)