3
3
CancellationToken ,
4
4
cast ,
5
5
CodeFixAction ,
6
+ CodeFixContext ,
6
7
Debug ,
7
8
DiagnosticAndArguments ,
8
9
DiagnosticMessage ,
@@ -14,12 +15,15 @@ import {
14
15
forEach ,
15
16
FunctionLikeDeclaration ,
16
17
getJSDocParameterTags ,
18
+ getNewLineOrDefaultFromHost ,
19
+ getPrecedingNonSpaceCharacterPosition ,
17
20
getTokenAtPosition ,
18
21
Identifier ,
19
22
ImportDeclaration ,
20
23
isArrayBindingPattern ,
21
24
isBinaryExpression ,
22
25
isCallExpression ,
26
+ isCallLikeExpression ,
23
27
isComputedPropertyName ,
24
28
isDeclarationWithTypeParameterChildren ,
25
29
isExpressionStatement ,
@@ -37,11 +41,14 @@ import {
37
41
isPrefixUnaryExpression ,
38
42
isPropertyAccessExpression ,
39
43
isSuperKeyword ,
44
+ isVariableDeclaration ,
40
45
isVariableDeclarationList ,
46
+ length ,
41
47
map ,
42
48
Node ,
43
49
ObjectBindingPattern ,
44
50
ParameterDeclaration ,
51
+ probablyUsesSemicolons ,
45
52
Program ,
46
53
showModuleSpecifier ,
47
54
SourceFile ,
@@ -114,7 +121,7 @@ registerCodeFix({
114
121
}
115
122
return [
116
123
createDeleteFix ( textChanges . ChangeTracker . with ( context , t =>
117
- t . delete ( sourceFile , token . parent . parent ) ) , Diagnostics . Remove_unused_destructuring_declaration )
124
+ deleteDestructuring ( context , t , sourceFile , token . parent as ObjectBindingPattern | ArrayBindingPattern ) ) , Diagnostics . Remove_unused_destructuring_declaration ) ,
118
125
] ;
119
126
}
120
127
@@ -243,6 +250,27 @@ function deleteDestructuringElements(changes: textChanges.ChangeTracker, sourceF
243
250
forEach ( node . elements , n => changes . delete ( sourceFile , n ) ) ;
244
251
}
245
252
253
+ function deleteDestructuring ( context : CodeFixContext , changes : textChanges . ChangeTracker , sourceFile : SourceFile , { parent } : ObjectBindingPattern | ArrayBindingPattern ) {
254
+ if ( isVariableDeclaration ( parent ) && parent . initializer && isCallLikeExpression ( parent . initializer ) ) {
255
+ if ( isVariableDeclarationList ( parent . parent ) && length ( parent . parent . declarations ) > 1 ) {
256
+ const varStatement = parent . parent . parent ;
257
+ const pos = varStatement . getStart ( sourceFile ) ;
258
+ const end = varStatement . end ;
259
+ changes . delete ( sourceFile , parent ) ;
260
+ changes . insertNodeAt ( sourceFile , end , parent . initializer , {
261
+ prefix : getNewLineOrDefaultFromHost ( context . host , context . formatContext . options ) + sourceFile . text . slice ( getPrecedingNonSpaceCharacterPosition ( sourceFile . text , pos - 1 ) , pos ) ,
262
+ suffix : probablyUsesSemicolons ( sourceFile ) ? ";" : "" ,
263
+ } ) ;
264
+ }
265
+ else {
266
+ changes . replaceNode ( sourceFile , parent . parent , parent . initializer ) ;
267
+ }
268
+ }
269
+ else {
270
+ changes . delete ( sourceFile , parent ) ;
271
+ }
272
+ }
273
+
246
274
function tryPrefixDeclaration ( changes : textChanges . ChangeTracker , errorCode : number , sourceFile : SourceFile , token : Node ) : void {
247
275
// Don't offer to prefix a property.
248
276
if ( errorCode === Diagnostics . Property_0_is_declared_but_its_value_is_never_read . code ) return ;
0 commit comments