Skip to content

Commit 749e151

Browse files
author
Andy
authored
Support path completions inside node_modules (microsoft#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
1 parent 78421e8 commit 749e151

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/services/pathCompletions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ namespace ts.Completions.PathCompletions {
144144

145145
let result: CompletionEntry[];
146146

147+
const fileExtensions = getSupportedExtensions(compilerOptions);
147148
if (baseUrl) {
148-
const fileExtensions = getSupportedExtensions(compilerOptions);
149149
const projectDir = compilerOptions.project || host.getCurrentDirectory();
150150
const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl);
151151
result = getCompletionEntriesForDirectoryFragment(fragment, normalizePath(absolute), fileExtensions, /*includeExtensions*/ false, span, host);
@@ -176,6 +176,15 @@ namespace ts.Completions.PathCompletions {
176176
result = [];
177177
}
178178

179+
if (compilerOptions.moduleResolution === ts.ModuleResolutionKind.NodeJs) {
180+
forEachAncestorDirectory(scriptPath, ancestor => {
181+
const nodeModules = combinePaths(ancestor, "node_modules");
182+
if (host.directoryExists(nodeModules)) {
183+
getCompletionEntriesForDirectoryFragment(fragment, nodeModules, fileExtensions, /*includeExtensions*/ false, span, host, /*exclude*/ undefined, result);
184+
}
185+
});
186+
}
187+
179188
getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span, result);
180189

181190
for (const moduleName of enumeratePotentialNonRelativeModules(fragment, scriptPath, compilerOptions, typeChecker, host)) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
5+
// @Filename: /node_modules/x/foo.d.ts
6+
////not read
7+
8+
// @Filename: /node_modules/x/bar.d.ts
9+
////not read
10+
11+
// @Filename: /src/node_modules/y/index.d.ts
12+
////not read
13+
14+
// @Filename: /src/a.ts
15+
////import {} from "/*1*/";
16+
17+
// @Filename: /src/folder/b.ts
18+
////import {} from "x//*2*/";
19+
20+
verify.completionsAt("1", ["y", "x"]);
21+
verify.completionsAt("2", ["bar", "foo"]);

0 commit comments

Comments
 (0)