fix(41621): fixUnusedIdentifier - allow deleting prefix/postfix unary operators (#41624)

This commit is contained in:
Oleksandr T
2020-11-30 23:58:47 +02:00
committed by GitHub
parent 1bd8e388ae
commit 23b3eb685f
5 changed files with 62 additions and 1 deletions

View File

@@ -225,7 +225,7 @@ namespace ts.codefix {
if (isIdentifier(token)) {
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
if (isPropertyAccessExpression(ref.parent) && ref.parent.name === ref) ref = ref.parent;
if (!isFixAll && isBinaryExpression(ref.parent) && isExpressionStatement(ref.parent.parent) && ref.parent.left === ref) {
if (!isFixAll && mayDeleteExpression(ref)) {
changes.delete(sourceFile, ref.parent.parent);
}
});
@@ -332,4 +332,9 @@ namespace ts.codefix {
parameters.slice(index + 1).every(p => isIdentifier(p.name) && !p.symbol.isReferenced) :
index === parameters.length - 1;
}
function mayDeleteExpression(node: Node) {
return ((isBinaryExpression(node.parent) && node.parent.left === node) ||
((isPostfixUnaryExpression(node.parent) || isPrefixUnaryExpression(node.parent)) && node.parent.operand === node)) && isExpressionStatement(node.parent.parent);
}
}

View File

@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @noUnusedLocals: true
////function fn() {
//// let x = 1;
//// x++;
////}
verify.codeFix({
description: "Remove unused declaration for: 'x'",
newFileContent:
`function fn() {
}`
});

View File

@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @noUnusedLocals: true
////function fn() {
//// let x = 1;
//// x--;
////}
verify.codeFix({
description: "Remove unused declaration for: 'x'",
newFileContent:
`function fn() {
}`
});

View File

@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @noUnusedLocals: true
////function fn() {
//// let x = 1;
//// ++x;
////}
verify.codeFix({
description: "Remove unused declaration for: 'x'",
newFileContent:
`function fn() {
}`
});

View File

@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @noUnusedLocals: true
////function fn() {
//// let x = 1;
//// --x;
////}
verify.codeFix({
description: "Remove unused declaration for: 'x'",
newFileContent:
`function fn() {
}`
});