Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/knip/fixtures/plugins/tsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@fixtures/tsx",
"devDependencies": {
"tsx": "*"
},
"scripts": {
"test": "tsx --test"
}
}
Empty file.
Empty file.
35 changes: 34 additions & 1 deletion packages/knip/src/plugins/tsx/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import type { Plugin } from '../../types/config.js';
import type { PackageJson } from 'src/types/package-json.js';
import { toDependency, toEntry } from 'src/util/input.js';
import { hasDependency } from 'src/util/plugin.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use relative imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';

// https://tsx.is

const title = 'tsx';

const enablers = ['tsx'];

const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);

const config = ['package.json'];

const packageJsonPath = (id: PackageJson) => id;

const resolveConfig: ResolveConfig<PackageJson> = localConfig => {
const scripts = localConfig.scripts;

const entries = [toDependency('tsx')];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should probably not be added here. If tsx is listed in package.json but not used anywhere we probably want it reported as unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


if (scripts && Object.keys(scripts).some(script => /(?<=^|\s)tsx\s(.*)--test/.test(scripts[script]))) {
const patterns = [
'**/*{.,-,_}test.?(c|m)(j|t)s',
'**/test-*.?(c|m)(j|t)s',
'**/test.?(c|m)(j|t)s',
'**/test/**/*.?(c|m)(j|t)s',
];
entries.push(...patterns.map(id => toEntry(id)));
}

return entries;
};

const args = {
positional: true,
nodeImportArgs: true,
Expand All @@ -12,5 +41,9 @@ const args = {

export default {
title,
isEnabled,
packageJsonPath,
config,
resolveConfig,
args,
} satisfies Plugin;
21 changes: 21 additions & 0 deletions packages/knip/test/plugins/tsx.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../../src/index.js';
import { resolve } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';

const cwd = resolve('fixtures/plugins/tsx');

test('Find dependencies with the tsx plugin', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 2,
total: 2,
});
});