Skip to content

Commit 82fb294

Browse files
author
Andy
authored
For import fix, support path mapping value that begins in "./" or ends in ".ts" (#21035)
* For import fix, support path mapping value that begins in "./" or ends in ".ts" * Handle repeated "./" in path * Rename parameters to indicate that they're relative to baseUrl * Support relative path beginning with `..\` * Remove unused function
1 parent fdd8a52 commit 82fb294

4 files changed

+91
-10
lines changed

src/services/codefixes/importFixes.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ namespace ts.codefix {
363363
}
364364
}
365365

366+
if (isPathRelativeToParent(relativeToBaseUrl)) {
367+
return [relativePath];
368+
}
369+
366370
/*
367371
Prefer a relative import over a baseUrl import if it doesn't traverse up to baseUrl.
368372
@@ -424,24 +428,25 @@ namespace ts.codefix {
424428
}
425429
}
426430

427-
function tryGetModuleNameFromPaths(relativeNameWithIndex: string, relativeName: string, paths: MapLike<ReadonlyArray<string>>): string | undefined {
431+
function tryGetModuleNameFromPaths(relativeToBaseUrlWithIndex: string, relativeToBaseUrl: string, paths: MapLike<ReadonlyArray<string>>): string | undefined {
428432
for (const key in paths) {
429-
for (const pattern of paths[key]) {
433+
for (const patternText of paths[key]) {
434+
const pattern = removeFileExtension(normalizePath(patternText));
430435
const indexOfStar = pattern.indexOf("*");
431436
if (indexOfStar === 0 && pattern.length === 1) {
432437
continue;
433438
}
434439
else if (indexOfStar !== -1) {
435440
const prefix = pattern.substr(0, indexOfStar);
436441
const suffix = pattern.substr(indexOfStar + 1);
437-
if (relativeName.length >= prefix.length + suffix.length &&
438-
startsWith(relativeName, prefix) &&
439-
endsWith(relativeName, suffix)) {
440-
const matchedStar = relativeName.substr(prefix.length, relativeName.length - suffix.length);
441-
return key.replace("\*", matchedStar);
442+
if (relativeToBaseUrl.length >= prefix.length + suffix.length &&
443+
startsWith(relativeToBaseUrl, prefix) &&
444+
endsWith(relativeToBaseUrl, suffix)) {
445+
const matchedStar = relativeToBaseUrl.substr(prefix.length, relativeToBaseUrl.length - suffix.length);
446+
return key.replace("*", matchedStar);
442447
}
443448
}
444-
else if (pattern === relativeName || pattern === relativeNameWithIndex) {
449+
else if (pattern === relativeToBaseUrl || pattern === relativeToBaseUrlWithIndex) {
445450
return key;
446451
}
447452
}
@@ -601,7 +606,10 @@ namespace ts.codefix {
601606
}
602607

603608
function getPathRelativeToRootDirs(path: string, rootDirs: ReadonlyArray<string>, getCanonicalFileName: GetCanonicalFileName): string | undefined {
604-
return firstDefined(rootDirs, rootDir => getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName));
609+
return firstDefined(rootDirs, rootDir => {
610+
const relativePath = getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName);
611+
return isPathRelativeToParent(relativePath) ? undefined : relativePath;
612+
});
605613
}
606614

607615
function removeExtensionAndIndexPostFix(fileName: string, options: CompilerOptions, addJsExtension: boolean): string {
@@ -615,7 +623,11 @@ namespace ts.codefix {
615623

616624
function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
617625
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
618-
return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath;
626+
return isRootedDiskPath(relativePath) ? undefined : relativePath;
627+
}
628+
629+
function isPathRelativeToParent(path: string): boolean {
630+
return startsWith(path, "..");
619631
}
620632

621633
function getRelativePath(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /src/a.ts
4+
////[|foo|]
5+
6+
// @Filename: /src/thisHasPathMapping.ts
7+
////export function foo() {};
8+
9+
// @Filename: /tsconfig.json
10+
////{
11+
//// "compilerOptions": {
12+
//// "baseUrl": ".",
13+
//// "paths": {
14+
//// "foo": ["src/thisHasPathMapping.ts"]
15+
//// }
16+
//// }
17+
////}
18+
19+
verify.importFixAtPosition([
20+
`import { foo } from "foo";
21+
22+
foo`
23+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////[|foo|]
5+
6+
// @Filename: /thisHasPathMapping.ts
7+
////export function foo() {};
8+
9+
// @Filename: /tsconfig.json
10+
////{
11+
//// "compilerOptions": {
12+
//// "baseUrl": ".",
13+
//// "paths": {
14+
//// "foo": ["././thisHasPathMapping"]
15+
//// }
16+
//// }
17+
////}
18+
19+
verify.importFixAtPosition([
20+
`import { foo } from "foo";
21+
22+
foo`
23+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /src/a.ts
4+
////[|foo|]
5+
6+
// @Filename: /thisHasPathMapping.ts
7+
////export function foo() {};
8+
9+
// @Filename: /tsconfig.json
10+
////{
11+
//// "compilerOptions": {
12+
//// "baseUrl": "src",
13+
//// "paths": {
14+
//// "foo": ["..\\thisHasPathMapping"]
15+
//// }
16+
//// }
17+
////}
18+
19+
verify.importFixAtPosition([
20+
`import { foo } from "foo";
21+
22+
foo`
23+
]);

0 commit comments

Comments
 (0)