Skip to content

Commit 580516d

Browse files
committed
fix(41621): fixUnusedIdentifier - allow deleting prefix/postfix unary operators
1 parent f4157b4 commit 580516d

6 files changed

+78
-4
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,11 @@ namespace ts.codefix {
225225
function deleteAssignments(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier, checker: TypeChecker) {
226226
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
227227
if (isPropertyAccessExpression(ref.parent) && ref.parent.name === ref) ref = ref.parent;
228-
if (isBinaryExpression(ref.parent) && isExpressionStatement(ref.parent.parent) && ref.parent.left === ref) {
229-
changes.delete(sourceFile, ref.parent.parent);
230-
}
228+
if (mayDeleteExpression(ref)) changes.delete(sourceFile, ref.parent.parent);
231229
});
232230
}
233231

232+
234233
function tryDeleteDeclarationWorker(token: Node, changes: textChanges.ChangeTracker, sourceFile: SourceFile, checker: TypeChecker, sourceFiles: readonly SourceFile[], program: Program, cancellationToken: CancellationToken, isFixAll: boolean): void {
235234
const { parent } = token;
236235
if (isParameter(parent)) {
@@ -327,4 +326,9 @@ namespace ts.codefix {
327326
parameters.slice(index + 1).every(p => isIdentifier(p.name) && !p.symbol.isReferenced) :
328327
index === parameters.length - 1;
329328
}
329+
330+
function mayDeleteExpression(node: Node) {
331+
return ((isBinaryExpression(node.parent) && node.parent.left === node) ||
332+
((isPostfixUnaryExpression(node.parent) || isPrefixUnaryExpression(node.parent)) && node.parent.operand === node)) && isExpressionStatement(node.parent.parent);
333+
}
330334
}

tests/cases/fourslash/codeFixUnusedIdentifier_all_delete.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@
5656
////export const cls = class<T, U> { u: U; };
5757
////export class Ctu<T, U> {}
5858
////export type Length<T> = T extends ArrayLike<infer U> ? number : never; // Not affected, can't delete
59+
////
60+
////function f4() {
61+
//// let x = 1;
62+
//// x++;
63+
//// x--;
64+
//// let y = 1;
65+
//// ++x;
66+
//// --x;
67+
////}
68+
////f4();
5969

6070
verify.codeFixAll({
6171
fixId: "unusedIdentifier_delete",
@@ -109,5 +119,9 @@ export type First<T> = T;
109119
export interface ISecond<U> { u: U; }
110120
export const cls = class<U> { u: U; };
111121
export class Ctu {}
112-
export type Length<T> = T extends ArrayLike<infer U> ? number : never; // Not affected, can't delete`,
122+
export type Length<T> = T extends ArrayLike<infer U> ? number : never; // Not affected, can't delete
123+
124+
function f4() {
125+
}
126+
f4();`,
113127
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
////function fn() {
5+
//// let x = 1;
6+
//// x++;
7+
////}
8+
9+
verify.codeFix({
10+
description: "Remove unused declaration for: 'x'",
11+
newFileContent:
12+
`function fn() {
13+
}`
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
////function fn() {
5+
//// let x = 1;
6+
//// x--;
7+
////}
8+
9+
verify.codeFix({
10+
description: "Remove unused declaration for: 'x'",
11+
newFileContent:
12+
`function fn() {
13+
}`
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
////function fn() {
5+
//// let x = 1;
6+
//// ++x;
7+
////}
8+
9+
verify.codeFix({
10+
description: "Remove unused declaration for: 'x'",
11+
newFileContent:
12+
`function fn() {
13+
}`
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
////function fn() {
5+
//// let x = 1;
6+
//// --x;
7+
////}
8+
9+
verify.codeFix({
10+
description: "Remove unused declaration for: 'x'",
11+
newFileContent:
12+
`function fn() {
13+
}`
14+
});

0 commit comments

Comments
 (0)