Skip to content

Commit 345012e

Browse files
author
Andy
authored
Don't add diagnostic on unused import starting with underscore (microsoft#24958)
* Don't add diagnostic on unused import starting with underscore * Fix lint
1 parent a56b272 commit 345012e

6 files changed

+54
-35
lines changed

src/compiler/checker.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -22895,7 +22895,11 @@ namespace ts {
2289522895
}
2289622896

2289722897
for (const declaration of local.declarations) {
22898-
if (isAmbientModule(declaration)) continue;
22898+
if (isAmbientModule(declaration) ||
22899+
(isVariableDeclaration(declaration) && isForInOrOfStatement(declaration.parent.parent) || isImportedDeclaration(declaration)) && isIdentifierThatStartsWithUnderScore(declaration.name!)) {
22900+
continue;
22901+
}
22902+
2289922903
if (isImportedDeclaration(declaration)) {
2290022904
addToGroup(unusedImports, importClauseFromImported(declaration), declaration, getNodeId);
2290122905
}
@@ -22907,9 +22911,7 @@ namespace ts {
2290722911
}
2290822912
}
2290922913
else if (isVariableDeclaration(declaration)) {
22910-
if (!isIdentifierThatStartsWithUnderScore(declaration.name) || !isForInOrOfStatement(declaration.parent.parent)) {
22911-
addToGroup(unusedVariables, declaration.parent, declaration, getNodeId);
22912-
}
22914+
addToGroup(unusedVariables, declaration.parent, declaration, getNodeId);
2291322915
}
2291422916
else {
2291522917
const parameter = local.valueDeclaration && tryGetRootParameterDeclaration(local.valueDeclaration);
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts(6,9): error TS6133: '_' is declared but its value is never read.
1+
/a.ts(7,11): error TS6133: '_ns' is declared but its value is never read.
2+
/a.ts(8,9): error TS6133: '_' is declared but its value is never read.
23

34

4-
==== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts (1 errors) ====
5+
==== /a.ts (2 errors) ====
6+
import * as _ from "./a";
7+
58
for (const _ of []) { }
69

710
for (const _ in []) { }
811

9-
namespace M {
12+
namespace _ns {
13+
~~~
14+
!!! error TS6133: '_ns' is declared but its value is never read.
1015
let _;
1116
~
1217
!!! error TS6133: '_' is declared but its value is never read.
1318
for (const _ of []) { }
1419

1520
for (const _ in []) { }
1621
}
17-
22+
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
//// [unusedLocalsStartingWithUnderscore.ts]
1+
//// [a.ts]
2+
import * as _ from "./a";
3+
24
for (const _ of []) { }
35

46
for (const _ in []) { }
57

6-
namespace M {
8+
namespace _ns {
79
let _;
810
for (const _ of []) { }
911

1012
for (const _ in []) { }
1113
}
12-
1314

14-
//// [unusedLocalsStartingWithUnderscore.js]
15+
16+
//// [a.js]
17+
"use strict";
18+
exports.__esModule = true;
1519
for (var _i = 0, _a = []; _i < _a.length; _i++) {
16-
var _ = _a[_i];
20+
var _1 = _a[_i];
1721
}
18-
for (var _ in []) { }
19-
var M;
20-
(function (M) {
22+
for (var _2 in []) { }
23+
var _ns;
24+
(function (_ns) {
2125
var _;
2226
for (var _i = 0, _a = []; _i < _a.length; _i++) {
23-
var _1 = _a[_i];
27+
var _3 = _a[_i];
2428
}
25-
for (var _2 in []) { }
26-
})(M || (M = {}));
29+
for (var _4 in []) { }
30+
})(_ns || (_ns = {}));
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
=== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts ===
1+
=== /a.ts ===
2+
import * as _ from "./a";
3+
>_ : Symbol(_, Decl(a.ts, 0, 6))
4+
25
for (const _ of []) { }
3-
>_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 0, 10))
6+
>_ : Symbol(_, Decl(a.ts, 2, 10))
47

58
for (const _ in []) { }
6-
>_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 2, 10))
9+
>_ : Symbol(_, Decl(a.ts, 4, 10))
710

8-
namespace M {
9-
>M : Symbol(M, Decl(unusedLocalsStartingWithUnderscore.ts, 2, 23))
11+
namespace _ns {
12+
>_ns : Symbol(_ns, Decl(a.ts, 4, 23))
1013

1114
let _;
12-
>_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 5, 7))
15+
>_ : Symbol(_, Decl(a.ts, 7, 7))
1316

1417
for (const _ of []) { }
15-
>_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 6, 14))
18+
>_ : Symbol(_, Decl(a.ts, 8, 14))
1619

1720
for (const _ in []) { }
18-
>_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 8, 14))
21+
>_ : Symbol(_, Decl(a.ts, 10, 14))
1922
}
20-
23+

tests/baselines/reference/unusedLocalsStartingWithUnderscore.types

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
=== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts ===
1+
=== /a.ts ===
2+
import * as _ from "./a";
3+
>_ : typeof _
4+
25
for (const _ of []) { }
36
>_ : any
47
>[] : undefined[]
@@ -7,8 +10,8 @@ for (const _ in []) { }
710
>_ : string
811
>[] : undefined[]
912

10-
namespace M {
11-
>M : typeof M
13+
namespace _ns {
14+
>_ns : typeof _ns
1215

1316
let _;
1417
>_ : any
@@ -21,4 +24,4 @@ namespace M {
2124
>_ : string
2225
>[] : undefined[]
2326
}
24-
27+

tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
//@noUnusedLocals:true
1+
// @noUnusedLocals:true
2+
3+
// @Filename: /a.ts
4+
import * as _ from "./a";
25

36
for (const _ of []) { }
47

58
for (const _ in []) { }
69

7-
namespace M {
10+
namespace _ns {
811
let _;
912
for (const _ of []) { }
1013

1114
for (const _ in []) { }
1215
}
13-

0 commit comments

Comments
 (0)