Skip to content

Commit 0c14ad8

Browse files
committed
Explicitly handle pattern exports by stopping up to the star
1 parent 3df1c87 commit 0c14ad8

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/services/stringCompletions.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,14 @@ namespace ts.Completions.StringCompletions {
582582
if (fragmentDirectory && isEmitModuleResolutionRespectingExportMaps(compilerOptions)) {
583583
const nodeModulesDirectoryLookup = ancestorLookup;
584584
ancestorLookup = ancestor => {
585-
const components = getPathComponents(fragmentDirectory);
586-
let packagePath = components.pop();
585+
const components = getPathComponents(fragment);
586+
components.shift(); // shift off empty root
587+
let packagePath = components.shift();
587588
if (!packagePath) {
588589
return nodeModulesDirectoryLookup(ancestor);
589590
}
590591
if (startsWith(packagePath, "@")) {
591-
const subName = components.pop();
592+
const subName = components.shift();
592593
if (!subName) {
593594
return nodeModulesDirectoryLookup(ancestor);
594595
}
@@ -610,7 +611,14 @@ namespace ts.Completions.StringCompletions {
610611
const subpath = k.substring(2);
611612
if (!startsWith(subpath, fragmentSubpath)) return undefined;
612613
// subpath is a valid export (barring conditions, which we don't currently check here)
613-
return subpath;
614+
if (!stringContains(subpath, "*")) {
615+
return subpath;
616+
}
617+
// pattern export - only return everything up to the `*`, so the user can autocomplete, then
618+
// keep filling in the pattern (we could speculatively return a list of options by hitting disk,
619+
// but conditions will make that somewhat awkward, as each condition may have a different set of possible
620+
// options for the `*`.
621+
return subpath.slice(0, subpath.indexOf("*"));
614622
});
615623
forEach(processedKeys, k => {
616624
result.push(nameAndKind(k, ScriptElementKind.externalModuleName, /*extension*/ undefined));

tests/cases/fourslash/server/nodeNextPathCompletions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
//// },
1212
//// "./lol": {
1313
//// "types": "./lib/lol.d.ts"
14-
//// }
14+
//// },
15+
//// "./dir/*": "./lib/*"
1516
//// }
1617
//// }
1718

@@ -37,6 +38,6 @@
3738

3839
verify.baselineCompletions();
3940
edit.insert("dependency/");
40-
verify.completions({ exact: ["lol"], isNewIdentifierLocation: true });
41+
verify.completions({ exact: ["lol", "dir/"], isNewIdentifierLocation: true });
4142
edit.insert("l");
4243
verify.completions({ exact: ["lol"], isNewIdentifierLocation: true });

0 commit comments

Comments
 (0)