Skip to content

Commit fcee820

Browse files
author
Andy
committed
Fix harness getDirectores implementation to not include directory as prefix (#21633)
1 parent c4dca61 commit fcee820

File tree

5 files changed

+11
-17
lines changed

5 files changed

+11
-17
lines changed

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ namespace Harness.LanguageService {
189189
getCancellationToken() { return this.cancellationToken; }
190190
getDirectories(path: string): string[] {
191191
const dir = this.virtualFileSystem.traversePath(path);
192-
if (dir && dir.isDirectory()) {
193-
return ts.map((<Utils.VirtualDirectory>dir).getDirectories(), (d) => ts.combinePaths(path, d.name));
194-
}
195-
return [];
192+
return dir && dir.isDirectory() ? dir.getDirectories().map(d => d.name) : [];
196193
}
197194
getCurrentDirectory(): string { return virtualFileSystemRoot; }
198195
getDefaultLibFileName(): string { return Harness.Compiler.defaultLibFileName; }

src/services/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ namespace ts.Completions {
365365
// import x = require("/*completion position*/");
366366
// var y = require("/*completion position*/");
367367
// export * from "/*completion position*/";
368-
return pathCompletionsInfo(PathCompletions.getStringLiteralCompletionsFromModuleNames(sourceFile, node as StringLiteral, compilerOptions, host, typeChecker));
368+
return pathCompletionsInfo(PathCompletions.getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker));
369369

370370
default:
371371
return fromContextualType();

src/services/pathCompletions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ts.Completions.PathCompletions {
66
const scriptPath = node.getSourceFile().path;
77
const scriptDirectory = getDirectoryPath(scriptPath);
88

9-
const span = getDirectoryFragmentTextSpan((<StringLiteral>node).text, node.getStart(sourceFile) + 1);
9+
const span = getDirectoryFragmentTextSpan(node.text, node.getStart(sourceFile) + 1);
1010
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
1111
const extensions = getSupportedExtensions(compilerOptions);
1212
if (compilerOptions.rootDirs) {
@@ -226,7 +226,8 @@ namespace ts.Completions.PathCompletions {
226226
const includeGlob = normalizedSuffix ? "**/*" : "./*";
227227

228228
const matches = tryReadDirectory(host, baseDirectory, fileExtensions, /*exclude*/ undefined, [includeGlob]);
229-
const directories = tryGetDirectories(host, baseDirectory);
229+
const directories = tryGetDirectories(host, baseDirectory).map(d => combinePaths(baseDirectory, d));
230+
230231
// Trim away prefix and suffix
231232
return mapDefined(concatenate(matches, directories), match => {
232233
const normalizedMatch = normalizePath(match);
@@ -476,14 +477,14 @@ namespace ts.Completions.PathCompletions {
476477
const nodeModulesDependencyKeys = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
477478

478479
function tryGetDirectories(host: LanguageServiceHost, directoryName: string): string[] {
479-
return tryIOAndConsumeErrors(host, host.getDirectories, directoryName);
480+
return tryIOAndConsumeErrors(host, host.getDirectories, directoryName) || [];
480481
}
481482

482-
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] | undefined {
483+
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] | undefined | undefined {
483484
return tryIOAndConsumeErrors(host, host.readDirectory, path, extensions, exclude, include);
484485
}
485486

486-
function tryReadFile(host: LanguageServiceHost, path: string): string {
487+
function tryReadFile(host: LanguageServiceHost, path: string): string | undefined {
487488
return tryIOAndConsumeErrors(host, host.readFile, path);
488489
}
489490

src/services/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ namespace ts {
200200
/* @internal */ hasChangedAutomaticTypeDirectiveNames?: boolean;
201201

202202
/*
203-
* getDirectories is also required for full import and type reference completions. Without it defined, certain
204-
* completions will not be provided
203+
* Required for full import and type reference completions.
204+
* These should be unprefixed names. E.g. `getDirectories("/foo/bar")` should return `["a", "b"]`, not `["/foo/bar/a", "/foo/bar/b"]`.
205205
*/
206206
getDirectories?(directoryName: string): string[];
207207

tests/cases/fourslash/completionsPaths_pathMapping.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,4 @@
2020
////}
2121

2222
const [replacementSpan] = test.ranges();
23-
verify.completionsAt("", [
24-
{ name: "a", replacementSpan },
25-
{ name: "b", replacementSpan },
26-
{ name: "dir", replacementSpan },
27-
]);
23+
verify.completionsAt("", ["a", "b", "dir"].map(name => ({ name, replacementSpan })));

0 commit comments

Comments
 (0)