@@ -34,18 +34,33 @@ namespace ts.codefix {
34
34
const changes = textChanges . ChangeTracker . with ( context , t => t . delete ( sourceFile , importDecl ) ) ;
35
35
return [ createDeleteFix ( changes , [ Diagnostics . Remove_import_from_0 , showModuleSpecifier ( importDecl ) ] ) ] ;
36
36
}
37
- const delDestructure = textChanges . ChangeTracker . with ( context , t =>
38
- tryDeleteFullDestructure ( token , t , sourceFile , checker , sourceFiles , /*isFixAll*/ false ) ) ;
39
- if ( delDestructure . length ) {
40
- return [ createDeleteFix ( delDestructure , Diagnostics . Remove_destructuring ) ] ;
37
+
38
+ if ( isObjectBindingPattern ( token . parent ) ) {
39
+ if ( isParameter ( token . parent . parent ) ) {
40
+ const elements = token . parent . elements ;
41
+ const diagnostic : [ DiagnosticMessage , string ] = [
42
+ elements . length > 1 ? Diagnostics . Remove_unused_declarations_for_Colon_0 : Diagnostics . Remove_unused_declaration_for_Colon_0 ,
43
+ map ( elements , e => e . getText ( sourceFile ) ) . join ( ", " )
44
+ ] ;
45
+ return [
46
+ createDeleteFix ( textChanges . ChangeTracker . with ( context , t =>
47
+ deleteDestructuringElements ( t , sourceFile , < ObjectBindingPattern > token . parent ) ) , diagnostic )
48
+ ] ;
49
+ }
50
+ return [
51
+ createDeleteFix ( textChanges . ChangeTracker . with ( context , t =>
52
+ t . delete ( sourceFile , token . parent . parent ) ) , Diagnostics . Remove_unused_destructuring_declaration )
53
+ ] ;
41
54
}
42
- const delVar = textChanges . ChangeTracker . with ( context , t => tryDeleteFullVariableStatement ( sourceFile , token , t ) ) ;
43
- if ( delVar . length ) {
44
- return [ createDeleteFix ( delVar , Diagnostics . Remove_variable_statement ) ] ;
55
+
56
+ if ( canDeleteEntireVariableStatement ( sourceFile , token ) ) {
57
+ return [
58
+ createDeleteFix ( textChanges . ChangeTracker . with ( context , t =>
59
+ deleteEntireVariableStatement ( t , sourceFile , < VariableDeclarationList > token . parent ) ) , Diagnostics . Remove_variable_statement )
60
+ ] ;
45
61
}
46
62
47
63
const result : CodeFixAction [ ] = [ ] ;
48
-
49
64
if ( token . kind === SyntaxKind . InferKeyword ) {
50
65
const changes = textChanges . ChangeTracker . with ( context , t => changeInferToUnknown ( t , sourceFile , token ) ) ;
51
66
const name = cast ( token . parent , isInferTypeNode ) . typeParameter . name . text ;
@@ -79,7 +94,9 @@ namespace ts.codefix {
79
94
tryPrefixDeclaration ( changes , diag . code , sourceFile , token ) ;
80
95
break ;
81
96
case fixIdDelete : {
82
- if ( token . kind === SyntaxKind . InferKeyword ) break ; // Can't delete
97
+ if ( token . kind === SyntaxKind . InferKeyword ) {
98
+ break ; // Can't delete
99
+ }
83
100
const importDecl = tryGetFullImport ( token ) ;
84
101
if ( importDecl ) {
85
102
changes . delete ( sourceFile , importDecl ) ;
@@ -90,8 +107,18 @@ namespace ts.codefix {
90
107
else if ( token . kind === SyntaxKind . LessThanToken ) {
91
108
deleteTypeParameters ( changes , sourceFile , token ) ;
92
109
}
93
- else if ( ! tryDeleteFullDestructure ( token , changes , sourceFile , checker , sourceFiles , /*isFixAll*/ true ) &&
94
- ! tryDeleteFullVariableStatement ( sourceFile , token , changes ) ) {
110
+ else if ( isObjectBindingPattern ( token . parent ) ) {
111
+ if ( isParameter ( token . parent . parent ) ) {
112
+ deleteDestructuringElements ( changes , sourceFile , token . parent ) ;
113
+ }
114
+ else {
115
+ changes . delete ( sourceFile , token . parent . parent ) ;
116
+ }
117
+ }
118
+ else if ( canDeleteEntireVariableStatement ( sourceFile , token ) ) {
119
+ deleteEntireVariableStatement ( changes , sourceFile , < VariableDeclarationList > token . parent ) ;
120
+ }
121
+ else {
95
122
tryDeleteDeclaration ( sourceFile , token , changes , checker , sourceFiles , /*isFixAll*/ true ) ;
96
123
}
97
124
break ;
@@ -125,25 +152,16 @@ namespace ts.codefix {
125
152
return token . kind === SyntaxKind . ImportKeyword ? tryCast ( token . parent , isImportDeclaration ) : undefined ;
126
153
}
127
154
128
- function tryDeleteFullDestructure ( token : Node , changes : textChanges . ChangeTracker , sourceFile : SourceFile , checker : TypeChecker , sourceFiles : readonly SourceFile [ ] , isFixAll : boolean ) : boolean {
129
- if ( token . kind !== SyntaxKind . OpenBraceToken || ! isObjectBindingPattern ( token . parent ) ) return false ;
130
- const decl = token . parent . parent ;
131
- if ( decl . kind === SyntaxKind . Parameter ) {
132
- tryDeleteParameter ( changes , sourceFile , decl , checker , sourceFiles , isFixAll ) ;
133
- }
134
- else {
135
- changes . delete ( sourceFile , decl ) ;
136
- }
137
- return true ;
155
+ function canDeleteEntireVariableStatement ( sourceFile : SourceFile , token : Node ) : boolean {
156
+ return isVariableDeclarationList ( token . parent ) && first ( token . parent . getChildren ( sourceFile ) ) === token ;
138
157
}
139
158
140
- function tryDeleteFullVariableStatement ( sourceFile : SourceFile , token : Node , changes : textChanges . ChangeTracker ) : boolean {
141
- const declarationList = tryCast ( token . parent , isVariableDeclarationList ) ;
142
- if ( declarationList && declarationList . getChildren ( sourceFile ) [ 0 ] === token ) {
143
- changes . delete ( sourceFile , declarationList . parent . kind === SyntaxKind . VariableStatement ? declarationList . parent : declarationList ) ;
144
- return true ;
145
- }
146
- return false ;
159
+ function deleteEntireVariableStatement ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , node : VariableDeclarationList ) {
160
+ changes . delete ( sourceFile , node . parent . kind === SyntaxKind . VariableStatement ? node . parent : node ) ;
161
+ }
162
+
163
+ function deleteDestructuringElements ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , node : ObjectBindingPattern ) {
164
+ forEach ( node . elements , n => changes . delete ( sourceFile , n ) ) ;
147
165
}
148
166
149
167
function tryPrefixDeclaration ( changes : textChanges . ChangeTracker , errorCode : number , sourceFile : SourceFile , token : Node ) : void {
@@ -205,7 +223,7 @@ namespace ts.codefix {
205
223
}
206
224
}
207
225
208
- function tryDeleteParameter ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , p : ParameterDeclaration , checker : TypeChecker , sourceFiles : readonly SourceFile [ ] , isFixAll : boolean ) : void {
226
+ function tryDeleteParameter ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , p : ParameterDeclaration , checker : TypeChecker , sourceFiles : readonly SourceFile [ ] , isFixAll = false ) : void {
209
227
if ( mayDeleteParameter ( p , checker , isFixAll ) ) {
210
228
if ( p . modifiers && p . modifiers . length > 0
211
229
&& ( ! isIdentifier ( p . name ) || FindAllReferences . Core . isSymbolReferencedInFile ( p . name , checker , sourceFile ) ) ) {
0 commit comments