mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
Merge pull request #11790 from Microsoft/allowUnderScoreForIn
Allow unused locals in for in or for of that start with _
This commit is contained in:
commit
23d325120e
@ -15836,15 +15836,31 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
else if (compilerOptions.noUnusedLocals) {
|
||||
forEach(local.declarations, d => error(d.name || d, Diagnostics._0_is_declared_but_never_used, local.name));
|
||||
forEach(local.declarations, d => errorUnusedLocal(d.name || d, local.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function errorUnusedLocal(node: Node, name: string) {
|
||||
if (isIdentifierThatStartsWithUnderScore(node)) {
|
||||
const declaration = getRootDeclaration(node.parent);
|
||||
if (declaration.kind === SyntaxKind.VariableDeclaration &&
|
||||
(declaration.parent.parent.kind === SyntaxKind.ForInStatement ||
|
||||
declaration.parent.parent.kind === SyntaxKind.ForOfStatement)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
error(node, Diagnostics._0_is_declared_but_never_used, name);
|
||||
}
|
||||
|
||||
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
|
||||
return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
|
||||
return parameter.name && isIdentifierThatStartsWithUnderScore(parameter.name);
|
||||
}
|
||||
|
||||
function isIdentifierThatStartsWithUnderScore(node: Node) {
|
||||
return node.kind === SyntaxKind.Identifier && (<Identifier>node).text.charCodeAt(0) === CharacterCodes._;
|
||||
}
|
||||
|
||||
function checkUnusedClassMembers(node: ClassDeclaration | ClassExpression): void {
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts(7,9): error TS6133: '_' is declared but never used.
|
||||
|
||||
|
||||
==== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts (1 errors) ====
|
||||
|
||||
for (const _ of []) { }
|
||||
|
||||
for (const _ in []) { }
|
||||
|
||||
namespace M {
|
||||
let _;
|
||||
~
|
||||
!!! error TS6133: '_' is declared but never used.
|
||||
for (const _ of []) { }
|
||||
|
||||
for (const _ in []) { }
|
||||
}
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
//// [unusedLocalsStartingWithUnderscore.ts]
|
||||
|
||||
for (const _ of []) { }
|
||||
|
||||
for (const _ in []) { }
|
||||
|
||||
namespace M {
|
||||
let _;
|
||||
for (const _ of []) { }
|
||||
|
||||
for (const _ in []) { }
|
||||
}
|
||||
|
||||
|
||||
//// [unusedLocalsStartingWithUnderscore.js]
|
||||
for (var _i = 0, _a = []; _i < _a.length; _i++) {
|
||||
var _ = _a[_i];
|
||||
}
|
||||
for (var _ in []) { }
|
||||
var M;
|
||||
(function (M) {
|
||||
var _;
|
||||
for (var _i = 0, _a = []; _i < _a.length; _i++) {
|
||||
var _1 = _a[_i];
|
||||
}
|
||||
for (var _2 in []) { }
|
||||
})(M || (M = {}));
|
||||
13
tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts
Normal file
13
tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts
Normal file
@ -0,0 +1,13 @@
|
||||
//@noUnusedLocals:true
|
||||
|
||||
for (const _ of []) { }
|
||||
|
||||
for (const _ in []) { }
|
||||
|
||||
namespace M {
|
||||
let _;
|
||||
for (const _ of []) { }
|
||||
|
||||
for (const _ in []) { }
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user