Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions src/services/pathCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down
6 changes: 1 addition & 5 deletions tests/cases/fourslash/completionsPaths_pathMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })));