Skip to content

Fix path completions with sibling dts lookup in exports #54869

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
merged 2 commits into from
Jul 3, 2023
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
15 changes: 11 additions & 4 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
getBaseFileName,
getConditions,
getContextualTypeFromParent,
getDeclarationEmitExtensionForPath,
getDirectoryPath,
getEffectiveTypeRoots,
getEmitModuleResolutionKind,
Expand Down Expand Up @@ -1023,6 +1024,8 @@ function getModulesForPathsPattern(
const expandedPrefixDirectory = fragmentHasPath ? combinePaths(normalizedPrefixDirectory, normalizedPrefixBase + fragmentDirectory) : normalizedPrefixDirectory;

const normalizedSuffix = normalizePath(parsed.suffix);
const declarationExtension = normalizedSuffix && getDeclarationEmitExtensionForPath("_" + normalizedSuffix);
const matchingSuffixes = declarationExtension ? [changeExtension(normalizedSuffix, declarationExtension), normalizedSuffix] : [normalizedSuffix];
// Need to normalize after combining: If we combinePaths("a", "../b"), we want "b" and not "a/../b".
const baseDirectory = normalizePath(combinePaths(packageDirectory, expandedPrefixDirectory));
const completePrefix = fragmentHasPath ? baseDirectory : ensureTrailingDirectorySeparator(baseDirectory) + normalizedPrefixBase;
Expand All @@ -1035,9 +1038,11 @@ function getModulesForPathsPattern(
// interpreted as "any character" can only return *too many* results as compared to the literal
// interpretation, so we can filter those superfluous results out via `trimPrefixAndSuffix` as we've always
// done.
const includeGlob = normalizedSuffix ? "**/*" + normalizedSuffix : "./*";
const includeGlobs = normalizedSuffix
? matchingSuffixes.map(suffix => "**/*" + suffix)
: ["./*"];

const matches = mapDefined(tryReadDirectory(host, baseDirectory, extensionOptions.extensionsToSearch, /*exclude*/ undefined, [includeGlob]), match => {
const matches = mapDefined(tryReadDirectory(host, baseDirectory, extensionOptions.extensionsToSearch, /*exclude*/ undefined, includeGlobs), match => {
const trimmedWithPattern = trimPrefixAndSuffix(match);
if (trimmedWithPattern) {
if (containsSlash(trimmedWithPattern)) {
Expand All @@ -1057,8 +1062,10 @@ function getModulesForPathsPattern(
return [...matches, ...directories];

function trimPrefixAndSuffix(path: string): string | undefined {
const inner = withoutStartAndEnd(normalizePath(path), completePrefix, normalizedSuffix);
return inner === undefined ? undefined : removeLeadingDirectorySeparator(inner);
return firstDefined(matchingSuffixes, suffix => {
const inner = withoutStartAndEnd(normalizePath(path), completePrefix, suffix);
return inner === undefined ? undefined : removeLeadingDirectorySeparator(inner);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "name": "foo",
//// "exports": {
//// "./*": "./dist/*.js"
//// }
//// }

// @Filename: /node_modules/foo/dist/blah.d.ts
//// export const blah = 0;

// @Filename: /index.mts
//// import { } from "foo//**/";

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [
{ name: "blah", kind: "script", kindModifiers: "" },
]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path="fourslash.ts" />

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "name": "foo",
//// "exports": {
//// "./*": "./dist/*.js"
//// }
//// }

// @Filename: /node_modules/foo/dist/blah.js
//// export const blah = 0;

// @Filename: /node_modules/foo/dist/blah.d.ts
//// export declare const blah: 0;

// @Filename: /index.mts
//// import { } from "foo//**/";

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [
{ name: "blah", kind: "script", kindModifiers: "" },
]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path="fourslash.ts" />

// @module: nodenext
// @allowJs: true
// @maxNodeModuleJsDepth: 1

// @Filename: /node_modules/foo/package.json
//// {
//// "name": "foo",
//// "exports": {
//// "./*": "./dist/*.js"
//// }
//// }

// @Filename: /node_modules/foo/dist/blah.js
//// export const blah = 0;

// @Filename: /index.mts
//// import { } from "foo//**/";

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [
{ name: "blah", kind: "script", kindModifiers: "" },
]
});