Skip to content

Commit 23b3eb6

Browse files
authored
fix(41621): fixUnusedIdentifier - allow deleting prefix/postfix unary operators (#41624)
1 parent 1bd8e38 commit 23b3eb6

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ namespace ts.codefix {
225225
if (isIdentifier(token)) {
226226
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
227227
if (isPropertyAccessExpression(ref.parent) && ref.parent.name === ref) ref = ref.parent;
228-
if (!isFixAll && isBinaryExpression(ref.parent) && isExpressionStatement(ref.parent.parent) && ref.parent.left === ref) {
228+
if (!isFixAll && mayDeleteExpression(ref)) {
229229
changes.delete(sourceFile, ref.parent.parent);
230230
}
231231
});
@@ -332,4 +332,9 @@ namespace ts.codefix {
332332
parameters.slice(index + 1).every(p => isIdentifier(p.name) && !p.symbol.isReferenced) :
333333
index === parameters.length - 1;
334334
}
335+
336+
function mayDeleteExpression(node: Node) {
337+
return ((isBinaryExpression(node.parent) && node.parent.left === node) ||
338+
((isPostfixUnaryExpression(node.parent) || isPrefixUnaryExpression(node.parent)) && node.parent.operand === node)) && isExpressionStatement(node.parent.parent);
339+
}
335340
}
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)