Skip to content

Commit e66e471

Browse files
author
Andy
authored
Remove leading directory separator from path mapping completion (#21688) (#21691)
1 parent c46ab32 commit e66e471

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/services/pathCompletions.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ namespace ts.Completions.PathCompletions {
179179

180180
function getCompletionsForPathMapping(
181181
path: string, patterns: ReadonlyArray<string>, fragment: string, baseUrl: string, fileExtensions: ReadonlyArray<string>, host: LanguageServiceHost,
182-
): string[] {
182+
): ReadonlyArray<string> {
183183
if (!endsWith(path, "*")) {
184184
// For a path mapping "foo": ["/x/y/z.ts"], add "foo" itself as a completion.
185185
return !stringContains(path, "*") && startsWith(path, fragment) ? [path] : emptyArray;
@@ -231,16 +231,19 @@ namespace ts.Completions.PathCompletions {
231231
// Trim away prefix and suffix
232232
return mapDefined(concatenate(matches, directories), match => {
233233
const normalizedMatch = normalizePath(match);
234-
if (!endsWith(normalizedMatch, normalizedSuffix) || !startsWith(normalizedMatch, completePrefix)) {
235-
return;
236-
}
237-
238-
const start = completePrefix.length;
239-
const length = normalizedMatch.length - start - normalizedSuffix.length;
240-
return removeFileExtension(normalizedMatch.substr(start, length));
234+
const inner = withoutStartAndEnd(normalizedMatch, completePrefix, normalizedSuffix);
235+
return inner !== undefined ? removeLeadingDirectorySeparator(removeFileExtension(inner)) : undefined;
241236
});
242237
}
243238

239+
function withoutStartAndEnd(s: string, start: string, end: string): string | undefined {
240+
return startsWith(s, start) && endsWith(s, end) ? s.slice(start.length, s.length - end.length) : undefined;
241+
}
242+
243+
function removeLeadingDirectorySeparator(path: string): string {
244+
return path[0] === directorySeparator ? path.slice(1) : path;
245+
}
246+
244247
function enumeratePotentialNonRelativeModules(fragment: string, scriptPath: string, options: CompilerOptions, typeChecker: TypeChecker, host: LanguageServiceHost): string[] {
245248
// Check If this is a nested module
246249
const isNestedModule = stringContains(fragment, directorySeparator);

tests/cases/fourslash/completionsPaths_pathMapping.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
/////export const x = 0;
88

99
// @Filename: /src/a.ts
10-
////import {} from "foo/[|/**/|]";
10+
////import {} from "foo/[|/*0*/|]";
11+
////import {} from "foo/dir/[|/*1*/|]";
1112

1213
// @Filename: /tsconfig.json
1314
////{
@@ -19,5 +20,6 @@
1920
//// }
2021
////}
2122

22-
const [replacementSpan] = test.ranges();
23-
verify.completionsAt("", ["a", "b", "dir"].map(name => ({ name, replacementSpan })));
23+
const [r0, r1] = test.ranges();
24+
verify.completionsAt("0", ["a", "b", "dir"].map(name => ({ name, replacementSpan: r0 })));
25+
verify.completionsAt("1", ["x"].map(name => ({ name, replacementSpan: r1 })));

0 commit comments

Comments
 (0)