@@ -8,7 +8,13 @@ namespace ts.codefix {
8
8
getCodeActions : ( context : CodeFixContext ) => {
9
9
const sourceFile = context . sourceFile ;
10
10
const start = context . span . start ;
11
- const token = getTokenAtPosition ( sourceFile , start ) ;
11
+
12
+ let token = getTokenAtPosition ( sourceFile , start ) ;
13
+
14
+ // this handles var ["computed"] = 12;
15
+ if ( token . kind === SyntaxKind . OpenBracketToken ) {
16
+ token = getTokenAtPosition ( sourceFile , start + 1 ) ;
17
+ }
12
18
13
19
switch ( token . kind ) {
14
20
case ts . SyntaxKind . Identifier :
@@ -72,34 +78,43 @@ namespace ts.codefix {
72
78
return removeSingleItem ( functionDeclaration . parameters , token ) ;
73
79
}
74
80
81
+ // handle case where 'import a = A;'
82
+ case SyntaxKind . ImportEqualsDeclaration :
83
+ let importEquals = findImportDeclaration ( token ) ;
84
+ return createCodeFix ( "" , importEquals . pos , importEquals . end - importEquals . pos ) ;
85
+
75
86
case SyntaxKind . ImportSpecifier :
76
87
const namedImports = < NamedImports > token . parent . parent ;
77
- const elements = namedImports . elements ;
78
- if ( elements . length === 1 ) {
79
- // Only 1 import and it is unused. So the entire line could be removed.
80
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
88
+ if ( namedImports . elements . length === 1 ) {
89
+ // Only 1 import and it is unused. So the entire declaration should be removed.
90
+ let importSpec = findImportDeclaration ( token ) ;
91
+ return createCodeFix ( "" , importSpec . pos , importSpec . end - importSpec . pos ) ;
81
92
}
82
93
else {
83
- return removeSingleItem ( elements , token ) ;
94
+ return removeSingleItem ( namedImports . elements , token ) ;
84
95
}
85
96
86
- // handle case where 'import a = A;'
87
- case SyntaxKind . ImportEqualsDeclaration :
88
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
89
-
90
- // handle case where 'import d from './file'
91
- case SyntaxKind . ImportClause :
92
- return createCodeFix ( "" , token . parent . parent . pos , token . parent . parent . end - token . parent . parent . pos ) ;
97
+ // handle case where "import d, * as ns from './file'"
98
+ // or "'import {a, b as ns} from './file'"
99
+ case SyntaxKind . ImportClause : // this covers both 'import |d|' and 'import |d,| *'
100
+ const importClause = < ImportClause > token . parent ;
101
+ if ( ! importClause . namedBindings ) { // |import d from './file'| or |import * as ns from './file'|
102
+ const importDecl = findImportDeclaration ( importClause ) ;
103
+ return createCodeFix ( "" , importDecl . pos , importDecl . end - importDecl . pos ) ;
104
+ }
105
+ else { // import |d,| * as ns from './file'
106
+ return createCodeFix ( "" , importClause . name . pos , importClause . namedBindings . pos - importClause . name . pos ) ;
107
+ }
93
108
94
- // handle case where 'import * as a from './file'
95
109
case SyntaxKind . NamespaceImport :
96
- return createCodeFix ( "" , token . parent . parent . parent . pos , token . parent . parent . parent . end - token . parent . parent . parent . pos ) ;
97
-
98
- default :
99
- if ( isDeclarationName ( token ) ) {
100
- return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
110
+ const namespaceImport = < NamespaceImport > token . parent ;
111
+ if ( namespaceImport . name == token && ! ( < ImportClause > namespaceImport . parent ) . name ) {
112
+ const importDecl = findImportDeclaration ( namespaceImport ) ;
113
+ return createCodeFix ( "" , importDecl . pos , importDecl . end - importDecl . pos ) ;
114
+ } else {
115
+ const start = ( < ImportClause > namespaceImport . parent ) . name . end ;
116
+ return createCodeFix ( "" , start , ( < ImportClause > namespaceImport . parent ) . namedBindings . end - start ) ;
101
117
}
102
- break ;
103
118
}
104
119
break ;
105
120
@@ -109,8 +124,24 @@ namespace ts.codefix {
109
124
case SyntaxKind . NamespaceImport :
110
125
return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
111
126
}
127
+ if ( isDeclarationName ( token ) ) {
128
+ return createCodeFix ( "" , token . parent . pos , token . parent . end - token . parent . pos ) ;
129
+ }
130
+ else if ( isLiteralComputedPropertyDeclarationName ( token ) ) {
131
+ return createCodeFix ( "" , token . parent . parent . pos , token . parent . parent . end - token . parent . parent . pos ) ;
132
+ }
133
+ else {
134
+ return undefined ;
135
+ }
112
136
113
- return undefined ;
137
+ function findImportDeclaration ( token : Node ) : Node {
138
+ let importDecl = token ;
139
+ while ( importDecl . kind != SyntaxKind . ImportDeclaration && importDecl . parent ) {
140
+ importDecl = importDecl . parent ;
141
+ }
142
+
143
+ return importDecl ;
144
+ }
114
145
115
146
function createCodeFix ( newText : string , start : number , length : number ) : CodeAction [ ] {
116
147
return [ {
@@ -133,3 +164,19 @@ namespace ts.codefix {
133
164
}
134
165
} ) ;
135
166
}
167
+
168
+ const s = "hello" ;
169
+
170
+ class C {
171
+
172
+
173
+
174
+ private [ "string" ] : string ;
175
+ private "b iz" : string ;
176
+
177
+ bar ( ) {
178
+ this
179
+ }
180
+ }
181
+
182
+
0 commit comments