diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts
index 49b8526a1e071..3642e46612cbe 100644
--- a/src/services/codefixes/importFixes.ts
+++ b/src/services/codefixes/importFixes.ts
@@ -416,8 +416,8 @@ namespace ts.codefix {
);
function getModuleSpecifierForNewImport() {
- const fileName = sourceFile.path;
- const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().path;
+ const fileName = sourceFile.fileName;
+ const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().fileName;
const sourceDirectory = getDirectoryPath(fileName);
const options = context.program.getCompilerOptions();
@@ -439,8 +439,7 @@ namespace ts.codefix {
return undefined;
}
- const normalizedBaseUrl = toPath(options.baseUrl, getDirectoryPath(options.baseUrl), getCanonicalFileName);
- let relativeName = tryRemoveParentDirectoryName(moduleFileName, normalizedBaseUrl);
+ let relativeName = getRelativePathIfInDirectory(moduleFileName, options.baseUrl);
if (!relativeName) {
return undefined;
}
@@ -477,9 +476,8 @@ namespace ts.codefix {
function tryGetModuleNameFromRootDirs() {
if (options.rootDirs) {
- const normalizedRootDirs = map(options.rootDirs, rootDir => toPath(rootDir, /*basePath*/ undefined, getCanonicalFileName));
- const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, normalizedRootDirs);
- const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, normalizedRootDirs);
+ const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, options.rootDirs);
+ const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, options.rootDirs);
if (normalizedTargetPath !== undefined) {
const relativePath = normalizedSourcePath !== undefined ? getRelativePath(normalizedTargetPath, normalizedSourcePath) : normalizedTargetPath;
return removeFileExtension(relativePath);
@@ -546,9 +544,9 @@ namespace ts.codefix {
}
}
- function getPathRelativeToRootDirs(path: Path, rootDirs: Path[]) {
+ function getPathRelativeToRootDirs(path: string, rootDirs: string[]) {
for (const rootDir of rootDirs) {
- const relativeName = tryRemoveParentDirectoryName(path, rootDir);
+ const relativeName = getRelativePathIfInDirectory(path, rootDir);
if (relativeName !== undefined) {
return relativeName;
}
@@ -564,19 +562,14 @@ namespace ts.codefix {
return fileName;
}
- function getRelativePath(path: string, directoryPath: string) {
+ function getRelativePathIfInDirectory(path: string, directoryPath: string) {
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, false);
- return moduleHasNonRelativeName(relativePath) ? "./" + relativePath : relativePath;
+ return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath;
}
- function tryRemoveParentDirectoryName(path: Path, parentDirectory: Path) {
- const index = path.indexOf(parentDirectory);
- if (index === 0) {
- return endsWith(parentDirectory, directorySeparator)
- ? path.substring(parentDirectory.length)
- : path.substring(parentDirectory.length + 1);
- }
- return undefined;
+ function getRelativePath(path: string, directoryPath: string) {
+ const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, false);
+ return moduleHasNonRelativeName(relativePath) ? "./" + relativePath : relativePath;
}
}
diff --git a/tests/cases/fourslash/importNameCodeFixNewImportFile1.ts b/tests/cases/fourslash/importNameCodeFixNewImportFile1.ts
index 0223d96e0186c..9f6cac0b7c14b 100644
--- a/tests/cases/fourslash/importNameCodeFixNewImportFile1.ts
+++ b/tests/cases/fourslash/importNameCodeFixNewImportFile1.ts
@@ -3,7 +3,7 @@
//// [|///
//// f1/*0*/();|]
-// @Filename: module.ts
+// @Filename: Module.ts
//// export function f1() {}
//// export var v1 = 5;
@@ -12,7 +12,7 @@
verify.importFixAtPosition([
`///
-import { f1 } from "./module";
+import { f1 } from "./Module";
f1();`
]);
\ No newline at end of file