Skip to content

Handle BindingElement in fixUnusedIdentifier #23819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/services/codefixes/fixUnusedIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true

////{
//// const { x, y } = o;
//// x;
////}
////{
//// const { x, y } = o;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be interesting to test the case where only one variable is declared and it is unused? Or is that covered by the other PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be covered by #23805.

//// 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;
}`,
});