Merge pull request #11818 from Microsoft/unusedDestructuredParameter

Report error on unused destructured parameters
This commit is contained in:
Sheetal Nandi
2016-10-24 15:05:42 -07:00
committed by GitHub
5 changed files with 49 additions and 18 deletions
+5 -5
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) {
@@ -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]);
@@ -0,0 +1,21 @@
//// [unusedDestructuringParameters.ts]
const f = ([a]) => { };
f([1]);
const f2 = ({a}) => { };
f2({ a: 10 });
const f3 = ([_]) => { };
f3([10]);
//// [unusedDestructuringParameters.js]
var f = function (_a) {
var a = _a[0];
};
f([1]);
var f2 = function (_a) {
var a = _a.a;
};
f2({ a: 10 });
var f3 = function (_a) {
var _ = _a[0];
};
f3([10]);
@@ -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) {
@@ -0,0 +1,7 @@
//@noUnusedParameters: true
const f = ([a]) => { };
f([1]);
const f2 = ({a}) => { };
f2({ a: 10 });
const f3 = ([_]) => { };
f3([10]);