Handle auto-import when `paths` pattern is absolute (#60236)

This commit is contained in:
Andrew Branch 2024-10-15 14:01:12 -07:00 коммит произвёл GitHub
Родитель 31de163db7
Коммит 3b0dfaa73c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 24 добавлений и 3 удалений

Просмотреть файл

@ -613,7 +613,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
prefersTsExtension(allowedEndings),
);
const fromPaths = pathsOnly || fromPackageJsonImports === undefined ? paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, allowedEndings, host, compilerOptions) : undefined;
const fromPaths = pathsOnly || fromPackageJsonImports === undefined ? paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, allowedEndings, baseDirectory, getCanonicalFileName, host, compilerOptions) : undefined;
if (pathsOnly) {
return fromPaths;
}
@ -925,10 +925,11 @@ function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol, checker: TypeCh
}
}
function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<readonly string[]>, allowedEndings: ModuleSpecifierEnding[], host: ModuleSpecifierResolutionHost, compilerOptions: CompilerOptions): string | undefined {
function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<readonly string[]>, allowedEndings: ModuleSpecifierEnding[], baseDirectory: string, getCanonicalFileName: GetCanonicalFileName, host: ModuleSpecifierResolutionHost, compilerOptions: CompilerOptions): string | undefined {
for (const key in paths) {
for (const patternText of paths[key]) {
const pattern = normalizePath(patternText);
const normalized = normalizePath(patternText);
const pattern = getRelativePathIfInSameVolume(normalized, baseDirectory, getCanonicalFileName) ?? normalized;
const indexOfStar = pattern.indexOf("*");
// In module resolution, if `pattern` itself has an extension, a file with that extension is looked up directly,
// meaning a '.ts' or '.d.ts' extension is allowed to resolve. This is distinct from the case where a '*' substitution
@ -1290,6 +1291,8 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
subModuleName,
versionPaths.paths,
allowedEndings,
packageRootPath,
getCanonicalFileName,
host,
options,
);

Просмотреть файл

@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />
// @Filename: tsconfig.json
//// {
//// "compilerOptions": {
//// "paths": {
//// "@root/*": ["${configDir}/src/*"]
//// }
//// }
//// }
// @Filename: src/one.ts
//// export const one = 1;
// @Filename: src/foo/two.ts
//// one/**/
verify.importFixModuleSpecifiers("", ["@root/one"]);