Skip to content

Commit ee7d0e2

Browse files
author
Andy
authored
getEditsForFileRename: Don't resolve to a.js when a.ts is moved (microsoft#27081)
1 parent f71d600 commit ee7d0e2

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/services/getEditsForFileRename.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,23 @@ namespace ts {
196196
}
197197

198198
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);
201202

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+
}
204207

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
208216
}
209217
}
210218

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export const x = 0;
5+
6+
// @Filename: /a.js
7+
////exports.x = 0;
8+
9+
// @Filename: /b.ts
10+
////import { x } from "./a";
11+
12+
verify.getEditsForFileRename({
13+
oldPath: "/a.ts",
14+
newPath: "/a2.ts",
15+
newFileContents: {
16+
"/b.ts": 'import { x } from "./a2";',
17+
},
18+
});

0 commit comments

Comments
 (0)