@knip/language-server 2.0.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -84,7 +84,7 @@ Example:
84
84
 
85
85
  ### Imports
86
86
 
87
- The file desciptor can be used to display an overview of imports of a document
87
+ The file descriptor can be used to display an overview of imports of a document
88
88
  with direct links to their definition location.
89
89
 
90
90
  Optionally, the client can implement:
@@ -99,7 +99,7 @@ Example:
99
99
 
100
100
  ### Exports
101
101
 
102
- The file desciptor can be used to display an overview of exports of a document
102
+ The file descriptor can be used to display an overview of exports of a document
103
103
  with direct links to their usage locations.
104
104
 
105
105
  Optionally, the client can implement:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knip/language-server",
3
- "version": "2.0.3",
3
+ "version": "3.0.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/webpro-nl/knip.git",
@@ -20,10 +20,10 @@
20
20
  "dependencies": {
21
21
  "vscode-languageserver": "^9.0.1",
22
22
  "vscode-languageserver-textdocument": "^1.0.12",
23
- "knip": "^5.87.0"
23
+ "knip": "^6.0.0"
24
24
  },
25
25
  "engines": {
26
- "node": ">=18.20.0"
26
+ "node": "^20.19.0 || >=22.12.0"
27
27
  },
28
28
  "scripts": {
29
29
  "release": "release-it -c ../../.release-it.json"
package/src/server.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
1
3
  import path from 'node:path';
2
4
  import { fileURLToPath, pathToFileURL } from 'node:url';
3
5
  import { createOptions, createSession, KNIP_CONFIG_LOCATIONS } from 'knip/session';
@@ -26,6 +28,17 @@ import { issueToDiagnostic } from './diagnostics.js';
26
28
 
27
29
  const RESTART_FOR = new Set(['package.json', ...KNIP_CONFIG_LOCATIONS]);
28
30
 
31
+ /** @param {string} resolvedPath */
32
+ function readKnipVersion(resolvedPath) {
33
+ for (let dir = path.dirname(resolvedPath); dir !== path.dirname(dir); dir = path.dirname(dir)) {
34
+ try {
35
+ const pkg = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'));
36
+ if (pkg.name === 'knip') return ` v${pkg.version}`;
37
+ } catch {}
38
+ }
39
+ return '';
40
+ }
41
+
29
42
  /** @type {Config} */
30
43
  const DEFAULT_CONFIG = {
31
44
  deferSession: false,
@@ -67,8 +80,8 @@ const FILE_CHANGE_TYPES = new Map([
67
80
  ]);
68
81
 
69
82
  const ISSUE_DESC = {
70
- classMembers: 'class member',
71
83
  enumMembers: 'enum member',
84
+ namespaceMembers: 'namespace member',
72
85
  types: 'export keyword',
73
86
  exports: 'export keyword',
74
87
  };
@@ -213,11 +226,28 @@ export class LanguageServer {
213
226
  this.published = new Set(newDiags.keys());
214
227
  }
215
228
 
229
+ async #resolveKnipSession() {
230
+ if (this.cwd) {
231
+ try {
232
+ const localRequire = createRequire(path.join(this.cwd, 'package.json'));
233
+ const resolved = localRequire.resolve('knip/session');
234
+ const local = await import(pathToFileURL(resolved).href);
235
+ this.connection.console.log(`Using local knip${readKnipVersion(resolved)}`);
236
+ return local;
237
+ } catch {}
238
+ }
239
+ this.connection.console.log(
240
+ `Using bundled knip${readKnipVersion(createRequire(__filename).resolve('knip/session'))}`
241
+ );
242
+ return { createOptions, createSession };
243
+ }
244
+
216
245
  async start() {
217
246
  if (this.session) return;
218
247
 
219
248
  try {
220
249
  const config = await this.getConfig();
250
+ const knip = await this.#resolveKnipSession();
221
251
 
222
252
  const configFilePath = config?.configFilePath
223
253
  ? path.isAbsolute(config.configFilePath)
@@ -226,12 +256,12 @@ export class LanguageServer {
226
256
  : undefined;
227
257
 
228
258
  this.connection.console.log('Creating options');
229
- const options = await createOptions({ cwd: this.cwd, isSession: true, args: { config: configFilePath } });
259
+ const options = await knip.createOptions({ cwd: this.cwd, isSession: true, args: { config: configFilePath } });
230
260
  this.rules = options.rules;
231
261
 
232
262
  this.connection.console.log('Building module graph...');
233
263
  const start = Date.now();
234
- const session = await createSession(options);
264
+ const session = await knip.createSession(options);
235
265
  this.connection.console.log(`Finished building module graph (${Date.now() - start}ms)`);
236
266
 
237
267
  this.session = session;
@@ -372,8 +402,8 @@ export class LanguageServer {
372
402
  if (
373
403
  issueType === 'exports' ||
374
404
  issueType === 'types' ||
375
- issueType === 'classMembers' ||
376
- issueType === 'enumMembers'
405
+ issueType === 'enumMembers' ||
406
+ issueType === 'namespaceMembers'
377
407
  ) {
378
408
  const removeExportEdit = createRemoveExportEdit(document, uri, issue);
379
409
  if (!removeExportEdit) continue;