Skip to content

Commit

Permalink
fix rename cause import change wrongly (#38462)
Browse files Browse the repository at this point in the history
* use canonical file name when resolve module

* renameSync in vfs supports same folder.

* Update src/harness/vfsUtil.ts

Co-authored-by: Ron Buckton <[email protected]>

* change tss rather than compiler.

* remove useless comment.

* use fileName rather than path.

Co-authored-by: Song Gao <[email protected]>
Co-authored-by: Ron Buckton <[email protected]>
  • Loading branch information
3 people authored Jun 18, 2020
1 parent f697d26 commit 8293e51
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/harness/vfsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ namespace vfs {
if (existingNode) {
if (isDirectory(node)) {
if (!isDirectory(existingNode)) throw createIOError("ENOTDIR");
if (this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY");
// if both old and new arguments point to the same directory, just pass. So we could rename /src/a/1 to /src/A/1 in Win.
// if not and the directory pointed by the new path is not empty, throw an error.
if (this.stringComparer(oldpath, newpath) !== 0 && this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY");
}
else {
if (isDirectory(existingNode)) throw createIOError("EISDIR");
Expand Down
6 changes: 3 additions & 3 deletions src/services/getEditsForFileRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ namespace ts {
): void {
const allFiles = program.getSourceFiles();
for (const sourceFile of allFiles) {
const newFromOld = oldToNew(sourceFile.path) as Path;
const newImportFromPath = newFromOld !== undefined ? newFromOld : sourceFile.path;
const newFromOld = oldToNew(sourceFile.fileName);
const newImportFromPath = newFromOld ?? sourceFile.fileName;
const newImportFromDirectory = getDirectoryPath(newImportFromPath);

const oldFromNew: string | undefined = newToOld(sourceFile.fileName);
Expand Down Expand Up @@ -157,7 +157,7 @@ namespace ts {

// Need an update if the imported file moved, or the importing file moved and was using a relative path.
return toImport !== undefined && (toImport.updated || (importingSourceFileMoved && pathIsRelative(importLiteral.text)))
? moduleSpecifiers.updateModuleSpecifier(program.getCompilerOptions(), newImportFromPath, toImport.newFileName, createModuleSpecifierResolutionHost(program, host), importLiteral.text)
? moduleSpecifiers.updateModuleSpecifier(program.getCompilerOptions(), getCanonicalFileName(newImportFromPath) as Path, toImport.newFileName, createModuleSpecifierResolutionHost(program, host), importLiteral.text)
: undefined;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

// @Filename: /a/b/file1.ts
////import { foo } from "foo";

// @Filename: /a/node_modules/foo/index.d.ts
////export const foo = 0;

verify.getEditsForFileRename({
oldPath: "/a/b",
newPath: "/a/B",
newFileContents: {},
});

0 comments on commit 8293e51

Please sign in to comment.