mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-20 10:29:36 -06:00
Merge pull request #13831 from zhengbli/importFixCasing
Fix casing in import quick fix
This commit is contained in:
commit
12e8f91bf2
@ -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,20 +562,15 @@ namespace ts.codefix {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
function getRelativePathIfInDirectory(path: string, directoryPath: string) {
|
||||
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, false);
|
||||
return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath;
|
||||
}
|
||||
|
||||
function getRelativePath(path: string, directoryPath: string) {
|
||||
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, false);
|
||||
return moduleHasNonRelativeName(relativePath) ? "./" + relativePath : 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
//// [|/// <reference path="./tripleSlashReference.ts" />
|
||||
//// f1/*0*/();|]
|
||||
|
||||
// @Filename: module.ts
|
||||
// @Filename: Module.ts
|
||||
//// export function f1() {}
|
||||
//// export var v1 = 5;
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
verify.importFixAtPosition([
|
||||
`/// <reference path="./tripleSlashReference.ts" />
|
||||
import { f1 } from "./module";
|
||||
import { f1 } from "./Module";
|
||||
|
||||
f1();`
|
||||
]);
|
||||
Loading…
x
Reference in New Issue
Block a user