@@ -25,7 +25,7 @@ namespace ts.codefix {
25
25
const forStatement = < ForStatement > token . parent . parent . parent ;
26
26
const forInitializer = < VariableDeclarationList > forStatement . initializer ;
27
27
if ( forInitializer . declarations . length === 1 ) {
28
- return createCodeFix ( "" , forInitializer . pos , forInitializer . end - forInitializer . pos ) ;
28
+ return createCodeFixToRemoveNode ( forInitializer ) ;
29
29
}
30
30
else {
31
31
return removeSingleItem ( forInitializer . declarations , token ) ;
@@ -35,7 +35,7 @@ namespace ts.codefix {
35
35
const forOfStatement = < ForOfStatement > token . parent . parent . parent ;
36
36
if ( forOfStatement . initializer . kind === SyntaxKind . VariableDeclarationList ) {
37
37
const forOfInitializer = < VariableDeclarationList > forOfStatement . initializer ;
38
- return createCodeFix ( "{}" , forOfInitializer . declarations [ 0 ] . pos , forOfInitializer . declarations [ 0 ] . end - forOfInitializer . declarations [ 0 ] . pos ) ;
38
+ return createCodeFix ( "{}" , forOfInitializer . declarations [ 0 ] . getStart ( ) , forOfInitializer . declarations [ 0 ] . getWidth ( ) ) ;
39
39
}
40
40
break ;
41
41
@@ -47,12 +47,12 @@ namespace ts.codefix {
47
47
case SyntaxKind . CatchClause :
48
48
const catchClause = < CatchClause > token . parent . parent ;
49
49
const parameter = catchClause . variableDeclaration . getChildren ( ) [ 0 ] ;
50
- return createCodeFix ( "" , parameter . pos , parameter . end - parameter . pos ) ;
50
+ return createCodeFixToRemoveNode ( parameter ) ;
51
51
52
52
default :
53
53
const variableStatement = < VariableStatement > token . parent . parent . parent ;
54
54
if ( variableStatement . declarationList . declarations . length === 1 ) {
55
- return createCodeFix ( "" , variableStatement . pos , variableStatement . end - variableStatement . pos ) ;
55
+ return createCodeFixToRemoveNode ( variableStatement ) ;
56
56
}
57
57
else {
58
58
const declarations = variableStatement . declarationList . declarations ;
@@ -72,7 +72,7 @@ namespace ts.codefix {
72
72
case ts . SyntaxKind . Parameter :
73
73
const functionDeclaration = < FunctionDeclaration > token . parent . parent ;
74
74
if ( functionDeclaration . parameters . length === 1 ) {
75
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
75
+ return createCodeFixToRemoveNode ( token . parent ) ;
76
76
}
77
77
else {
78
78
return removeSingleItem ( functionDeclaration . parameters , token ) ;
@@ -81,14 +81,14 @@ namespace ts.codefix {
81
81
// handle case where 'import a = A;'
82
82
case SyntaxKind . ImportEqualsDeclaration :
83
83
const importEquals = findImportDeclaration ( token ) ;
84
- return createCodeFix ( "" , importEquals . pos , importEquals . end - importEquals . pos ) ;
84
+ return createCodeFixToRemoveNode ( importEquals ) ;
85
85
86
86
case SyntaxKind . ImportSpecifier :
87
87
const namedImports = < NamedImports > token . parent . parent ;
88
88
if ( namedImports . elements . length === 1 ) {
89
89
// Only 1 import and it is unused. So the entire declaration should be removed.
90
90
const importSpec = findImportDeclaration ( token ) ;
91
- return createCodeFix ( "" , importSpec . pos , importSpec . end - importSpec . pos ) ;
91
+ return createCodeFixToRemoveNode ( importSpec ) ;
92
92
}
93
93
else {
94
94
return removeSingleItem ( namedImports . elements , token ) ;
@@ -100,17 +100,24 @@ namespace ts.codefix {
100
100
const importClause = < ImportClause > token . parent ;
101
101
if ( ! importClause . namedBindings ) { // |import d from './file'| or |import * as ns from './file'|
102
102
const importDecl = findImportDeclaration ( importClause ) ;
103
- return createCodeFix ( "" , importDecl . pos , importDecl . end - importDecl . pos ) ;
103
+ return createCodeFixToRemoveNode ( importDecl ) ;
104
104
}
105
- else { // import |d,| * as ns from './file'
106
- return createCodeFix ( "" , importClause . name . pos , importClause . namedBindings . pos - importClause . name . pos ) ;
105
+ else {
106
+ // import |d,| * as ns from './file'
107
+ const start = importClause . name . getStart ( ) ;
108
+ let end = findFirstNonSpaceCharPosStarting ( importClause . name . end ) ;
109
+ if ( sourceFile . text . charCodeAt ( end ) === CharacterCodes . comma ) {
110
+ end = findFirstNonSpaceCharPosStarting ( end + 1 ) ;
111
+ }
112
+
113
+ return createCodeFix ( "" , start , end - start ) ;
107
114
}
108
115
109
116
case SyntaxKind . NamespaceImport :
110
117
const namespaceImport = < NamespaceImport > token . parent ;
111
118
if ( namespaceImport . name == token && ! ( < ImportClause > namespaceImport . parent ) . name ) {
112
119
const importDecl = findImportDeclaration ( namespaceImport ) ;
113
- return createCodeFix ( "" , importDecl . pos , importDecl . end - importDecl . pos ) ;
120
+ return createCodeFixToRemoveNode ( importDecl ) ;
114
121
}
115
122
else {
116
123
const start = ( < ImportClause > namespaceImport . parent ) . name . end ;
@@ -120,16 +127,14 @@ namespace ts.codefix {
120
127
break ;
121
128
122
129
case SyntaxKind . PropertyDeclaration :
123
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
124
-
125
130
case SyntaxKind . NamespaceImport :
126
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
131
+ return createCodeFixToRemoveNode ( token . parent ) ;
127
132
}
128
133
if ( isDeclarationName ( token ) ) {
129
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
134
+ return createCodeFixToRemoveNode ( token . parent ) ;
130
135
}
131
136
else if ( isLiteralComputedPropertyDeclarationName ( token ) ) {
132
- return createCodeFix ( "" , token . parent . parent . pos , token . parent . parent . end - token . parent . parent . pos ) ;
137
+ return createCodeFixToRemoveNode ( token . parent . parent ) ;
133
138
}
134
139
else {
135
140
return undefined ;
@@ -144,6 +149,17 @@ namespace ts.codefix {
144
149
return importDecl ;
145
150
}
146
151
152
+ function createCodeFixToRemoveNode ( node : Node ) {
153
+ return createCodeFix ( "" , node . getStart ( ) , node . getWidth ( ) ) ;
154
+ }
155
+
156
+ function findFirstNonSpaceCharPosStarting ( start : number ) {
157
+ while ( isWhiteSpace ( sourceFile . text . charCodeAt ( start ) ) ) {
158
+ start += 1 ;
159
+ }
160
+ return start ;
161
+ }
162
+
147
163
function createCodeFix ( newText : string , start : number , length : number ) : CodeAction [ ] {
148
164
return [ {
149
165
description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Remove_declaration_for_Colon_0 ) , { 0 : token . getText ( ) } ) ,
0 commit comments