Support path completions inside node_modules (#19692)

* Support path completions inside node_modules

* Fix: Start searching from current file's directory, not host.getCurrentDirectory()

* Add test for nested node_modules

* Also test in /src/folder/b.ts
This commit is contained in:
Andy
2017-11-03 15:05:44 -07:00
committed by GitHub
parent 78421e87f7
commit 749e151c23
2 changed files with 31 additions and 1 deletions
+10 -1
View File
@@ -144,8 +144,8 @@ namespace ts.Completions.PathCompletions {
let result: CompletionEntry[];
const fileExtensions = getSupportedExtensions(compilerOptions);
if (baseUrl) {
const fileExtensions = getSupportedExtensions(compilerOptions);
const projectDir = compilerOptions.project || host.getCurrentDirectory();
const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl);
result = getCompletionEntriesForDirectoryFragment(fragment, normalizePath(absolute), fileExtensions, /*includeExtensions*/ false, span, host);
@@ -176,6 +176,15 @@ namespace ts.Completions.PathCompletions {
result = [];
}
if (compilerOptions.moduleResolution === ts.ModuleResolutionKind.NodeJs) {
forEachAncestorDirectory(scriptPath, ancestor => {
const nodeModules = combinePaths(ancestor, "node_modules");
if (host.directoryExists(nodeModules)) {
getCompletionEntriesForDirectoryFragment(fragment, nodeModules, fileExtensions, /*includeExtensions*/ false, span, host, /*exclude*/ undefined, result);
}
});
}
getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span, result);
for (const moduleName of enumeratePotentialNonRelativeModules(fragment, scriptPath, compilerOptions, typeChecker, host)) {
+21
View File
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />
// @moduleResolution: node
// @Filename: /node_modules/x/foo.d.ts
////not read
// @Filename: /node_modules/x/bar.d.ts
////not read
// @Filename: /src/node_modules/y/index.d.ts
////not read
// @Filename: /src/a.ts
////import {} from "/*1*/";
// @Filename: /src/folder/b.ts
////import {} from "x//*2*/";
verify.completionsAt("1", ["y", "x"]);
verify.completionsAt("2", ["bar", "foo"]);