@@ -196,15 +196,23 @@ namespace ts {
196
196
}
197
197
198
198
function getSourceFileToImportFromResolved ( resolved : ResolvedModuleWithFailedLookupLocations | undefined , oldToNew : PathUpdater , host : LanguageServiceHost ) : ToImport | undefined {
199
- return resolved && (
200
- ( resolved . resolvedModule && getIfExists ( resolved . resolvedModule . resolvedFileName ) ) || firstDefined ( resolved . failedLookupLocations , getIfExists ) ) ;
199
+ // Search through all locations looking for a moved file, and only then test already existing files.
200
+ // This is because if `a.ts` is compiled to `a.js` and `a.ts` is moved, we don't want to resolve anything to `a.js`, but to `a.ts`'s new location.
201
+ return tryEach ( tryGetNewFile ) || tryEach ( tryGetOldFile ) ;
201
202
202
- function getIfExists ( oldLocation : string ) : ToImport | undefined {
203
- const newLocation = oldToNew ( oldLocation ) ;
203
+ function tryEach ( cb : ( oldFileName : string ) => ToImport | undefined ) : ToImport | undefined {
204
+ return resolved && (
205
+ ( resolved . resolvedModule && cb ( resolved . resolvedModule . resolvedFileName ) ) || firstDefined ( resolved . failedLookupLocations , cb ) ) ;
206
+ }
204
207
205
- return host . fileExists ! ( oldLocation ) || newLocation !== undefined && host . fileExists ! ( newLocation ) // TODO: GH#18217
206
- ? newLocation !== undefined ? { newFileName : newLocation , updated : true } : { newFileName : oldLocation , updated : false }
207
- : undefined ;
208
+ function tryGetNewFile ( oldFileName : string ) : ToImport | undefined {
209
+ const newFileName = oldToNew ( oldFileName ) ;
210
+ return newFileName !== undefined && host . fileExists ! ( newFileName ) ? { newFileName, updated : true } : undefined ; // TODO: GH#18217
211
+ }
212
+
213
+ function tryGetOldFile ( oldFileName : string ) : ToImport | undefined {
214
+ const newFileName = oldToNew ( oldFileName ) ;
215
+ return host . fileExists ! ( oldFileName ) ? newFileName !== undefined ? { newFileName, updated : true } : { newFileName : oldFileName , updated : false } : undefined ; // TODO: GH#18217
208
216
}
209
217
}
210
218
0 commit comments