Skip to content

Commit a2fb5f9

Browse files
authored
Merge pull request #12778 from Microsoft/unusedLocalsAndObjectSpread
Do not report unused local error on locals that are intended for removing properties with object spread
2 parents 7da3383 + 81452c5 commit a2fb5f9

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16702,6 +16702,14 @@ namespace ts {
1670216702
}
1670316703
}
1670416704

16705+
function isRemovedPropertyFromObjectSpread(node: Node) {
16706+
if (isBindingElement(node) && isObjectBindingPattern(node.parent)) {
16707+
const lastElement = lastOrUndefined(node.parent.elements);
16708+
return lastElement !== node && !!lastElement.dotDotDotToken;
16709+
}
16710+
return false;
16711+
}
16712+
1670516713
function errorUnusedLocal(node: Node, name: string) {
1670616714
if (isIdentifierThatStartsWithUnderScore(node)) {
1670716715
const declaration = getRootDeclaration(node.parent);
@@ -16711,7 +16719,10 @@ namespace ts {
1671116719
return;
1671216720
}
1671316721
}
16714-
error(node, Diagnostics._0_is_declared_but_never_used, name);
16722+
16723+
if (!isRemovedPropertyFromObjectSpread(node.kind === SyntaxKind.Identifier ? node.parent : node)) {
16724+
error(node, Diagnostics._0_is_declared_but_never_used, name);
16725+
}
1671516726
}
1671616727

1671716728
function parameterNameStartsWithUnderscore(parameterName: DeclarationName) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
tests/cases/compiler/unusedLocalsAndObjectSpread.ts(21,18): error TS6133: 'bar' is declared but never used.
2+
tests/cases/compiler/unusedLocalsAndObjectSpread.ts(28,21): error TS6133: 'bar' is declared but never used.
3+
4+
5+
==== tests/cases/compiler/unusedLocalsAndObjectSpread.ts (2 errors) ====
6+
7+
declare var console: { log(a: any): void };
8+
9+
function one() {
10+
const foo = { a: 1, b: 2 };
11+
// 'a' is declared but never used
12+
const {a, ...bar} = foo;
13+
console.log(bar);
14+
}
15+
16+
function two() {
17+
const foo = { a: 1, b: 2 };
18+
// '_' is declared but never used
19+
const {a: _, ...bar} = foo;
20+
console.log(bar);
21+
}
22+
23+
function three() {
24+
const foo = { a: 1, b: 2 };
25+
// 'a' is declared but never used
26+
const {a, ...bar} = foo; // bar should be unused
27+
~~~
28+
!!! error TS6133: 'bar' is declared but never used.
29+
//console.log(bar);
30+
}
31+
32+
function four() {
33+
const foo = { a: 1, b: 2 };
34+
// '_' is declared but never used
35+
const {a: _, ...bar} = foo; // bar should be unused
36+
~~~
37+
!!! error TS6133: 'bar' is declared but never used.
38+
//console.log(bar);
39+
}
40+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//// [unusedLocalsAndObjectSpread.ts]
2+
3+
declare var console: { log(a: any): void };
4+
5+
function one() {
6+
const foo = { a: 1, b: 2 };
7+
// 'a' is declared but never used
8+
const {a, ...bar} = foo;
9+
console.log(bar);
10+
}
11+
12+
function two() {
13+
const foo = { a: 1, b: 2 };
14+
// '_' is declared but never used
15+
const {a: _, ...bar} = foo;
16+
console.log(bar);
17+
}
18+
19+
function three() {
20+
const foo = { a: 1, b: 2 };
21+
// 'a' is declared but never used
22+
const {a, ...bar} = foo; // bar should be unused
23+
//console.log(bar);
24+
}
25+
26+
function four() {
27+
const foo = { a: 1, b: 2 };
28+
// '_' is declared but never used
29+
const {a: _, ...bar} = foo; // bar should be unused
30+
//console.log(bar);
31+
}
32+
33+
34+
//// [unusedLocalsAndObjectSpread.js]
35+
var __rest = (this && this.__rest) || function (s, e) {
36+
var t = {};
37+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
38+
t[p] = s[p];
39+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
40+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
41+
t[p[i]] = s[p[i]];
42+
return t;
43+
};
44+
function one() {
45+
var foo = { a: 1, b: 2 };
46+
// 'a' is declared but never used
47+
var a = foo.a, bar = __rest(foo, ["a"]);
48+
console.log(bar);
49+
}
50+
function two() {
51+
var foo = { a: 1, b: 2 };
52+
// '_' is declared but never used
53+
var _ = foo.a, bar = __rest(foo, ["a"]);
54+
console.log(bar);
55+
}
56+
function three() {
57+
var foo = { a: 1, b: 2 };
58+
// 'a' is declared but never used
59+
var a = foo.a, bar = __rest(foo, ["a"]); // bar should be unused
60+
//console.log(bar);
61+
}
62+
function four() {
63+
var foo = { a: 1, b: 2 };
64+
// '_' is declared but never used
65+
var _ = foo.a, bar = __rest(foo, ["a"]); // bar should be unused
66+
//console.log(bar);
67+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@noUnusedLocals:true
2+
3+
declare var console: { log(a: any): void };
4+
5+
function one() {
6+
const foo = { a: 1, b: 2 };
7+
// 'a' is declared but never used
8+
const {a, ...bar} = foo;
9+
console.log(bar);
10+
}
11+
12+
function two() {
13+
const foo = { a: 1, b: 2 };
14+
// '_' is declared but never used
15+
const {a: _, ...bar} = foo;
16+
console.log(bar);
17+
}
18+
19+
function three() {
20+
const foo = { a: 1, b: 2 };
21+
// 'a' is declared but never used
22+
const {a, ...bar} = foo; // bar should be unused
23+
//console.log(bar);
24+
}
25+
26+
function four() {
27+
const foo = { a: 1, b: 2 };
28+
// '_' is declared but never used
29+
const {a: _, ...bar} = foo; // bar should be unused
30+
//console.log(bar);
31+
}

0 commit comments

Comments
 (0)