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:
commit
8f1cdc9b0c
|
@ -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:
|
||||
|
|
|
@ -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)",
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue