Skip to content

Commit 8f1cdc9

Browse files
authored
Merge pull request #20373 from amcasey/PropertyUnderscore
Don't offer to prepend an underscore to the name of an unused private property
2 parents 2ec2238 + d7da7d4 commit 8f1cdc9

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace ts.codefix {
1818

1919
switch (token.kind) {
2020
case ts.SyntaxKind.Identifier:
21-
return deleteIdentifierOrPrefixWithUnderscore(<Identifier>token);
21+
return deleteIdentifierOrPrefixWithUnderscore(<Identifier>token, context.errorCode);
2222

2323
case SyntaxKind.PropertyDeclaration:
2424
case SyntaxKind.NamespaceImport:
@@ -54,7 +54,7 @@ namespace ts.codefix {
5454
};
5555
}
5656

57-
function deleteIdentifierOrPrefixWithUnderscore(identifier: Identifier): CodeAction[] | undefined {
57+
function deleteIdentifierOrPrefixWithUnderscore(identifier: Identifier, errorCode: number): CodeAction[] | undefined {
5858
const parent = identifier.parent;
5959
switch (parent.kind) {
6060
case ts.SyntaxKind.VariableDeclaration:
@@ -76,8 +76,10 @@ namespace ts.codefix {
7676

7777
case ts.SyntaxKind.Parameter:
7878
const functionDeclaration = <FunctionDeclaration>parent.parent;
79-
return [functionDeclaration.parameters.length === 1 ? deleteNode(parent) : deleteNodeInList(parent),
80-
prefixIdentifierWithUnderscore(identifier)];
79+
const deleteAction = functionDeclaration.parameters.length === 1 ? deleteNode(parent) : deleteNodeInList(parent);
80+
return errorCode === Diagnostics.Property_0_is_declared_but_its_value_is_never_read.code
81+
? [deleteAction]
82+
: [deleteAction, prefixIdentifierWithUnderscore(identifier)];
8183

8284
// handle case where 'import a = A;'
8385
case SyntaxKind.ImportEqualsDeclaration:
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// <reference path='fourslash.ts' />
22

3-
// @noUnusedLocals: true
3+
// @noUnusedParameters: true
44
//// class C1 {
5-
//// [|constructor(private p1: string, public p2: boolean, public p3: any, p5) |] { p5; }
5+
//// [|constructor(p1: string, public p2: boolean, public p3: any, p5) |] { p5; }
66
//// }
77

88
verify.codeFix({
99
description: "Prefix 'p1' with an underscore.",
1010
index: 1,
11-
newRangeContent: "constructor(private _p1: string, public p2: boolean, public p3: any, p5)",
11+
newRangeContent: "constructor(_p1: string, public p2: boolean, public p3: any, p5)",
1212
});

0 commit comments

Comments
 (0)