Skip to content

Commit 03a6eeb

Browse files
author
Paul van Brenk
committed
Major refactoring after PR feedback
1 parent 28c08fd commit 03a6eeb

1 file changed

Lines changed: 92 additions & 111 deletions

File tree

src/services/codefixes/unusedIdentifierFixes.ts

Lines changed: 92 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,134 +10,106 @@ namespace ts.codefix {
1010
const start = context.span.start;
1111
const token = getTokenAtPosition(sourceFile, start);
1212

13-
if (token.kind === ts.SyntaxKind.Identifier) {
14-
if (token.parent.kind === ts.SyntaxKind.VariableDeclaration) {
15-
if (token.parent.parent.parent.kind === SyntaxKind.ForStatement) {
16-
const forStatement = <ForStatement>token.parent.parent.parent;
17-
const initializer = <VariableDeclarationList>forStatement.initializer;
18-
if (initializer.declarations.length === 1) {
19-
return createCodeFix("", initializer.pos, initializer.end - initializer.pos);
20-
}
21-
else {
22-
if (initializer.declarations[0] === token.parent) {
23-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1);
13+
switch (token.kind) {
14+
case ts.SyntaxKind.Identifier:
15+
switch (token.parent.kind) {
16+
case ts.SyntaxKind.VariableDeclaration:
17+
switch (token.parent.parent.parent.kind) {
18+
case SyntaxKind.ForStatement:
19+
const forStatement = <ForStatement>token.parent.parent.parent;
20+
const forInitializer = <VariableDeclarationList>forStatement.initializer;
21+
if (forInitializer.declarations.length === 1) {
22+
return createCodeFix("", forInitializer.pos, forInitializer.end - forInitializer.pos);
23+
}
24+
else {
25+
return removeSingleItem(forInitializer.declarations, token);
26+
}
27+
28+
case SyntaxKind.ForOfStatement:
29+
case SyntaxKind.ForInStatement:
30+
const forOfStatement = <ForOfStatement | ForInStatement>token.parent.parent.parent;
31+
if (forOfStatement.initializer.kind === SyntaxKind.VariableDeclarationList) {
32+
const forOfInitializer = <VariableDeclarationList>forOfStatement.initializer;
33+
return createCodeFix("{}", forOfInitializer.declarations[0].pos, forOfInitializer.declarations[0].end - forOfInitializer.declarations[0].pos);
34+
}
35+
break;
36+
37+
case SyntaxKind.CatchClause:
38+
const catchClause = <CatchClause>token.parent.parent;
39+
const parameter = catchClause.variableDeclaration.getChildren()[0];
40+
return createCodeFix("", parameter.pos, parameter.end - parameter.pos);
41+
42+
default:
43+
const variableStatement = <VariableStatement>token.parent.parent.parent;
44+
if (variableStatement.declarationList.declarations.length === 1) {
45+
return createCodeFix("", variableStatement.pos, variableStatement.end - variableStatement.pos);
46+
}
47+
else {
48+
const declarations = variableStatement.declarationList.declarations;
49+
return removeSingleItem(declarations, token);
50+
}
51+
}
52+
53+
case SyntaxKind.FunctionDeclaration:
54+
case SyntaxKind.ClassDeclaration:
55+
case SyntaxKind.InterfaceDeclaration:
56+
case SyntaxKind.MethodDeclaration:
57+
case SyntaxKind.ModuleDeclaration:
58+
case SyntaxKind.PropertyDeclaration:
59+
case SyntaxKind.ArrowFunction:
60+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
61+
62+
case SyntaxKind.TypeParameter:
63+
const typeParameters = (<DeclarationWithTypeParameters>token.parent.parent).typeParameters;
64+
if (typeParameters.length === 1) {
65+
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 2);
2466
}
2567
else {
26-
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1);
68+
return removeSingleItem(typeParameters, token);
2769
}
28-
}
29-
}
30-
else if (token.parent.parent.parent.kind === SyntaxKind.ForInStatement) {
31-
const forInStatement = <ForInStatement>token.parent.parent.parent;
32-
const initializer = <VariableDeclarationList>forInStatement.initializer;
33-
return createCodeFix("{}", initializer.declarations[0].pos, initializer.declarations[0].end - initializer.declarations[0].pos);
34-
}
35-
else if (token.parent.parent.parent.kind === SyntaxKind.ForOfStatement) {
36-
const forOfStatement = <ForOfStatement>token.parent.parent.parent;
37-
const initializer = <VariableDeclarationList>forOfStatement.initializer;
38-
return createCodeFix("{}", initializer.declarations[0].pos, initializer.declarations[0].end - initializer.declarations[0].pos);
39-
}
40-
else if (token.parent.parent.kind === SyntaxKind.CatchClause) {
41-
const catchClause = <CatchClause>token.parent.parent;
42-
const parameter = catchClause.variableDeclaration.getChildren()[0];
43-
return createCodeFix("", parameter.pos, parameter.end - parameter.pos);
44-
}
45-
else {
46-
const variableStatement = <VariableStatement>token.parent.parent.parent;
47-
if (variableStatement.declarationList.declarations.length === 1) {
48-
return createCodeFix("", variableStatement.pos, variableStatement.end - variableStatement.pos);
49-
}
50-
else {
51-
const declarations = variableStatement.declarationList.declarations;
52-
if (declarations[0].name === token) {
53-
return createCodeFix("", token.parent.pos + 1, token.parent.end - token.parent.pos);
70+
71+
case ts.SyntaxKind.Parameter:
72+
const functionDeclaration = <FunctionDeclaration>token.parent.parent;
73+
if (functionDeclaration.parameters.length === 1) {
74+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
5475
}
5576
else {
56-
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1);
77+
return removeSingleItem(functionDeclaration.parameters, token);
5778
}
58-
}
59-
}
60-
}
6179

62-
if (token.parent.kind === SyntaxKind.FunctionDeclaration ||
63-
token.parent.kind === SyntaxKind.ClassDeclaration ||
64-
token.parent.kind === SyntaxKind.InterfaceDeclaration ||
65-
token.parent.kind === SyntaxKind.MethodDeclaration ||
66-
token.parent.kind === SyntaxKind.ModuleDeclaration ||
67-
token.parent.kind === SyntaxKind.PropertyDeclaration ||
68-
token.parent.kind === SyntaxKind.ArrowFunction) {
69-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
70-
}
80+
case SyntaxKind.ImportSpecifier:
81+
const namedImports = <NamedImports>token.parent.parent;
82+
const elements = namedImports.elements;
83+
if (elements.length === 1) {
84+
// Only 1 import and it is unused. So the entire line could be removed.
85+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
86+
}
87+
else {
88+
return removeSingleItem(elements, token);
89+
}
7190

72-
if (token.parent.kind === SyntaxKind.TypeParameter) {
73-
const typeParameters = (<ClassDeclaration>token.parent.parent).typeParameters;
74-
if (typeParameters.length === 1) {
75-
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 2);
76-
}
77-
else {
78-
if (typeParameters[0] === token.parent) {
79-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1);
80-
}
81-
else {
82-
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1);
83-
}
84-
}
85-
}
91+
case SyntaxKind.ImportEqualsDeclaration:
92+
return createCodeFix("{}", token.pos, token.end - token.pos);
8693

