Skip to content

Do not report unused local error on locals that are intended for removing properties with object spread #12778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16702,6 +16702,14 @@ namespace ts {
}
}

function isRemovedPropertyFromObjectSpread(node: Node) {
if (isBindingElement(node) && isObjectBindingPattern(node.parent)) {
const lastElement = lastOrUndefined(node.parent.elements);
return lastElement !== node && !!lastElement.dotDotDotToken;
}
return false;
}

function errorUnusedLocal(node: Node, name: string) {
if (isIdentifierThatStartsWithUnderScore(node)) {
const declaration = getRootDeclaration(node.parent);
Expand All @@ -16711,7 +16719,10 @@ namespace ts {
return;
}
}
error(node, Diagnostics._0_is_declared_but_never_used, name);

if (!isRemovedPropertyFromObjectSpread(node.kind === SyntaxKind.Identifier ? node.parent : node)) {
error(node, Diagnostics._0_is_declared_but_never_used, name);
}
}

function parameterNameStartsWithUnderscore(parameterName: DeclarationName) {
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/unusedLocalsAndObjectSpread.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
tests/cases/compiler/unusedLocalsAndObjectSpread.ts(21,18): error TS6133: 'bar' is declared but never used.
tests/cases/compiler/unusedLocalsAndObjectSpread.ts(28,21): error TS6133: 'bar' is declared but never used.


==== tests/cases/compiler/unusedLocalsAndObjectSpread.ts (2 errors) ====

declare var console: { log(a: any): void };

function one() {
const foo = { a: 1, b: 2 };
// 'a' is declared but never used
const {a, ...bar} = foo;
console.log(bar);
}

function two() {
const foo = { a: 1, b: 2 };
// '_' is declared but never used
const {a: _, ...bar} = foo;
console.log(bar);
}

function three() {
const foo = { a: 1, b: 2 };
// 'a' is declared but never used
const {a, ...bar} = foo; // bar should be unused
~~~
!!! error TS6133: 'bar' is declared but never used.
//console.log(bar);
}

function four() {
const foo = { a: 1, b: 2 };
// '_' is declared but never used
const {a: _, ...bar} = foo; // bar should be unused
~~~
!!! error TS6133: 'bar' is declared but never used.
//console.log(bar);
}

67 changes: 67 additions & 0 deletions tests/baselines/reference/unusedLocalsAndObjectSpread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//// [unusedLocalsAndObjectSpread.ts]

declare var console: { log(a: any): void };

function one() {
const foo = { a: 1, b: 2 };
// 'a' is declared but never used
const {a, ...bar} = foo;
console.log(bar);
}

function two() {
const foo = { a: 1, b: 2 };
// '_' is declared but never used
const {a: _, ...bar} = foo;
console.log(bar);
}

function three() {
const foo = { a: 1, b: 2 };
// 'a' is declared but never used
const {a, ...bar} = foo; // bar should be unused
//console.log(bar);
}

function four() {
const foo = { a: 1, b: 2 };
// '_' is declared but never used
const {a: _, ...bar} = foo; // bar should be unused
//console.log(bar);
}


//// [unusedLocalsAndObjectSpread.js]
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
function one() {
var foo = { a: 1, b: 2 };
// 'a' is declared but never used
var a = foo.a, bar = __rest(foo, ["a"]);
console.log(bar);
}
function two() {
var foo = { a: 1, b: 2 };
// '_' is declared but never used
var _ = foo.a, bar = __rest(foo, ["a"]);
console.log(bar);
}
function three() {
var foo = { a: 1, b: 2 };
// 'a' is declared but never used
var a = foo.a, bar = __rest(foo, ["a"]); // bar should be unused
//console.log(bar);
}
function four() {
var foo = { a: 1, b: 2 };
// '_' is declared but never used
var _ = foo.a, bar = __rest(foo, ["a"]); // bar should be unused
//console.log(bar);
}
31 changes: 31 additions & 0 deletions tests/cases/compiler/unusedLocalsAndObjectSpread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//@noUnusedLocals:true

declare var console: { log(a: any): void };

function one() {
const foo = { a: 1, b: 2 };
// 'a' is declared but never used
const {a, ...bar} = foo;
console.log(bar);
}

function two() {
const foo = { a: 1, b: 2 };
// '_' is declared but never used
const {a: _, ...bar} = foo;
console.log(bar);
}

function three() {
const foo = { a: 1, b: 2 };
// 'a' is declared but never used
const {a, ...bar} = foo; // bar should be unused
//console.log(bar);
}

function four() {
const foo = { a: 1, b: 2 };
// '_' is declared but never used
const {a: _, ...bar} = foo; // bar should be unused
//console.log(bar);
}