Skip to content

Commit 5712730

Browse files
authored
fix: non-relative path completion without baseUrl (#52908)
1 parent 511921e commit 5712730

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/services/stringCompletions.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
getOwnKeys,
5555
getPackageJsonTypesVersionsPaths,
5656
getPathComponents,
57+
getPathsBasePath,
5758
getReplacementSpanForContextToken,
5859
getResolvePackageJsonExports,
5960
getSupportedExtensions,
@@ -559,7 +560,7 @@ function getStringLiteralCompletionsFromModuleNamesWorker(sourceFile: SourceFile
559560
const scriptDirectory = getDirectoryPath(scriptPath);
560561
const extensionOptions = getExtensionOptions(compilerOptions, ReferenceKind.ModuleSpecifier, sourceFile, typeChecker, preferences, mode);
561562

562-
return isPathRelativeToScript(literalValue) || !compilerOptions.baseUrl && (isRootedDiskPath(literalValue) || isUrl(literalValue))
563+
return isPathRelativeToScript(literalValue) || !compilerOptions.baseUrl && !compilerOptions.paths && (isRootedDiskPath(literalValue) || isUrl(literalValue))
563564
? getCompletionEntriesForRelativeModules(literalValue, scriptDirectory, compilerOptions, host, scriptPath, extensionOptions)
564565
: getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, mode, compilerOptions, host, extensionOptions, typeChecker);
565566
}
@@ -850,13 +851,15 @@ function getCompletionEntriesForNonRelativeModules(
850851

851852
const result = createNameAndKindSet();
852853
const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
854+
853855
if (baseUrl) {
854-
const projectDir = compilerOptions.project || host.getCurrentDirectory();
855-
const absolute = normalizePath(combinePaths(projectDir, baseUrl));
856+
const absolute = normalizePath(combinePaths(host.getCurrentDirectory(), baseUrl));
856857
getCompletionEntriesForDirectoryFragment(fragment, absolute, extensionOptions, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
857-
if (paths) {
858-
addCompletionEntriesFromPaths(result, fragment, absolute, extensionOptions, host, paths);
859-
}
858+
}
859+
860+
if (paths) {
861+
const absolute = getPathsBasePath(compilerOptions, host)!;
862+
addCompletionEntriesFromPaths(result, fragment, absolute, extensionOptions, host, paths);
860863
}
861864

862865
const fragmentDirectory = getFragmentDirectory(fragment);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Should give completions for modules referenced via baseUrl and paths compiler options with explicit name mappings
4+
5+
// @Filename: tsconfig.json
6+
//// {
7+
//// "compilerOptions": {
8+
//// "paths": {
9+
//// "module1/*": ["some/path/*"],
10+
//// }
11+
//// }
12+
//// }
13+
14+
// @Filename: test0.ts
15+
//// import * as foo1 from "module1/w/*first*/
16+
17+
// @Filename: some/path/whatever.ts
18+
//// export {}
19+
20+
verify.completions({ marker: ["first"], exact: "whatever", isNewIdentifierLocation: true });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Should give completions for modules referenced via baseUrl and paths compiler options with explicit name mappings
4+
5+
// @Filename: tsconfig.json
6+
//// {
7+
//// "compilerOptions": {
8+
//// "paths": {
9+
//// "/*": ["./*"]
10+
//// },
11+
//// }
12+
//// }
13+
14+
// @Filename: test0.ts
15+
//// import * as foo1 from "/path/w/*first*/
16+
17+
// @Filename: path/whatever.ts
18+
//// export {}
19+
20+
verify.completions({ marker: ["first"], exact: ["whatever"], isNewIdentifierLocation: true });

0 commit comments

Comments
 (0)