@knip/mcp 0.0.22 → 0.0.23

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.
@@ -251,20 +251,20 @@ This is why plugins can implement the `resolveFromAST` function.
251
251
  ### 8. resolveFromAST
252
252
 
253
253
  Let's take a look at the Astro plugin implementation. This example assumes some
254
- familiarity with Abstract Syntax Trees (AST) and the TypeScript compiler API.
255
- Knip will provide more and more AST helpers to make implementing plugins more
256
- fun and a little less tedious.
254
+ familiarity with Abstract Syntax Trees (AST). Knip provides AST helpers to make
255
+ implementing plugins more fun and a little less tedious.
257
256
 
258
257
  Anyway, let's dive in. Here's how we're adding the Starlight `components` paths
259
258
  to the default `production` file patterns:
260
259
 
261
260
  ```ts
262
- import ts from 'typescript';
261
+ import type { Program } from 'oxc-parser';
263
262
  import {
263
+ findCallArg,
264
264
  getDefaultImportName,
265
265
  getImportMap,
266
266
  getPropertyValues,
267
- } from '../../typescript/ast-helpers.js';
267
+ } from '../../typescript/ast-helpers.ts';
268
268
 
269
269
  const title = 'Astro';
270
270
 
@@ -275,44 +275,24 @@ const production = [
275
275
  'src/actions/index.{js,ts}',
276
276
  ];
277
277
 
278
- const getComponentPathsFromSourceFile = (sourceFile: ts.SourceFile) => {
279
- const componentPaths: Set<string> = new Set();
280
- const importMap = getImportMap(sourceFile);
278
+ const getComponentPaths = (program: Program) => {
279
+ const importMap = getImportMap(program);
281
280
  const importName = getDefaultImportName(importMap, '@astrojs/starlight');
282
-
283
- function visit(node: ts.Node) {
284
- if (
285
- ts.isCallExpression(node) &&
286
- ts.isIdentifier(node.expression) &&
287
- node.expression.text === importName // match the starlight() function call
288
- ) {
289
- const starlightConfig = node.arguments[0];
290
- if (ts.isObjectLiteralExpression(starlightConfig)) {
291
- const values = getPropertyValues(starlightConfig, 'components');
292
- for (const value of values) componentPaths.add(value);
293
- }
294
- }
295
-
296
- ts.forEachChild(node, visit);
297
- }
298
-
299
- visit(sourceFile);
300
-
301
- return componentPaths;
281
+ if (!importName) return new Set<string>();
282
+ const starlightConfig = findCallArg(program, importName);
283
+ return getPropertyValues(starlightConfig, 'components');
302
284
  };
303
285
 
304
- const resolveFromAST: ResolveFromAST = (sourceFile: ts.SourceFile) => {
305
- // Include './src/components/Head.astro' and './src/components/Footer.astro'
306
- // as production entry files so they're also part of the analysis
307
- const componentPaths = getComponentPathsFromSourceFile(sourceFile);
286
+ const resolveFromAST: ResolveFromAST = (program: Program) => {
287
+ const componentPaths = getComponentPaths(program);
308
288
  return [...production, ...componentPaths].map(id => toProductionEntry(id));
309
289
  };
310
290
 
311
- const plugin: Plugin {
291
+ const plugin: Plugin = {
312
292
  title,
313
293
  production,
314
294
  resolveFromAST,
315
- }
295
+ };
316
296
 
317
297
  export default plugin;
318
298
  ```