@@ -133,7 +133,7 @@ namespace ts.codefix {
133
133
const symbolIdActionMap = new ImportCodeActionMap ( ) ;
134
134
135
135
// this is a module id -> module import declaration map
136
- const cachedImportDeclarations : ( ImportDeclaration | ImportEqualsDeclaration ) [ ] [ ] = [ ] ;
136
+ const cachedImportDeclarations : AnyImportSyntax [ ] [ ] = [ ] ;
137
137
let lastImportDeclaration : Node ;
138
138
139
139
const currentTokenMeaning = getMeaningFromLocation ( token ) ;
@@ -199,28 +199,20 @@ namespace ts.codefix {
199
199
return cached ;
200
200
}
201
201
202
- const existingDeclarations : ( ImportDeclaration | ImportEqualsDeclaration ) [ ] = [ ] ;
203
- for ( const importModuleSpecifier of sourceFile . imports ) {
204
- const importSymbol = checker . getSymbolAtLocation ( importModuleSpecifier ) ;
205
- if ( importSymbol === moduleSymbol ) {
206
- existingDeclarations . push ( getImportDeclaration ( importModuleSpecifier ) ) ;
207
- }
208
- }
202
+ const existingDeclarations = mapDefined ( sourceFile . imports , importModuleSpecifier =>
203
+ checker . getSymbolAtLocation ( importModuleSpecifier ) === moduleSymbol ? getImportDeclaration ( importModuleSpecifier ) : undefined ) ;
209
204
cachedImportDeclarations [ moduleSymbolId ] = existingDeclarations ;
210
205
return existingDeclarations ;
211
206
212
- function getImportDeclaration ( moduleSpecifier : LiteralExpression ) {
213
- let node : Node = moduleSpecifier ;
214
- while ( node ) {
215
- if ( node . kind === SyntaxKind . ImportDeclaration ) {
216
- return < ImportDeclaration > node ;
217
- }
218
- if ( node . kind === SyntaxKind . ImportEqualsDeclaration ) {
219
- return < ImportEqualsDeclaration > node ;
220
- }
221
- node = node . parent ;
207
+ function getImportDeclaration ( { parent } : LiteralExpression ) : AnyImportSyntax {
208
+ switch ( parent . kind ) {
209
+ case SyntaxKind . ImportDeclaration :
210
+ return parent as ImportDeclaration ; ;
211
+ case SyntaxKind . ExternalModuleReference :
212
+ return ( parent as ExternalModuleReference ) . parent ;
213
+ default :
214
+ return undefined ;
222
215
}
223
- return undefined ;
224
216
}
225
217
}
226
218
0 commit comments