Skip to content

Commit ccfd3bf

Browse files
author
Andy
authored
Handle BindingElement in fixUnusedIdentifier (microsoft#23819)
* Handle BindingElement in fixUnusedIdentifier * Add array destructure tests
1 parent 8ab5be9 commit ccfd3bf

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

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

174+
case SyntaxKind.BindingElement: {
175+
const pattern = (parent as BindingElement).parent;
176+
switch (pattern.kind) {
177+
case SyntaxKind.ArrayBindingPattern:
178+
changes.deleteNode(sourceFile, parent); // Don't delete ','
179+
break;
180+
case SyntaxKind.ObjectBindingPattern:
181+
changes.deleteNodeInList(sourceFile, parent);
182+
break;
183+
default:
184+
return Debug.assertNever(pattern);
185+
}
186+
break;
187+
}
188+
174189
// handle case where 'import a = A;'
175190
case SyntaxKind.ImportEqualsDeclaration:
176191
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
//// const { x, y, z } = o;
15+
//// y;
16+
////}
17+
////{
18+
//// const { x, y, z } = o;
19+
//// x; z;
20+
////}
21+
////{
22+
//// const [x, y] = o;
23+
//// x;
24+
////}
25+
////{
26+
//// const [x, y] = o;
27+
//// y;
28+
////}
29+
////{
30+
//// const [x, y, z] = o;
31+
//// y;
32+
////}
33+
////{
34+
//// const [x, y, z] = o;
35+
//// x; z;
36+
////}
37+
38+
39+
verify.codeFixAll({
40+
fixId: "unusedIdentifier_delete",
41+
fixAllDescription: "Delete all unused declarations",
42+
newFileContent:
43+
`{
44+
const { x } = o;
45+
x;
46+
}
47+
{
48+
const { y } = o;
49+
y;
50+
}
51+
{
52+
const { y } = o;
53+
y;
54+
}
55+
{
56+
const { x, z } = o;
57+
x; z;
58+
}
59+
{
60+
const [x,] = o;
61+
x;
62+
}
63+
{
64+
const [, y] = o;
65+
y;
66+
}
67+
{
68+
const [, y,] = o;
69+
y;
70+
}
71+
{
72+
const [x,, z] = o;
73+
x; z;
74+
}`,
75+
});

0 commit comments

Comments
 (0)