Skip to content

Commit b9885ee

Browse files
committed
Only add underscore in
* for-in * for-of * parameters
1 parent ce5f707 commit b9885ee

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ namespace ts.codefix {
1818

1919
switch (token.kind) {
2020
case ts.SyntaxKind.Identifier:
21-
let actions = deleteIdentifier(<Identifier>token);
22-
(actions || (actions = [])).push(prefixIdentifierWithUnderscore(<Identifier>token));
23-
return actions;
21+
return deleteIdentifierOrPrefixWithUnderscore(<Identifier>token);
2422

2523
case SyntaxKind.PropertyDeclaration:
2624
case SyntaxKind.NamespaceImport:
27-
return deleteNode(token.parent);
25+
return [deleteNode(token.parent)];
2826

2927
default:
30-
return deleteDefault();
28+
return [deleteDefault()];
3129
}
3230

3331
function deleteDefault() {
@@ -43,7 +41,6 @@ namespace ts.codefix {
4341
}
4442

4543
function prefixIdentifierWithUnderscore(identifier: Identifier): CodeAction {
46-
// TODO: make sure this work with prefixing trivia.
4744
const startPosition = identifier.getStart(sourceFile, /*includeJsDocComment*/ false);
4845
return {
4946
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Prefix_0_with_an_underscore), { 0: token.getText() }),
@@ -57,11 +54,11 @@ namespace ts.codefix {
5754
};
5855
}
5956

60-
function deleteIdentifier(identifier: Identifier): CodeAction[] | undefined {
57+
function deleteIdentifierOrPrefixWithUnderscore(identifier: Identifier): CodeAction[] | undefined {
6158
const parent = identifier.parent;
6259
switch (parent.kind) {
6360
case ts.SyntaxKind.VariableDeclaration:
64-
return deleteVariableDeclaration(<ts.VariableDeclaration>parent);
61+
return deleteVariableDeclarationOrPrefixWithUnderscore(identifier, <ts.VariableDeclaration>parent);
6562

6663
case SyntaxKind.TypeParameter:
6764
const typeParameters = (<DeclarationWithTypeParameters>parent.parent).typeParameters;
@@ -71,33 +68,32 @@ namespace ts.codefix {
7168
Debug.assert(previousToken.kind === SyntaxKind.LessThanToken);
7269
Debug.assert(nextToken.kind === SyntaxKind.GreaterThanToken);
7370

74-
return deleteNodeRange(previousToken, nextToken);
71+
return [deleteNodeRange(previousToken, nextToken)];
7572
}
7673
else {
77-
return deleteNodeInList(parent);
74+
return [deleteNodeInList(parent)];
7875
}
7976

8077
case ts.SyntaxKind.Parameter:
8178
const functionDeclaration = <FunctionDeclaration>parent.parent;
82-
return functionDeclaration.parameters.length === 1 ?
83-
deleteNode(parent) :
84-
deleteNodeInList(parent);
79+
return [functionDeclaration.parameters.length === 1 ? deleteNode(parent) : deleteNodeInList(parent),
80+
prefixIdentifierWithUnderscore(identifier)];
8581

8682
// handle case where 'import a = A;'
8783
case SyntaxKind.ImportEqualsDeclaration:
8884
const importEquals = getAncestor(identifier, SyntaxKind.ImportEqualsDeclaration);
89-
return deleteNode(importEquals);
85+
return [deleteNode(importEquals)];
9086

9187
case SyntaxKind.ImportSpecifier:
9288
const namedImports = <NamedImports>parent.parent;
9389
if (namedImports.elements.length === 1) {
9490
// Only 1 import and it is unused. So the entire declaration should be removed.
9591
const importSpec = getAncestor(identifier, SyntaxKind.ImportDeclaration);
96-
return deleteNode(importSpec);
92+
return [deleteNode(importSpec)];
9793
}
9894
else {
9995
// delete import specifier
100-
return deleteNodeInList(parent);
96+
return [deleteNodeInList(parent)];
10197
}
10298

10399
// handle case where "import d, * as ns from './file'"
@@ -106,72 +102,70 @@ namespace ts.codefix {
106102
const importClause = <ImportClause>parent;
107103
if (!importClause.namedBindings) { // |import d from './file'| or |import * as ns from './file'|
108104
const importDecl = getAncestor(importClause, SyntaxKind.ImportDeclaration);
109-
return deleteNode(importDecl);
105+
return [deleteNode(importDecl)];
110106
}
111107
else {
112108
// import |d,| * as ns from './file'
113109
const start = importClause.name.getStart(sourceFile);
114110
const nextToken = getTokenAtPosition(sourceFile, importClause.name.end, /*includeJsDocComment*/ false);
115111
if (nextToken && nextToken.kind === SyntaxKind.CommaToken) {
116112
// shift first non-whitespace position after comma to the start position of the node
117-
return deleteRange({ pos: start, end: skipTrivia(sourceFile.text, nextToken.end, /*stopAfterLineBreaks*/ false, /*stopAtComments*/ true) });
113+
return [deleteRange({ pos: start, end: skipTrivia(sourceFile.text, nextToken.end, /*stopAfterLineBreaks*/ false, /*stopAtComments*/ true) })];
118114
}
119115
else {
120-
return deleteNode(importClause.name);
116+
return [deleteNode(importClause.name)];
121117
}
122118
}
123119

124120
case SyntaxKind.NamespaceImport:
125121
const namespaceImport = <NamespaceImport>parent;
126122
if (namespaceImport.name === identifier && !(<ImportClause>namespaceImport.parent).name) {
127123
const importDecl = getAncestor(namespaceImport, SyntaxKind.ImportDeclaration);
128-
return deleteNode(importDecl);
124+
return [deleteNode(importDecl)];
129125
}
130126
else {
131127
const previousToken = getTokenAtPosition(sourceFile, namespaceImport.pos - 1, /*includeJsDocComment*/ false);
132128
if (previousToken && previousToken.kind === SyntaxKind.CommaToken) {
133129
const startPosition = textChanges.getAdjustedStartPosition(sourceFile, previousToken, {}, textChanges.Position.FullStart);
134-
return deleteRange({ pos: startPosition, end: namespaceImport.end });
130+
return [deleteRange({ pos: startPosition, end: namespaceImport.end })];
135131
}
136-
return deleteRange(namespaceImport);
132+
return [deleteRange(namespaceImport)];
137133
}
138134

139135
default:
140-
return deleteDefault();
136+
return [deleteDefault()];
141137
}
142138
}
143139

144140
// token.parent is a variableDeclaration
145-
function deleteVariableDeclaration(varDecl: ts.VariableDeclaration): CodeAction[] | undefined {
141+
function deleteVariableDeclarationOrPrefixWithUnderscore(identifier: Identifier, varDecl: ts.VariableDeclaration): CodeAction[] | undefined {
146142
switch (varDecl.parent.parent.kind) {
147143
case SyntaxKind.ForStatement:
148144
const forStatement = <ForStatement>varDecl.parent.parent;
149145
const forInitializer = <VariableDeclarationList>forStatement.initializer;
150-
if (forInitializer.declarations.length === 1) {
151-
return deleteNode(forInitializer);
152-
}
153-
else {
154-
return deleteNodeInList(varDecl);
155-
}
146+
return [forInitializer.declarations.length === 1 ? deleteNode(forInitializer) : deleteNodeInList(varDecl)];
156147

157148
case SyntaxKind.ForOfStatement:
158149
const forOfStatement = <ForOfStatement>varDecl.parent.parent;
159150
Debug.assert(forOfStatement.initializer.kind === SyntaxKind.VariableDeclarationList);
160151
const forOfInitializer = <VariableDeclarationList>forOfStatement.initializer;
161-
return replaceNode(forOfInitializer.declarations[0], createObjectLiteral());
152+
return [
153+
replaceNode(forOfInitializer.declarations[0], createObjectLiteral()),
154+
prefixIdentifierWithUnderscore(identifier)
155+
];
162156

163157
case SyntaxKind.ForInStatement:
164158
// There is no valid fix in the case of:
165159
// for .. in
166-
return undefined;
160+
return [prefixIdentifierWithUnderscore(identifier)];
167161

168162
default:
169163
const variableStatement = <VariableStatement>varDecl.parent.parent;
170164
if (variableStatement.declarationList.declarations.length === 1) {
171-
return deleteNode(variableStatement);
165+
return [deleteNode(variableStatement)];
172166
}
173167
else {
174-
return deleteNodeInList(varDecl);
168+
return [deleteNodeInList(varDecl)];
175169
}
176170
}
177171
}
@@ -196,11 +190,11 @@ namespace ts.codefix {
196190
return makeChange(textChanges.ChangeTracker.fromCodeFixContext(context).replaceNode(sourceFile, n, newNode));
197191
}
198192

199-
function makeChange(changeTracker: textChanges.ChangeTracker) {
200-
return [{
193+
function makeChange(changeTracker: textChanges.ChangeTracker): CodeAction {
194+
return {
201195
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Remove_declaration_for_Colon_0), { 0: token.getText() }),
202196
changes: changeTracker.getChanges()
203-
}];
197+
};
204198
}
205199
}
206200
});

0 commit comments

Comments
 (0)