Report error on unused destructured parameters

Fixes #11795
This commit is contained in:
Sheetal Nandi
2016-10-24 11:12:15 -07:00
parent 1e32b6742e
commit c1c670f8f6
7 changed files with 31 additions and 60 deletions

View File

@@ -15832,12 +15832,12 @@ namespace ts {
for (const key in node.locals) {
const local = node.locals[key];
if (!local.isReferenced) {
if (local.valueDeclaration && local.valueDeclaration.kind === SyntaxKind.Parameter) {
const parameter = <ParameterDeclaration>local.valueDeclaration;
if (local.valueDeclaration && getRootDeclaration(local.valueDeclaration).kind === SyntaxKind.Parameter) {
const parameter = <ParameterDeclaration>getRootDeclaration(local.valueDeclaration);
if (compilerOptions.noUnusedParameters &&
!isParameterPropertyDeclaration(parameter) &&
!parameterIsThisKeyword(parameter) &&
!parameterNameStartsWithUnderscore(parameter)) {
!parameterNameStartsWithUnderscore(local.valueDeclaration.name)) {
error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name);
}
}
@@ -15861,8 +15861,8 @@ namespace ts {
error(node, Diagnostics._0_is_declared_but_never_used, name);
}
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
return parameter.name && isIdentifierThatStartsWithUnderScore(parameter.name);
function parameterNameStartsWithUnderscore(parameterName: DeclarationName) {
return parameterName && isIdentifierThatStartsWithUnderScore(parameterName);
}
function isIdentifierThatStartsWithUnderScore(node: Node) {

View File

@@ -0,0 +1,15 @@
tests/cases/compiler/unusedDestructuringParameters.ts(1,13): error TS6133: 'a' is declared but never used.
tests/cases/compiler/unusedDestructuringParameters.ts(3,14): error TS6133: 'a' is declared but never used.
==== tests/cases/compiler/unusedDestructuringParameters.ts (2 errors) ====
const f = ([a]) => { };
~
!!! error TS6133: 'a' is declared but never used.
f([1]);
const f2 = ({a}) => { };
~
!!! error TS6133: 'a' is declared but never used.
f2({ a: 10 });
const f3 = ([_]) => { };
f3([10]);

View File

@@ -2,7 +2,9 @@
const f = ([a]) => { };
f([1]);
const f2 = ({a}) => { };
f2({ a: 10 });
f2({ a: 10 });
const f3 = ([_]) => { };
f3([10]);
//// [unusedDestructuringParameters.js]
var f = function (_a) {
@@ -13,3 +15,7 @@ var f2 = function (_a) {
var a = _a.a;
};
f2({ a: 10 });
var f3 = function (_a) {
var _ = _a[0];
};
f3([10]);

View File

@@ -1,16 +0,0 @@
=== tests/cases/compiler/unusedDestructuringParameters.ts ===
const f = ([a]) => { };
>f : Symbol(f, Decl(unusedDestructuringParameters.ts, 0, 5))
>a : Symbol(a, Decl(unusedDestructuringParameters.ts, 0, 12))
f([1]);
>f : Symbol(f, Decl(unusedDestructuringParameters.ts, 0, 5))
const f2 = ({a}) => { };
>f2 : Symbol(f2, Decl(unusedDestructuringParameters.ts, 2, 5))
>a : Symbol(a, Decl(unusedDestructuringParameters.ts, 2, 13))
f2({ a: 10 });
>f2 : Symbol(f2, Decl(unusedDestructuringParameters.ts, 2, 5))
>a : Symbol(a, Decl(unusedDestructuringParameters.ts, 3, 4))

View File

@@ -1,24 +0,0 @@
=== tests/cases/compiler/unusedDestructuringParameters.ts ===
const f = ([a]) => { };
>f : ([a]: [any]) => void
>([a]) => { } : ([a]: [any]) => void
>a : any
f([1]);
>f([1]) : void
>f : ([a]: [any]) => void
>[1] : [number]
>1 : 1
const f2 = ({a}) => { };
>f2 : ({a}: { a: any; }) => void
>({a}) => { } : ({a}: { a: any; }) => void
>a : any
f2({ a: 10 });
>f2({ a: 10 }) : void
>f2 : ({a}: { a: any; }) => void
>{ a: 10 } : { a: number; }
>a : number
>10 : 10

View File

@@ -2,15 +2,11 @@ tests/cases/compiler/unusedParametersWithUnderscore.ts(2,12): error TS6133: 'a'
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,19): error TS6133: 'c' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,27): error TS6133: 'd' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,29): error TS6133: 'e___' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(6,14): error TS6133: '_a' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(6,18): error TS6133: '___b' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(9,14): error TS6133: '_a' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(9,19): error TS6133: '___b' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(12,16): error TS6133: 'arg' is declared but never used.
tests/cases/compiler/unusedParametersWithUnderscore.ts(18,13): error TS6133: 'arg' is declared but never used.
==== tests/cases/compiler/unusedParametersWithUnderscore.ts (10 errors) ====
==== tests/cases/compiler/unusedParametersWithUnderscore.ts (6 errors) ====
function f(a, _b, c, ___, d,e___, _f) {
~
@@ -25,17 +21,9 @@ tests/cases/compiler/unusedParametersWithUnderscore.ts(18,13): error TS6133: 'ar
function f2({_a, __b}) {
~~
!!! error TS6133: '_a' is declared but never used.
~~~
!!! error TS6133: '___b' is declared but never used.
}
function f3([_a, ,__b]) {
~~
!!! error TS6133: '_a' is declared but never used.
~~~
!!! error TS6133: '___b' is declared but never used.
}
function f4(...arg) {

View File

@@ -2,4 +2,6 @@
const f = ([a]) => { };
f([1]);
const f2 = ({a}) => { };
f2({ a: 10 });
f2({ a: 10 });
const f3 = ([_]) => { };
f3([10]);