@@ -133,7 +133,7 @@ namespace ts.codefix {
133133 const symbolIdActionMap = new ImportCodeActionMap ( ) ;
134134
135135 // this is a module id -> module import declaration map
136- const cachedImportDeclarations : ( ImportDeclaration | ImportEqualsDeclaration ) [ ] [ ] = [ ] ;
136+ const cachedImportDeclarations : AnyImportSyntax [ ] [ ] = [ ] ;
137137 let lastImportDeclaration : Node ;
138138
139139 const currentTokenMeaning = getMeaningFromLocation ( token ) ;
@@ -199,28 +199,20 @@ namespace ts.codefix {
199199 return cached ;
200200 }
201201
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 ) ;
209204 cachedImportDeclarations [ moduleSymbolId ] = existingDeclarations ;
210205 return existingDeclarations ;
211206
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 ;
222215 }
223- return undefined ;
224216 }
225217 }
226218
0 commit comments