Skip to content
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

Add path completions for package.json exports with wildcards #49644

Merged
merged 7 commits into from
Jul 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2092,10 +2092,11 @@ namespace ts {
}

/**
* @internal
* From https://github.com/nodejs/node/blob/8f39f51cbbd3b2de14b9ee896e26421cc5b20121/lib/internal/modules/esm/resolve.js#L722 -
* "longest" has some nuance as to what "longest" means in the presence of pattern trailers
*/
function comparePatternKeys(a: string, b: string) {
export function comparePatternKeys(a: string, b: string) {
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
const aPatternIndex = a.indexOf("*");
const bPatternIndex = b.indexOf("*");
const baseLenA = aPatternIndex === -1 ? a.length : aPatternIndex + 1;
Expand Down Expand Up @@ -2361,7 +2362,7 @@ namespace ts {
}

/* @internal */
export function isApplicableVersionedTypesKey(conditions: string[], key: string) {
export function isApplicableVersionedTypesKey(conditions: readonly string[], key: string) {
if (conditions.indexOf("types") === -1) return false; // only apply versioned types conditions if the types condition is applied
if (!startsWith(key, "types@")) return false;
const range = VersionRange.tryParse(key.substring("types@".length));
Expand Down
295 changes: 191 additions & 104 deletions src/services/stringCompletions.ts

Large diffs are not rendered by default.

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

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "name": "foo",
//// "main": "dist/index.js",
//// "module": "dist/index.mjs",
//// "types": "dist/index.d.ts",
//// "exports": {
//// ".": {
//// "types": "./dist/index.d.ts",
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
//// "import": "./dist/index.mjs",
//// "default": "./dist/index.js"
//// },
//// "./*": {
//// "types": "./dist/*.d.ts",
//// "import": "./dist/*.mjs",
//// "default": "./dist/*.js"
//// },
//// "./arguments": {
//// "types": "./dist/arguments/index.d.ts",
//// "import": "./dist/arguments/index.mjs",
//// "default": "./dist/arguments/index.js"
//// }
//// }
//// }

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

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

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

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

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

// @module: nodenext

// @Filename: /node_modules/salesforce-pageobjects/package.json
//// {
//// "name": "salesforce-pageobjects",
//// "version": "1.0.0",
//// "exports": {
//// "./*": {
//// "types": "./dist/*.d.ts",
//// "import": "./dist/*.mjs",
//// "default": "./dist/*.js"
//// }
//// }
//// }

// @Filename: /node_modules/salesforce-pageobjects/dist/action/pageObjects/actionRenderer.d.ts
//// export const actionRenderer = 0;

// @Filename: /index.mts
//// import { } from "salesforce-pageobjects//**/";

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [{ name: "action", kind: "directory" }]
});

edit.insert("action/");

verify.completions({
isNewIdentifierLocation: true,
exact: [{ name: "pageObjects", kind: "directory" }],
});

edit.insert("pageObjects/");

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

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "types": "index.d.ts",
//// "exports": {
//// "./component-*": {
//// "types@>=4.3.5": "types/components/*.d.ts"
//// }
//// }
//// }

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

// @Filename: /node_modules/foo/types/components/index.d.ts
//// export const index = 0;

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

// @Filename: /node_modules/foo/types/components/subfolder/one.d.ts
//// export const one = 0;

// @Filename: /a.ts
//// import { } from "foo//**/";

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [
{ name: "component-blah", kind: "script" },
{ name: "component-index", kind: "script" },
{ name: "component-subfolder", kind: "directory" },
],
});

edit.insert("component-subfolder/");

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

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "types": "index.d.ts",
//// "exports": {
//// "./*": "dist/*",
//// "./foo/*": "dist/*",
//// "./bar/*": "dist/*",
//// "./exact-match": "dist/index.d.ts"
//// }
//// }

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

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

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

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

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

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

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [
{ name: "blah.js", kind: "script", kindModifiers: ".js" },
{ name: "index.js", kind: "script", kindModifiers: ".js" },
{ name: "foo", kind: "directory" },
{ name: "subfolder", kind: "directory" },
{ name: "bar", kind: "directory" },
{ name: "exact-match", kind: "script" },
],
});

edit.insert("foo/");

verify.completions({
isNewIdentifierLocation: true,
exact: [
{ name: "blah.js", kind: "script", kindModifiers: ".js" },
{ name: "index.js", kind: "script", kindModifiers: ".js" },
{ name: "foo", kind: "directory" },
{ name: "subfolder", kind: "directory" },
],
});

edit.insert("foo/");

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

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "name": "foo",
//// "main": "dist/index.js",
//// "module": "dist/index.mjs",
//// "types": "dist/index.d.ts",
//// "exports": {
//// ".": {
//// "import": {
//// "types": "./dist/types/index.d.mts",
//// "default": "./dist/esm/index.mjs"
//// },
//// "default": {
//// "types": "./dist/types/index.d.ts",
//// "default": "./dist/cjs/index.js"
//// }
//// },
//// "./*": {
//// "import": {
//// "types": "./dist/types/*.d.mts",
//// "default": "./dist/esm/*.mjs"
//// },
//// "default": {
//// "types": "./dist/types/*.d.ts",
//// "default": "./dist/cjs/*.js"
//// }
//// },
//// "./only-in-cjs": {
//// "require": {
//// "types": "./dist/types/only-in-cjs/index.d.ts",
//// "default": "./dist/cjs/only-in-cjs/index.js"
//// }
//// }
//// }
//// }

// @Filename: /node_modules/foo/dist/types/index.d.mts
//// export const index = 0;

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

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

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

// @Filename: /node_modules/foo/dist/types/only-in-cjs/index.d.ts
//// export const onlyInCjs = 0;

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

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

// @module: nodenext

// @Filename: /node_modules/foo/package.json
//// {
//// "name": "foo",
//// "main": "dist/index.js",
//// "module": "dist/index.mjs",
//// "types": "dist/index.d.ts",
//// "exports": {
//// "./*": "./dist/*?.d.ts"
//// }
//// }

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

// @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: "" },
]
});
2 changes: 1 addition & 1 deletion tests/cases/fourslash/server/nodeNextPathCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@

verify.baselineCompletions();
edit.insert("dependency/");
verify.completions({ exact: ["lol", "dir/"], isNewIdentifierLocation: true });
verify.completions({ exact: ["lol", "dir"], isNewIdentifierLocation: true });
edit.insert("l");
verify.completions({ exact: ["lol"], isNewIdentifierLocation: true });