Skip to content

Commit 12e8f91

Browse files
authored
Merge pull request #13831 from zhengbli/importFixCasing
Fix casing in import quick fix
2 parents 79529db + 6c56b3d commit 12e8f91

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/services/codefixes/importFixes.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ namespace ts.codefix {
416416
);
417417

418418
function getModuleSpecifierForNewImport() {
419-
const fileName = sourceFile.path;
420-
const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().path;
419+
const fileName = sourceFile.fileName;
420+
const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().fileName;
421421
const sourceDirectory = getDirectoryPath(fileName);
422422
const options = context.program.getCompilerOptions();
423423

@@ -439,8 +439,7 @@ namespace ts.codefix {
439439
return undefined;
440440
}
441441

442-
const normalizedBaseUrl = toPath(options.baseUrl, getDirectoryPath(options.baseUrl), getCanonicalFileName);
443-
let relativeName = tryRemoveParentDirectoryName(moduleFileName, normalizedBaseUrl);
442+
let relativeName = getRelativePathIfInDirectory(moduleFileName, options.baseUrl);
444443
if (!relativeName) {
445444
return undefined;
446445
}
@@ -477,9 +476,8 @@ namespace ts.codefix {
477476

478477
function tryGetModuleNameFromRootDirs() {
479478
if (options.rootDirs) {
480-
const normalizedRootDirs = map(options.rootDirs, rootDir => toPath(rootDir, /*basePath*/ undefined, getCanonicalFileName));
481-
const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, normalizedRootDirs);
482-
const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, normalizedRootDirs);
479+
const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, options.rootDirs);
480+
const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, options.rootDirs);
483481
if (normalizedTargetPath !== undefined) {
484482
const relativePath = normalizedSourcePath !== undefined ? getRelativePath(normalizedTargetPath, normalizedSourcePath) : normalizedTargetPath;
485483
return removeFileExtension(relativePath);
@@ -546,9 +544,9 @@ namespace ts.codefix {
546544
}
547545
}
548546

549-
function getPathRelativeToRootDirs(path: Path, rootDirs: Path[]) {
547+
function getPathRelativeToRootDirs(path: string, rootDirs: string[]) {
550548
for (const rootDir of rootDirs) {
551-
const relativeName = tryRemoveParentDirectoryName(path, rootDir);
549+
const relativeName = getRelativePathIfInDirectory(path, rootDir);
552550
if (relativeName !== undefined) {
553551
return relativeName;
554552
}
@@ -564,19 +562,14 @@ namespace ts.codefix {
564562
return fileName;
565563
}
566564

567-
function getRelativePath(path: string, directoryPath: string) {
565+
function getRelativePathIfInDirectory(path: string, directoryPath: string) {
568566
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, false);
569-
return moduleHasNonRelativeName(relativePath) ? "./" + relativePath : relativePath;
567+
return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath;
570568
}
571569

572-
function tryRemoveParentDirectoryName(path: Path, parentDirectory: Path) {
573-
const index = path.indexOf(parentDirectory);
574-
if (index === 0) {
575-
return endsWith(parentDirectory, directorySeparator)
576-
? path.substring(parentDirectory.length)
577-
: path.substring(parentDirectory.length + 1);
578-
}
579-
return undefined;
570+
function getRelativePath(path: string, directoryPath: string) {
571+
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, false);
572+
return moduleHasNonRelativeName(relativePath) ? "./" + relativePath : relativePath;
580573
}
581574
}
582575

tests/cases/fourslash/importNameCodeFixNewImportFile1.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//// [|/// <reference path="./tripleSlashReference.ts" />
44
//// f1/*0*/();|]
55

6-
// @Filename: module.ts
6+
// @Filename: Module.ts
77
//// export function f1() {}
88
//// export var v1 = 5;
99

@@ -12,7 +12,7 @@
1212

1313
verify.importFixAtPosition([
1414
`/// <reference path="./tripleSlashReference.ts" />
15-
import { f1 } from "./module";
15+
import { f1 } from "./Module";
1616
1717
f1();`
1818
]);

0 commit comments

Comments
 (0)