GitHub

Knip

The Knip Language Server is powered by Knip: Find unused files, dependencies, and exports in your JavaScript/TypeScript projects.

Contents

Configuration

Latest version of available settings: types.d.ts

Diagnostics

Diagnostics should work out of the box.

Most Knip issue types are translated to Diagnostic items with a DiagnosticSeverity and emitted using this.connection.sendDiagnostics(). Also see diagnostics.js for details.

Code Actions

Code actions should work out of the box.

Some issues/diagnostics have code actions available. Also see code-actions.js for details.

File Descriptor

Clients request a file descriptor to get available data for a document by sending the REQUEST_FILE_NODE request, in short:

const file = await this.#client.sendRequest(REQUEST_FILE_NODE, {
  uri: editor.document.uri.toString(),
});

Type definition for File: session/types.ts

The file descriptor can be used to implement features like Annotations, Export Hover, Imports and Exports.

Annotations

Annotations (aka "Code Lens" or "Inlay Hint") for exported identifiers can be implemented using data from the file descriptor.

Example:

Export Hover

On hover of an export identifier, the file descriptor can be used to render import locations for the exported identifier.

Optionally, code snippets can be searched for using the provided locations and mixed into the rendered list.

Example:

Imports

The file descriptor can be used to display an overview of imports of a document with direct links to their definition location.

Optionally, the client can implement:

  • Follow cursor between open document and highlight import in view
  • Show cyclic dependencies

Example:

Exports

The file descriptor can be used to display an overview of exports of a document with direct links to their usage locations.

Optionally, the client can implement:

  • Follow cursor between open document and highlight export in view
  • Show contention: naming conflicts through re-exports
  • Show contention: branched/diamond-shaped re-export structures

Example:

File Descriptor for package.json

Clients request the package.json file descriptor to get dependency usage data by sending the REQUEST_PACKAGE_JSON request:

const packageJson = await this.#client.sendRequest(REQUEST_PACKAGE_JSON);

Type definition for DependencyNodes: session/types.ts

Dependency Hover

On hover of a dependency name in package.json, the packageJson descriptor can be used to render import locations for the dependency.

Example:

CLI

To run the language server directly from CLI:

npx @knip/language-server

Add --node-ipc, --stdio, --socket <port> or --pipe <name> to set transport (default: --stdio).

Read the original on github.com ↗