Handle BindingElement in fixUnusedIdentifier (#23819)

* Handle BindingElement in fixUnusedIdentifier

* Add array destructure tests
This commit is contained in:
Andy
2018-05-08 11:23:01 -07:00
committed by GitHub
parent 8ab5be9a86
commit ccfd3bf603
3 changed files with 102 additions and 8 deletions

View File

@@ -171,6 +171,21 @@ namespace ts.codefix {
}
break;
case SyntaxKind.BindingElement: {
const pattern = (parent as BindingElement).parent;
switch (pattern.kind) {
case SyntaxKind.ArrayBindingPattern:
changes.deleteNode(sourceFile, parent); // Don't delete ','
break;
case SyntaxKind.ObjectBindingPattern:
changes.deleteNodeInList(sourceFile, parent);
break;
default:
return Debug.assertNever(pattern);
}
break;
}
// handle case where 'import a = A;'
case SyntaxKind.ImportEqualsDeclaration:
const importEquals = getAncestor(identifier, SyntaxKind.ImportEqualsDeclaration);

View File

@@ -327,9 +327,10 @@ namespace ts.formatting {
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> {
if (node.parent) {
const { end } = node;
switch (node.parent.kind) {
case SyntaxKind.TypeReference:
return getListIfStartEndIsInListRange((<TypeReferenceNode>node.parent).typeArguments, node.getStart(sourceFile), node.getEnd());
return getListIfStartEndIsInListRange((<TypeReferenceNode>node.parent).typeArguments, node.getStart(sourceFile), end);
case SyntaxKind.ObjectLiteralExpression:
return (<ObjectLiteralExpression>node.parent).properties;
case SyntaxKind.ArrayLiteralExpression:
@@ -344,22 +345,25 @@ namespace ts.formatting {
case SyntaxKind.ConstructorType:
case SyntaxKind.ConstructSignature: {
const start = node.getStart(sourceFile);
return getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).typeParameters, start, node.getEnd()) ||
getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).parameters, start, node.getEnd());
return getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).typeParameters, start, end) ||
getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).parameters, start, end);
}
case SyntaxKind.ClassDeclaration:
return getListIfStartEndIsInListRange((<ClassDeclaration>node.parent).typeParameters, node.getStart(sourceFile), node.getEnd());
return getListIfStartEndIsInListRange((<ClassDeclaration>node.parent).typeParameters, node.getStart(sourceFile), end);
case SyntaxKind.NewExpression:
case SyntaxKind.CallExpression: {
const start = node.getStart(sourceFile);
return getListIfStartEndIsInListRange((<CallExpression>node.parent).typeArguments, start, node.getEnd()) ||
getListIfStartEndIsInListRange((<CallExpression>node.parent).arguments, start, node.getEnd());
return getListIfStartEndIsInListRange((<CallExpression>node.parent).typeArguments, start, end) ||
getListIfStartEndIsInListRange((<CallExpression>node.parent).arguments, start, end);
}
case SyntaxKind.VariableDeclarationList:
return getListIfStartEndIsInListRange((<VariableDeclarationList>node.parent).declarations, node.getStart(sourceFile), node.getEnd());
return getListIfStartEndIsInListRange((<VariableDeclarationList>node.parent).declarations, node.getStart(sourceFile), end);
case SyntaxKind.NamedImports:
case SyntaxKind.NamedExports:
return getListIfStartEndIsInListRange((<NamedImportsOrExports>node.parent).elements, node.getStart(sourceFile), node.getEnd());
return getListIfStartEndIsInListRange((<NamedImportsOrExports>node.parent).elements, node.getStart(sourceFile), end);
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.ArrayBindingPattern:
return getListIfStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node.parent).elements, node.getStart(sourceFile), end);
}
}
return undefined;

View File

@@ -0,0 +1,75 @@
/// <reference path='fourslash.ts' />
// @noUnusedLocals: true
////{
//// const { x, y } = o;
//// x;
////}
////{
//// const { x, y } = o;
//// y;
////}
////{
//// const { x, y, z } = o;
//// y;
////}
////{
//// const { x, y, z } = o;
//// x; z;
////}
////{
//// const [x, y] = o;
//// x;
////}
////{
//// const [x, y] = o;
//// y;
////}
////{
//// const [x, y, z] = o;
//// y;
////}
////{
//// const [x, y, z] = o;
//// x; z;
////}
verify.codeFixAll({
fixId: "unusedIdentifier_delete",
fixAllDescription: "Delete all unused declarations",
newFileContent:
`{
const { x } = o;
x;
}
{
const { y } = o;
y;
}
{
const { y } = o;
y;
}
{
const { x, z } = o;
x; z;
}
{
const [x,] = o;
x;
}
{
const [, y] = o;
y;
}
{
const [, y,] = o;
y;
}
{
const [x,, z] = o;
x; z;
}`,
});