Skip to content

Commit b2ca04b

Browse files
author
Andy Hanson
committed
Handle BindingElement in fixUnusedIdentifier
1 parent 081322b commit b2ca04b

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ namespace ts.codefix {
171171
}
172172
break;
173173

174+
case SyntaxKind.BindingElement:
175+
changes.deleteNodeInList(sourceFile, parent);
176+
break;
177+
174178
// handle case where 'import a = A;'
175179
case SyntaxKind.ImportEqualsDeclaration:
176180
const importEquals = getAncestor(identifier, SyntaxKind.ImportEqualsDeclaration);

src/services/formatting/smartIndenter.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,10 @@ namespace ts.formatting {
327327

328328
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> {
329329
if (node.parent) {
330+
const { end } = node;
330331
switch (node.parent.kind) {
331332
case SyntaxKind.TypeReference:
332-
return getListIfStartEndIsInListRange((<TypeReferenceNode>node.parent).typeArguments, node.getStart(sourceFile), node.getEnd());
333+
return getListIfStartEndIsInListRange((<TypeReferenceNode>node.parent).typeArguments, node.getStart(sourceFile), end);
333334
case SyntaxKind.ObjectLiteralExpression:
334335
return (<ObjectLiteralExpression>node.parent).properties;
335336
case SyntaxKind.ArrayLiteralExpression:
@@ -344,22 +345,25 @@ namespace ts.formatting {
344345
case SyntaxKind.ConstructorType:
345346
case SyntaxKind.ConstructSignature: {
346347
const start = node.getStart(sourceFile);
347-
return getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).typeParameters, start, node.getEnd()) ||
348-
getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).parameters, start, node.getEnd());
348+
return getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).typeParameters, start, end) ||
349+
getListIfStartEndIsInListRange((<SignatureDeclaration>node.parent).parameters, start, end);
349350
}
350351
case SyntaxKind.ClassDeclaration:
351-
return getListIfStartEndIsInListRange((<ClassDeclaration>node.parent).typeParameters, node.getStart(sourceFile), node.getEnd());
352+
return getListIfStartEndIsInListRange((<ClassDeclaration>node.parent).typeParameters, node.getStart(sourceFile), end);
352353
case SyntaxKind.NewExpression:
353354
case SyntaxKind.CallExpression: {
354355
const start = node.getStart(sourceFile);
355-
return getListIfStartEndIsInListRange((<CallExpression>node.parent).typeArguments, start, node.getEnd()) ||
356-
getListIfStartEndIsInListRange((<CallExpression>node.parent).arguments, start, node.getEnd());
356+
return getListIfStartEndIsInListRange((<CallExpression>node.parent).typeArguments, start, end) ||
357+
getListIfStartEndIsInListRange((<CallExpression>node.parent).arguments, start, end);
357358
}
358359
case SyntaxKind.VariableDeclarationList:
359-
return getListIfStartEndIsInListRange((<VariableDeclarationList>node.parent).declarations, node.getStart(sourceFile), node.getEnd());
360+
return getListIfStartEndIsInListRange((<VariableDeclarationList>node.parent).declarations, node.getStart(sourceFile), end);
360361
case SyntaxKind.NamedImports:
361362
case SyntaxKind.NamedExports:
362-
return getListIfStartEndIsInListRange((<NamedImportsOrExports>node.parent).elements, node.getStart(sourceFile), node.getEnd());
363+
return getListIfStartEndIsInListRange((<NamedImportsOrExports>node.parent).elements, node.getStart(sourceFile), end);
364+
case SyntaxKind.ObjectBindingPattern:
365+
case SyntaxKind.ArrayBindingPattern:
366+
return getListIfStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node.parent).elements, node.getStart(sourceFile), end);
363367
}
364368
}
365369
return undefined;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
5+
////{
6+
//// const { x, y } = o;
7+
//// x;
8+
////}
9+
////{
10+
//// const { x, y } = o;
11+
//// y;
12+
////}
13+
14+
verify.codeFixAll({
15+
fixId: "unusedIdentifier_delete",
16+
fixAllDescription: "Delete all unused declarations",
17+
newFileContent:
18+
`{
19+
const { x } = o;
20+
x;
21+
}
22+
{
23+
const { y } = o;
24+
y;
25+
}`,
26+
});

0 commit comments

Comments
 (0)