From d49a28534a44241f9fd42f3dd523b67252667eb9 Mon Sep 17 00:00:00 2001 From: Emma Hamilton Date: Fri, 2 Jun 2023 09:16:50 +1000 Subject: [PATCH] Use custom conditions in path completions (#54332) --- src/services/stringCompletions.ts | 3 +- ...ackageJsonExportsBundlerNoNodeCondition.ts | 31 +++++++++++++++++++ ...tionsPackageJsonExportsCustomConditions.ts | 28 +++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/pathCompletionsPackageJsonExportsBundlerNoNodeCondition.ts create mode 100644 tests/cases/fourslash/pathCompletionsPackageJsonExportsCustomConditions.ts diff --git a/src/services/stringCompletions.ts b/src/services/stringCompletions.ts index 61c23266701..df0e24e5589 100644 --- a/src/services/stringCompletions.ts +++ b/src/services/stringCompletions.ts @@ -44,6 +44,7 @@ import { flatten, forEachAncestorDirectory, getBaseFileName, + getConditions, getContextualTypeFromParent, getDirectoryPath, getEffectiveTypeRoots, @@ -924,7 +925,7 @@ function getCompletionEntriesForNonRelativeModules( } const keys = getOwnKeys(exports); const fragmentSubpath = components.join("/") + (components.length && hasTrailingDirectorySeparator(fragment) ? "/" : ""); - const conditions = mode === ModuleKind.ESNext ? ["node", "import", "types"] : ["node", "require", "types"]; + const conditions = getConditions(compilerOptions, mode === ModuleKind.ESNext); addCompletionEntriesFromPathsOrExports( result, fragmentSubpath, diff --git a/tests/cases/fourslash/pathCompletionsPackageJsonExportsBundlerNoNodeCondition.ts b/tests/cases/fourslash/pathCompletionsPackageJsonExportsBundlerNoNodeCondition.ts new file mode 100644 index 00000000000..13134b69605 --- /dev/null +++ b/tests/cases/fourslash/pathCompletionsPackageJsonExportsBundlerNoNodeCondition.ts @@ -0,0 +1,31 @@ +/// + +// @moduleResolution: bundler + +// @Filename: /node_modules/foo/package.json +//// { +//// "name": "foo", +//// "exports": { +//// "./only-for-node": { +//// "node": "./something.js" +//// }, +//// "./for-everywhere": "./other.js", +//// } +//// } + +// @Filename: /node_modules/foo/something.d.ts +//// export const index = 0; + +// @Filename: /node_modules/foo/other.d.ts +//// export const index = 0; + +// @Filename: /index.ts +//// import { } from "foo//**/"; + +verify.completions({ + marker: "", + isNewIdentifierLocation: true, + exact: [ + { name: "for-everywhere", kind: "script", kindModifiers: "" }, + ] +}); diff --git a/tests/cases/fourslash/pathCompletionsPackageJsonExportsCustomConditions.ts b/tests/cases/fourslash/pathCompletionsPackageJsonExportsCustomConditions.ts new file mode 100644 index 00000000000..749bd13855f --- /dev/null +++ b/tests/cases/fourslash/pathCompletionsPackageJsonExportsCustomConditions.ts @@ -0,0 +1,28 @@ +/// + +// @module: nodenext +// @customConditions: custom-condition + +// @Filename: /node_modules/foo/package.json +//// { +//// "name": "foo", +//// "exports": { +//// "./only-with-custom-conditions": { +//// "custom-condition": "./something.js" +//// }, +//// } +//// } + +// @Filename: /node_modules/foo/something.d.ts +//// export const index = 0; + +// @Filename: /index.ts +//// import { } from "foo//**/"; + +verify.completions({ + marker: "", + isNewIdentifierLocation: true, + exact: [ + { name: "only-with-custom-conditions", kind: "script", kindModifiers: "" }, + ] +});