Skip to content

Commit 5de7ca2

Browse files
committed
Fix #9458: exclude parameters starting with underscore from unusedParamter checks
1 parent 54b4bef commit 5de7ca2

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

Diff for: src/compiler/checker.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -14554,7 +14554,10 @@ namespace ts {
1455414554
const local = node.locals[key];
1455514555
if (!local.isReferenced) {
1455614556
if (local.valueDeclaration && local.valueDeclaration.kind === SyntaxKind.Parameter) {
14557-
if (compilerOptions.noUnusedParameters && !isParameterPropertyDeclaration(<ParameterDeclaration>local.valueDeclaration)) {
14557+
const parameter = <ParameterDeclaration>local.valueDeclaration;
14558+
if (compilerOptions.noUnusedParameters &&
14559+
!isParameterPropertyDeclaration(parameter) &&
14560+
!parameterNameStartsWithUnderscore(parameter)) {
1455814561
error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name);
1455914562
}
1456014563
}
@@ -14567,6 +14570,10 @@ namespace ts {
1456714570
}
1456814571
}
1456914572

14573+
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
14574+
return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
14575+
}
14576+
1457014577
function checkUnusedClassMembers(node: ClassDeclaration | ClassExpression): void {
1457114578
if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) {
1457214579
if (node.members) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,12): error TS6133: 'a' is declared but never used.
2+
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,19): error TS6133: 'c' is declared but never used.
3+
tests/cases/compiler/unusedParametersWithUnderscore.ts(2,27): error TS6133: 'd' is declared but never used.
4+
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.
9+
tests/cases/compiler/unusedParametersWithUnderscore.ts(12,16): error TS6133: 'arg' is declared but never used.
10+
tests/cases/compiler/unusedParametersWithUnderscore.ts(18,13): error TS6133: 'arg' is declared but never used.
11+
12+
13+
==== tests/cases/compiler/unusedParametersWithUnderscore.ts (10 errors) ====
14+
15+
function f(a, _b, c, ___, d,e___, _f) {
16+
~
17+
!!! error TS6133: 'a' is declared but never used.
18+
~
19+
!!! error TS6133: 'c' is declared but never used.
20+
~
21+
!!! error TS6133: 'd' is declared but never used.
22+
~~~~
23+
!!! error TS6133: 'e___' is declared but never used.
24+
}
25+
26+
27+
function f2({_a, __b}) {
28+
~~
29+
!!! error TS6133: '_a' is declared but never used.
30+
~~~
31+
!!! error TS6133: '___b' is declared but never used.
32+
}
33+
34+
function f3([_a, ,__b]) {
35+
~~
36+
!!! error TS6133: '_a' is declared but never used.
37+
~~~
38+
!!! error TS6133: '___b' is declared but never used.
39+
}
40+
41+
function f4(...arg) {
42+
~~~
43+
!!! error TS6133: 'arg' is declared but never used.
44+
}
45+
46+
function f5(..._arg) {
47+
}
48+
49+
function f6(arg?, _arg?) {
50+
~~~
51+
!!! error TS6133: 'arg' is declared but never used.
52+
}
53+
54+
var f7 = _ => undefined;
55+
56+
var f8 = function (_) { };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [unusedParametersWithUnderscore.ts]
2+
3+
function f(a, _b, c, ___, d,e___, _f) {
4+
}
5+
6+
7+
function f2({_a, __b}) {
8+
}
9+
10+
function f3([_a, ,__b]) {
11+
}
12+
13+
function f4(...arg) {
14+
}
15+
16+
function f5(..._arg) {
17+
}
18+
19+
function f6(arg?, _arg?) {
20+
}
21+
22+
var f7 = _ => undefined;
23+
24+
var f8 = function (_) { };
25+
26+
//// [unusedParametersWithUnderscore.js]
27+
function f(a, _b, c, ___, d, e___, _f) {
28+
}
29+
function f2(_c) {
30+
var _a = _c._a, __b = _c.__b;
31+
}
32+
function f3(_c) {
33+
var _a = _c[0], __b = _c[2];
34+
}
35+
function f4() {
36+
var arg = [];
37+
for (var _i = 0; _i < arguments.length; _i++) {
38+
arg[_i - 0] = arguments[_i];
39+
}
40+
}
41+
function f5() {
42+
var _arg = [];
43+
for (var _i = 0; _i < arguments.length; _i++) {
44+
_arg[_i - 0] = arguments[_i];
45+
}
46+
}
47+
function f6(arg, _arg) {
48+
}
49+
var f7 = function (_) { return undefined; };
50+
var f8 = function (_) { };
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@noUnusedLocals:true
2+
//@noUnusedParameters:true
3+
4+
function f(a, _b, c, ___, d,e___, _f) {
5+
}
6+
7+
8+
function f2({_a, __b}) {
9+
}
10+
11+
function f3([_a, ,__b]) {
12+
}
13+
14+
function f4(...arg) {
15+
}
16+
17+
function f5(..._arg) {
18+
}
19+
20+
function f6(arg?, _arg?) {
21+
}
22+
23+
var f7 = _ => undefined;
24+
25+
var f8 = function (_) { };

0 commit comments

Comments
 (0)