Merge pull request #11790 from Microsoft/allowUnderScoreForIn

Allow unused locals in for in or for of that start with _
This commit is contained in:
Sheetal Nandi
2016-10-24 10:42:20 -07:00
committed by GitHub
4 changed files with 76 additions and 2 deletions

View File

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