Skip to content

Commit 1884c89

Browse files
authored
Merge pull request #11818 from Microsoft/unusedDestructuredParameter
Report error on unused destructured parameters
2 parents e20f52b + c1c670f commit 1884c89

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15832,12 +15832,12 @@ namespace ts {
1583215832
for (const key in node.locals) {
1583315833
const local = node.locals[key];
1583415834
if (!local.isReferenced) {
15835-
if (local.valueDeclaration && local.valueDeclaration.kind === SyntaxKind.Parameter) {
15836-
const parameter = <ParameterDeclaration>local.valueDeclaration;
15835+
if (local.valueDeclaration && getRootDeclaration(local.valueDeclaration).kind === SyntaxKind.Parameter) {
15836+
const parameter = <ParameterDeclaration>getRootDeclaration(local.valueDeclaration);
1583715837
if (compilerOptions.noUnusedParameters &&
1583815838
!isParameterPropertyDeclaration(parameter) &&
1583915839
!parameterIsThisKeyword(parameter) &&
15840-
!parameterNameStartsWithUnderscore(parameter)) {
15840+
!parameterNameStartsWithUnderscore(local.valueDeclaration.name)) {
1584115841
error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name);
1584215842
}
1584315843
}
@@ -15861,8 +15861,8 @@ namespace ts {
1586115861
error(node, Diagnostics._0_is_declared_but_never_used, name);
1586215862
}
1586315863

15864-
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
15865-
return parameter.name && isIdentifierThatStartsWithUnderScore(parameter.name);
15864+
function parameterNameStartsWithUnderscore(parameterName: DeclarationName) {
15865+
return parameterName && isIdentifierThatStartsWithUnderScore(parameterName);
1586615866
}
1586715867

1586815868
function isIdentifierThatStartsWithUnderScore(node: Node) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/unusedDestructuringParameters.ts(1,13): error TS6133: 'a' is declared but never used.
2+
tests/cases/compiler/unusedDestructuringParameters.ts(3,14): error TS6133: 'a' is declared but never used.
3+
4+
5+
==== tests/cases/compiler/unusedDestructuringParameters.ts (2 errors) ====
6+
const f = ([a]) => { };
7+
~
8+
!!! error TS6133: 'a' is declared but never used.
9+
f([1]);
10+
const f2 = ({a}) => { };
11+
~
12+
!!! error TS6133: 'a' is declared but never used.
13+
f2({ a: 10 });
14+
const f3 = ([_]) => { };
15+
f3([10]);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [unusedDestructuringParameters.ts]
2+
const f = ([a]) => { };
3+
f([1]);
4+
const f2 = ({a}) => { };
5+
f2({ a: 10 });
6+
const f3 = ([_]) => { };
7+
f3([10]);
8+
9+
//// [unusedDestructuringParameters.js]
10+
var f = function (_a) {
11+
var a = _a[0];
12+
};
13+
f([1]);
14+
var f2 = function (_a) {
15+
var a = _a.a;
16+
};
17+
f2({ a: 10 });
18+
var f3 = function (_a) {
19+
var _ = _a[0];
20+
};
21+
f3([10]);

tests/baselines/reference/unusedParametersWithUnderscore.errors.txt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ tests/cases/compiler/unusedParametersWithUnderscore.ts(2,12): error TS6133: 'a'
22
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,19): error TS6133: 'c' is declared but never used.
33
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,27): error TS6133: 'd' is declared but never used.
44
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,29): error TS6133: 'e___' is declared but never used.
5-
tests/cases/compiler/unusedParametersWithUnderscore.ts(6,14): error TS6133: '_a' is declared but never used.
6-
tests/cases/compiler/unusedParametersWithUnderscore.ts(6,18): error TS6133: '___b' is declared but never used.
7-
tests/cases/compiler/unusedParametersWithUnderscore.ts(9,14): error TS6133: '_a' is declared but never used.
8-
tests/cases/compiler/unusedParametersWithUnderscore.ts(9,19): error TS6133: '___b' is declared but never used.
95
tests/cases/compiler/unusedParametersWithUnderscore.ts(12,16): error TS6133: 'arg' is declared but never used.
106
tests/cases/compiler/unusedParametersWithUnderscore.ts(18,13): error TS6133: 'arg' is declared but never used.
117

128

13-
==== tests/cases/compiler/unusedParametersWithUnderscore.ts (10 errors) ====
9+
==== tests/cases/compiler/unusedParametersWithUnderscore.ts (6 errors) ====
1410

1511
function f(a, _b, c, ___, d,e___, _f) {
1612
~
@@ -25,17 +21,9 @@ tests/cases/compiler/unusedParametersWithUnderscore.ts(18,13): error TS6133: 'ar
2521

2622

2723
function f2({_a, __b}) {
28-
~~
29-
!!! error TS6133: '_a' is declared but never used.
30-
~~~
31-
!!! error TS6133: '___b' is declared but never used.
3224
}
3325

3426
function f3([_a, ,__b]) {
35-
~~
36-
!!! error TS6133: '_a' is declared but never used.
37-
~~~
38-
!!! error TS6133: '___b' is declared but never used.
3927
}
4028

4129
function f4(...arg) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@noUnusedParameters: true
2+
const f = ([a]) => { };
3+
f([1]);
4+
const f2 = ({a}) => { };
5+
f2({ a: 10 });
6+
const f3 = ([_]) => { };
7+
f3([10]);

0 commit comments

Comments
 (0)