Fix harness getDirectores implementation to not include directory as prefix (#21633)

This commit is contained in:
Andy 2018-02-05 11:11:00 -08:00 committed by GitHub
parent 14bd0a2d56
commit 1784e51929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 17 deletions

View File

@ -189,10 +189,7 @@ namespace Harness.LanguageService {
getCancellationToken() { return this.cancellationToken; }
getDirectories(path: string): string[] {
const dir = this.virtualFileSystem.traversePath(path);
if (dir && dir.isDirectory()) {
return ts.map((<Utils.VirtualDirectory>dir).getDirectories(), (d) => ts.combinePaths(path, d.name));
}
return [];
return dir && dir.isDirectory() ? dir.getDirectories().map(d => d.name) : [];
}
getCurrentDirectory(): string { return virtualFileSystemRoot; }
getDefaultLibFileName(): string { return Harness.Compiler.defaultLibFileName; }

View File

@ -359,7 +359,7 @@ namespace ts.Completions {
// import x = require("/*completion position*/");
// var y = require("/*completion position*/");
// export * from "/*completion position*/";
return pathCompletionsInfo(PathCompletions.getStringLiteralCompletionsFromModuleNames(sourceFile, node as StringLiteral, compilerOptions, host, typeChecker));
return pathCompletionsInfo(PathCompletions.getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker));
default:
return fromContextualType();

View File

@ -6,7 +6,7 @@ namespace ts.Completions.PathCompletions {
const scriptPath = node.getSourceFile().path;
const scriptDirectory = getDirectoryPath(scriptPath);
const span = getDirectoryFragmentTextSpan((<StringLiteral>node).text, node.getStart(sourceFile) + 1);
const span = getDirectoryFragmentTextSpan(node.text, node.getStart(sourceFile) + 1);
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
const extensions = getSupportedExtensions(compilerOptions);
if (compilerOptions.rootDirs) {
@ -226,7 +226,8 @@ namespace ts.Completions.PathCompletions {
const includeGlob = normalizedSuffix ? "**/*" : "./*";
const matches = tryReadDirectory(host, baseDirectory, fileExtensions, /*exclude*/ undefined, [includeGlob]);
const directories = tryGetDirectories(host, baseDirectory);
const directories = tryGetDirectories(host, baseDirectory).map(d => combinePaths(baseDirectory, d));
// Trim away prefix and suffix
return mapDefined(concatenate(matches, directories), match => {
const normalizedMatch = normalizePath(match);
@ -476,14 +477,14 @@ namespace ts.Completions.PathCompletions {
const nodeModulesDependencyKeys = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
function tryGetDirectories(host: LanguageServiceHost, directoryName: string): string[] {
return tryIOAndConsumeErrors(host, host.getDirectories, directoryName);
return tryIOAndConsumeErrors(host, host.getDirectories, directoryName) || [];
}
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] | undefined {
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] | undefined | undefined {
return tryIOAndConsumeErrors(host, host.readDirectory, path, extensions, exclude, include);
}
function tryReadFile(host: LanguageServiceHost, path: string): string {
function tryReadFile(host: LanguageServiceHost, path: string): string | undefined {
return tryIOAndConsumeErrors(host, host.readFile, path);
}

View File

@ -200,8 +200,8 @@ namespace ts {
/* @internal */ hasChangedAutomaticTypeDirectiveNames?: boolean;
/*
* getDirectories is also required for full import and type reference completions. Without it defined, certain
* completions will not be provided
* Required for full import and type reference completions.
* These should be unprefixed names. E.g. `getDirectories("/foo/bar")` should return `["a", "b"]`, not `["/foo/bar/a", "/foo/bar/b"]`.
*/
getDirectories?(directoryName: string): string[];

View File

@ -20,8 +20,4 @@
////}
const [replacementSpan] = test.ranges();
verify.completionsAt("", [
{ name: "a", replacementSpan },
{ name: "b", replacementSpan },
{ name: "dir", replacementSpan },
]);
verify.completionsAt("", ["a", "b", "dir"].map(name => ({ name, replacementSpan })));