diff --git a/packages/knip/src/plugins/index.ts b/packages/knip/src/plugins/index.ts index f55f79e1b..21411329f 100644 --- a/packages/knip/src/plugins/index.ts +++ b/packages/knip/src/plugins/index.ts @@ -38,7 +38,6 @@ import { default as msw } from './msw/index.js'; import { default as nest } from './nest/index.js'; import { default as netlify } from './netlify/index.js'; import { default as next } from './next/index.js'; -import { default as nodeTestRunner } from './node-test-runner/index.js'; import { default as node } from './node/index.js'; import { default as nodemon } from './nodemon/index.js'; import { default as npmPackageJsonLint } from './npm-package-json-lint/index.js'; @@ -133,7 +132,6 @@ export const Plugins = { netlify, next, node, - 'node-test-runner': nodeTestRunner, nodemon, 'npm-package-json-lint': npmPackageJsonLint, nuxt, diff --git a/packages/knip/src/plugins/node-test-runner/index.ts b/packages/knip/src/plugins/node-test-runner/index.ts deleted file mode 100644 index 48ef55027..000000000 --- a/packages/knip/src/plugins/node-test-runner/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { IsPluginEnabled, Plugin } from '../../types/config.js'; - -// https://nodejs.dev/en/api/test/ - -const title = 'Node.js Test Runner'; - -const enablers = 'This plugin is enabled when any script in `package.json` includes `node --test`'; - -// TODO Better to scan the entry files until the first `node:test` import, but that's expensive -const isEnabled: IsPluginEnabled = ({ manifest }) => - Object.keys(manifest.scripts ?? {}).some( - script => manifest.scripts && /(?<=^|\s)node (.*)--test/.test(manifest.scripts[script]) - ); - -const entry = ['**/*{.,-,_}test.?(c|m)js', '**/test-*.?(c|m)js', '**/test.?(c|m)js', '**/test/**/*.?(c|m)js']; - -export default { - title, - enablers, - isEnabled, - entry, -} satisfies Plugin; diff --git a/packages/knip/src/plugins/node/index.ts b/packages/knip/src/plugins/node/index.ts index 0d5422c5f..a535eb66c 100644 --- a/packages/knip/src/plugins/node/index.ts +++ b/packages/knip/src/plugins/node/index.ts @@ -1,7 +1,27 @@ -import type { Plugin } from '../../types/config.js'; +import type { IsPluginEnabled, Plugin, ResolveEntryPaths } from '../../types/config.js'; +import type { PackageJson } from '../../types/package-json.js'; +import { toEntry } from '../../util/input.js'; const title = 'Node.js'; +const isEnabled: IsPluginEnabled = () => true; + +const config = ['package.json']; + +const packageJsonPath = (id: PackageJson) => id; + +const resolveEntryPaths: ResolveEntryPaths = localConfig => { + const scripts = localConfig.scripts; + + const entry = ['server.js']; + + if (scripts && Object.keys(scripts).some(script => /(?<=^|\s)node\s(.*)--test/.test(scripts[script]))) { + entry.push(...['**/*{.,-,_}test.?(c|m)js', '**/test-*.?(c|m)js', '**/test.?(c|m)js', '**/test/**/*.?(c|m)js']); + } + + return entry.map(toEntry); +}; + const args = { positional: true, nodeImportArgs: true, @@ -9,5 +29,9 @@ const args = { export default { title, + isEnabled, + packageJsonPath, + config, + resolveEntryPaths, args, } satisfies Plugin; diff --git a/packages/knip/src/schema/plugins.ts b/packages/knip/src/schema/plugins.ts index b77ff9042..30e67668a 100644 --- a/packages/knip/src/schema/plugins.ts +++ b/packages/knip/src/schema/plugins.ts @@ -53,7 +53,6 @@ export const pluginsSchema = z.object({ netlify: pluginSchema, next: pluginSchema, node: pluginSchema, - 'node-test-runner': pluginSchema, nodemon: pluginSchema, 'npm-package-json-lint': pluginSchema, nuxt: pluginSchema, diff --git a/packages/knip/src/types/PluginNames.ts b/packages/knip/src/types/PluginNames.ts index cb22d414b..1f6c04c2f 100644 --- a/packages/knip/src/types/PluginNames.ts +++ b/packages/knip/src/types/PluginNames.ts @@ -40,7 +40,6 @@ export type PluginName = | 'netlify' | 'next' | 'node' - | 'node-test-runner' | 'nodemon' | 'npm-package-json-lint' | 'nuxt' @@ -134,7 +133,6 @@ export const pluginNames = [ 'netlify', 'next', 'node', - 'node-test-runner', 'nodemon', 'npm-package-json-lint', 'nuxt', diff --git a/packages/knip/src/types/config.ts b/packages/knip/src/types/config.ts index 197664e8b..683466a01 100644 --- a/packages/knip/src/types/config.ts +++ b/packages/knip/src/types/config.ts @@ -125,7 +125,7 @@ export type Resolve = (options: PluginOptions) => Promise | Input[]; export interface Plugin { title: string; args?: Args; - packageJsonPath?: string | ((manifest: PackageJson) => string); + packageJsonPath?: string | ((manifest: PackageJson) => unknown); enablers?: IgnorePatterns | string; isEnabled?: IsPluginEnabled; isRootOnly?: boolean;