Skip to content

Commit

Permalink
Handle auto-import when paths pattern is absolute (microsoft#60236)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch authored Oct 15, 2024
1 parent 31de163 commit 3b0dfaa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1290,6 +1291,8 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
subModuleName,
versionPaths.paths,
allowedEndings,
packageRootPath,
getCanonicalFileName,
host,
options,
);
Expand Down
18 changes: 18 additions & 0 deletions tests/cases/fourslash/autoImportPathsConfigDir.ts
Original file line number Diff line number Diff line change
@@ -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"]);

0 comments on commit 3b0dfaa

Please sign in to comment.