@@ -18,16 +18,14 @@ namespace ts.codefix {
18
18
19
19
switch ( token . kind ) {
20
20
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 ) ;
24
22
25
23
case SyntaxKind . PropertyDeclaration :
26
24
case SyntaxKind . NamespaceImport :
27
- return deleteNode ( token . parent ) ;
25
+ return [ deleteNode ( token . parent ) ] ;
28
26
29
27
default :
30
- return deleteDefault ( ) ;
28
+ return [ deleteDefault ( ) ] ;
31
29
}
32
30
33
31
function deleteDefault ( ) {
@@ -43,7 +41,6 @@ namespace ts.codefix {
43
41
}
44
42
45
43
function prefixIdentifierWithUnderscore ( identifier : Identifier ) : CodeAction {
46
- // TODO: make sure this work with prefixing trivia.
47
44
const startPosition = identifier . getStart ( sourceFile , /*includeJsDocComment*/ false ) ;
48
45
return {
49
46
description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Prefix_0_with_an_underscore ) , { 0 : token . getText ( ) } ) ,
@@ -57,11 +54,11 @@ namespace ts.codefix {
57
54
} ;
58
55
}
59
56
60
- function deleteIdentifier ( identifier : Identifier ) : CodeAction [ ] | undefined {
57
+ function deleteIdentifierOrPrefixWithUnderscore ( identifier : Identifier ) : CodeAction [ ] | undefined {
61
58
const parent = identifier . parent ;
62
59
switch ( parent . kind ) {
63
60
case ts . SyntaxKind . VariableDeclaration :
64
- return deleteVariableDeclaration ( < ts . VariableDeclaration > parent ) ;
61
+ return deleteVariableDeclarationOrPrefixWithUnderscore ( identifier , < ts . VariableDeclaration > parent ) ;
65
62
66
63
case SyntaxKind . TypeParameter :
67
64
const typeParameters = ( < DeclarationWithTypeParameters > parent . parent ) . typeParameters ;
@@ -71,33 +68,32 @@ namespace ts.codefix {
71
68
Debug . assert ( previousToken . kind === SyntaxKind . LessThanToken ) ;
72
69
Debug . assert ( nextToken . kind === SyntaxKind . GreaterThanToken ) ;
73
70
74
- return deleteNodeRange ( previousToken , nextToken ) ;
71
+ return [ deleteNodeRange ( previousToken , nextToken ) ] ;
75
72
}
76
73
else {
77
- return deleteNodeInList ( parent ) ;
74
+ return [ deleteNodeInList ( parent ) ] ;
78
75
}
79
76
80
77
case ts . SyntaxKind . Parameter :
81
78
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 ) ] ;
85
81
86
82
// handle case where 'import a = A;'
87
83
case SyntaxKind . ImportEqualsDeclaration :
88
84
const importEquals = getAncestor ( identifier , SyntaxKind . ImportEqualsDeclaration ) ;
89
- return deleteNode ( importEquals ) ;
85
+ return [ deleteNode ( importEquals ) ] ;
90
86
91
87
case SyntaxKind . ImportSpecifier :
92
88
const namedImports = < NamedImports > parent . parent ;
93
89
if ( namedImports . elements . length === 1 ) {
94
90
// Only 1 import and it is unused. So the entire declaration should be removed.
95
91
const importSpec = getAncestor ( identifier , SyntaxKind . ImportDeclaration ) ;
96
- return deleteNode ( importSpec ) ;
92
+ return [ deleteNode ( importSpec ) ] ;
97
93
}
98
94
else {
99
95
// delete import specifier
100
- return deleteNodeInList ( parent ) ;
96
+ return [ deleteNodeInList ( parent ) ] ;
101
97
}
102
98
103
99
// handle case where "import d, * as ns from './file'"
@@ -106,72 +102,70 @@ namespace ts.codefix {
106
102
const importClause = < ImportClause > parent ;
107
103
if ( ! importClause . namedBindings ) { // |import d from './file'| or |import * as ns from './file'|
108
104
const importDecl = getAncestor ( importClause , SyntaxKind . ImportDeclaration ) ;
109
- return deleteNode ( importDecl ) ;
105
+ return [ deleteNode ( importDecl ) ] ;
110
106
}
111
107
else {
112
108
// import |d,| * as ns from './file'
113
109
const start = importClause . name . getStart ( sourceFile ) ;
114
110
const nextToken = getTokenAtPosition ( sourceFile , importClause . name . end , /*includeJsDocComment*/ false ) ;
115
111
if ( nextToken && nextToken . kind === SyntaxKind . CommaToken ) {
116
112
// 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 ) } ) ] ;
118
114
}
119
115
else {
120
- return deleteNode ( importClause . name ) ;
116
+ return [ deleteNode ( importClause . name ) ] ;
121
117
}
122
118
}
123
119
124
120
case SyntaxKind . NamespaceImport :
125
121
const namespaceImport = < NamespaceImport > parent ;
126
122
if ( namespaceImport . name === identifier && ! ( < ImportClause > namespaceImport . parent ) . name ) {
127
123
const importDecl = getAncestor ( namespaceImport , SyntaxKind . ImportDeclaration ) ;
128
- return deleteNode ( importDecl ) ;
124
+ return [ deleteNode ( importDecl ) ] ;
129
125
}
130
126
else {
131
127
const previousToken = getTokenAtPosition ( sourceFile , namespaceImport . pos - 1 , /*includeJsDocComment*/ false ) ;
132
128
if ( previousToken && previousToken . kind === SyntaxKind . CommaToken ) {
133
129
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 } ) ] ;
135
131
}
136
- return deleteRange ( namespaceImport ) ;
132
+ return [ deleteRange ( namespaceImport ) ] ;
137
133
}
138
134
139
135
default :
140
- return deleteDefault ( ) ;
136
+ return [ deleteDefault ( ) ] ;
141
137
}
142
138
}
143
139
144
140
// token.parent is a variableDeclaration
145
- function deleteVariableDeclaration ( varDecl : ts . VariableDeclaration ) : CodeAction [ ] | undefined {
141
+ function deleteVariableDeclarationOrPrefixWithUnderscore ( identifier : Identifier , varDecl : ts . VariableDeclaration ) : CodeAction [ ] | undefined {
146
142
switch ( varDecl . parent . parent . kind ) {
147
143
case SyntaxKind . ForStatement :
148
144
const forStatement = < ForStatement > varDecl . parent . parent ;
149
145
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 ) ] ;
156
147
157
148
case SyntaxKind . ForOfStatement :
158
149
const forOfStatement = < ForOfStatement > varDecl . parent . parent ;
159
150
Debug . assert ( forOfStatement . initializer . kind === SyntaxKind . VariableDeclarationList ) ;
160
151
const forOfInitializer = < VariableDeclarationList > forOfStatement . initializer ;
161
- return replaceNode ( forOfInitializer . declarations [ 0 ] , createObjectLiteral ( ) ) ;
152
+ return [
153
+ replaceNode ( forOfInitializer . declarations [ 0 ] , createObjectLiteral ( ) ) ,
154
+ prefixIdentifierWithUnderscore ( identifier )
155
+ ] ;
162
156
163
157
case SyntaxKind . ForInStatement :
164
158
// There is no valid fix in the case of:
165
159
// for .. in
166
- return undefined ;
160
+ return [ prefixIdentifierWithUnderscore ( identifier ) ] ;
167
161
168
162
default :
169
163
const variableStatement = < VariableStatement > varDecl . parent . parent ;
170
164
if ( variableStatement . declarationList . declarations . length === 1 ) {
171
- return deleteNode ( variableStatement ) ;
165
+ return [ deleteNode ( variableStatement ) ] ;
172
166
}
173
167
else {
174
- return deleteNodeInList ( varDecl ) ;
168
+ return [ deleteNodeInList ( varDecl ) ] ;
175
169
}
176
170
}
177
171
}
@@ -196,11 +190,11 @@ namespace ts.codefix {
196
190
return makeChange ( textChanges . ChangeTracker . fromCodeFixContext ( context ) . replaceNode ( sourceFile , n , newNode ) ) ;
197
191
}
198
192
199
- function makeChange ( changeTracker : textChanges . ChangeTracker ) {
200
- return [ {
193
+ function makeChange ( changeTracker : textChanges . ChangeTracker ) : CodeAction {
194
+ return {
201
195
description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Remove_declaration_for_Colon_0 ) , { 0 : token . getText ( ) } ) ,
202
196
changes : changeTracker . getChanges ( )
203
- } ] ;
197
+ } ;
204
198
}
205
199
}
206
200
} ) ;
0 commit comments