87-
if (token.parent.kind === ts.SyntaxKind.Parameter) {
88-
const functionDeclaration = <FunctionDeclaration>token.parent.parent;
89-
if (functionDeclaration.parameters.length === 1) {
90-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
91-
}
92-
else {
93-
if (functionDeclaration.parameters[0] === token.parent) {
94-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1);
95-
}
96-
else {
97-
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1);
98-
}
94+
case SyntaxKind.EnumDeclaration:
95+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
9996
}
100-
}
10197

102-
if (token.parent.kind === SyntaxKind.ImportSpecifier) {
103-
const namedImports = <NamedImports>token.parent.parent;
104-
const elements = namedImports.elements;
105-
if (elements.length === 1) {
106-
// Only 1 import and it is unused. So the entire line could be removed.
107-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
98+
if (token.parent.parent.kind === SyntaxKind.ImportClause || token.parent.parent.kind === SyntaxKind.ImportDeclaration) {
99+
return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos);
108100
}
109-
else {
110-
if (elements[0] === token.parent) {
111-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1);
112-
}
113-
else {
114-
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1);
115-
}
116-
}
117-
}
118-
119-
if (token.parent.parent.kind === SyntaxKind.ImportClause || token.parent.parent.kind === SyntaxKind.ImportDeclaration) {
120-
return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos);
121-
}
122-
123-
if (token.parent.kind === SyntaxKind.ImportEqualsDeclaration) {
124-
return createCodeFix("{}", token.pos, token.end - token.pos);
125-
}
101+
break;
126102

127-
if (token.parent.kind === SyntaxKind.EnumDeclaration) {
103+
case SyntaxKind.PrivateKeyword:
104+
case SyntaxKind.PropertyDeclaration:
128105
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
129-
}
130-
}
131106

132-
if (token.kind === SyntaxKind.PrivateKeyword && token.parent.kind === SyntaxKind.PropertyDeclaration) {
133-
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
134-
}
135-
136-
if (token.kind === SyntaxKind.AsteriskToken && token.parent.kind === SyntaxKind.NamespaceImport) {
137-
return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos);
107+
case SyntaxKind.AsteriskToken:
108+
case SyntaxKind.NamespaceImport:
109+
return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos);
138110
}
139111

140-
return undefined;
112+
return [];
141113

142114
function createCodeFix(newText: string, start: number, length: number): CodeAction[] {
143115
return [{
@@ -148,6 +120,15 @@ namespace ts.codefix {
148120
}]
149121
}];
150122
}
123+
124+
function removeSingleItem<T extends Node>(elements: NodeArray<T>, token: T): CodeAction[] {
125+
if (elements[0] === token.parent) {
126+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1);
127+
}
128+
else {
129+
return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1);
130+
}
131+
}
151132
}
152133
});
153134
}

0 commit comments

Comments
 (0)