From 513706a44aba2f5111f182179325bbbb095110f3 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 22 Jun 2018 10:21:56 -0700 Subject: [PATCH] getEditsForFileRename: Don't update import with non-relative path if the imported file didn't move --- src/services/getEditsForFileRename.ts | 9 +++++---- ...ForFileRename_unaffectedNonRelativePath.ts | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/getEditsForFileRename_unaffectedNonRelativePath.ts diff --git a/src/services/getEditsForFileRename.ts b/src/services/getEditsForFileRename.ts index b9b8ba4e3a38e..c7fd5313022c7 100644 --- a/src/services/getEditsForFileRename.ts +++ b/src/services/getEditsForFileRename.ts @@ -125,10 +125,11 @@ namespace ts { // TODO:GH#18217 ? getSourceFileToImportFromResolved(resolveModuleName(importLiteral.text, oldImportFromPath, program.getCompilerOptions(), host as ModuleResolutionHost), oldToNew, program) : getSourceFileToImport(importLiteral, sourceFile, program, host, oldToNew); - // If neither the importing source file nor the imported file moved, do nothing. - return toImport === undefined || !toImport.updated && !importingSourceFileMoved - ? undefined - : moduleSpecifiers.getModuleSpecifier(program.getCompilerOptions(), sourceFile, newImportFromPath, toImport.newFileName, host, preferences); + + // 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.getModuleSpecifier(program.getCompilerOptions(), sourceFile, newImportFromPath, toImport.newFileName, host, preferences) + : undefined; }); } } diff --git a/tests/cases/fourslash/getEditsForFileRename_unaffectedNonRelativePath.ts b/tests/cases/fourslash/getEditsForFileRename_unaffectedNonRelativePath.ts new file mode 100644 index 0000000000000..d2f9daeba3e19 --- /dev/null +++ b/tests/cases/fourslash/getEditsForFileRename_unaffectedNonRelativePath.ts @@ -0,0 +1,20 @@ +/// + +// @Filename: /sub/a.ts +////export const a = 1; + +// @Filename: /sub/b.ts +////import { a } from "sub/a"; + +// @Filename: /tsconfig.json +////{ +//// "compilerOptions": { +//// "baseUrl": "." +//// } +////} + +verify.getEditsForFileRename({ + oldPath: "/sub/b.ts", + newPath: "/sub/c/d.ts", + newFileContents: {}, +});