Merge pull request #20373 from amcasey/PropertyUnderscore

Don't offer to prepend an underscore to the name of an unused private property
This commit is contained in:
Andrew Casey 2017-11-30 13:08:26 -08:00 committed by GitHub
commit 8f1cdc9b0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View file

@ -18,7 +18,7 @@ namespace ts.codefix {
switch (token.kind) {
case ts.SyntaxKind.Identifier:
return deleteIdentifierOrPrefixWithUnderscore(<Identifier>token);
return deleteIdentifierOrPrefixWithUnderscore(<Identifier>token, context.errorCode);
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.NamespaceImport:
@ -54,7 +54,7 @@ namespace ts.codefix {
};
}
function deleteIdentifierOrPrefixWithUnderscore(identifier: Identifier): CodeAction[] | undefined {
function deleteIdentifierOrPrefixWithUnderscore(identifier: Identifier, errorCode: number): CodeAction[] | undefined {
const parent = identifier.parent;
switch (parent.kind) {
case ts.SyntaxKind.VariableDeclaration:
@ -76,8 +76,10 @@ namespace ts.codefix {
case ts.SyntaxKind.Parameter:
const functionDeclaration = <FunctionDeclaration>parent.parent;
return [functionDeclaration.parameters.length === 1 ? deleteNode(parent) : deleteNodeInList(parent),
prefixIdentifierWithUnderscore(identifier)];
const deleteAction = functionDeclaration.parameters.length === 1 ? deleteNode(parent) : deleteNodeInList(parent);
return errorCode === Diagnostics.Property_0_is_declared_but_its_value_is_never_read.code
? [deleteAction]
: [deleteAction, prefixIdentifierWithUnderscore(identifier)];
// handle case where 'import a = A;'
case SyntaxKind.ImportEqualsDeclaration:

View file

@ -1,12 +1,12 @@
/// <reference path='fourslash.ts' />
// @noUnusedLocals: true
// @noUnusedParameters: true
//// class C1 {
//// [|constructor(private p1: string, public p2: boolean, public p3: any, p5) |] { p5; }
//// [|constructor(p1: string, public p2: boolean, public p3: any, p5) |] { p5; }
//// }
verify.codeFix({
description: "Prefix 'p1' with an underscore.",
index: 1,
newRangeContent: "constructor(private _p1: string, public p2: boolean, public p3: any, p5)",
newRangeContent: "constructor(_p1: string, public p2: boolean, public p3: any, p5)",
});