Skip to content

Commit c1c670f

Browse files
committed
Report error on unused destructured parameters
Fixes #11795
1 parent 1e32b67 commit c1c670f

File tree

7 files changed

+31
-60
lines changed

7 files changed

+31
-60
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]);

tests/baselines/reference/unusedDestructuringParameters.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
const f = ([a]) => { };
33
f([1]);
44
const f2 = ({a}) => { };
5-
f2({ a: 10 });
5+
f2({ a: 10 });
6+
const f3 = ([_]) => { };
7+
f3([10]);
68

79
//// [unusedDestructuringParameters.js]
810
var f = function (_a) {
@@ -13,3 +15,7 @@ var f2 = function (_a) {
1315
var a = _a.a;
1416
};
1517
f2({ a: 10 });
18+
var f3 = function (_a) {
19+
var _ = _a[0];
20+
};
21+
f3([10]);

tests/baselines/reference/unusedDestructuringParameters.symbols

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/baselines/reference/unusedDestructuringParameters.types

Lines changed: 0 additions & 24 deletions
This file was deleted.

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) {

tests/cases/compiler/unusedDestructuringParameters.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
const f = ([a]) => { };
33
f([1]);
44
const f2 = ({a}) => { };
5-
f2({ a: 10 });
5+
f2({ a: 10 });
6+
const f3 = ([_]) => { };
7+
f3([10]);

0 commit comments

Comments
 (0)