Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add knip core logic #4

Merged
merged 19 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ jobs:
- run: git branch --track main origin/main
if: ${{ github.event_name == 'pull_request' }}

- run: npx nx-cloud record -- nx format:check
- run: npx nx affected -t lint test build
- run: npx nx format:check
- run: npx nx affected -t lint unit-test integration-test build
6,669 changes: 4,086 additions & 2,583 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@
"packages/*"
],
"dependencies": {
"@code-pushup/cli": "^0.47.0",
"@code-pushup/core": "^0.47.0",
"@code-pushup/models": "^0.47.0",
"@code-pushup/utils": "^0.47.0",
"@code-pushup/cli": "^0.57.0",
"@code-pushup/core": "^0.57.0",
"@code-pushup/models": "^0.57.0",
"@code-pushup/utils": "^0.57.0",
"@nx/devkit": "19.4.3",
"@poppinss/cliui": "^6.4.1",
"ansis": "^3.8.1",
"knip": "^5.42.0",
"memfs": "^4.17.0",
"zod": "^3.24.1"
},
"optionalDependencies": {
"@nx/nx-darwin-arm64": "^16.9.1",
"@nx/nx-darwin-x64": "^16.10.0",
"@nx/nx-linux-x64-gnu": "16.7.4"
}
}
7 changes: 6 additions & 1 deletion packages/plugin-knip/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": ["error"]
"@nx/dependency-checks": [
"error",
{
"ignoredDependencies": ["@code-pushup/test-utils"]
}
]
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-knip/CONTRIBUTING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
The [knip](https://knip.dev/) plugin implementation is restricted due to [a bug that wont get fixed](https://github.com/webpro/knip/issues/551).

As we can't consume knip core logic because it produces an unwanted side effect (linked above), we have to implement a `RunnerConfig` instead of a `RunnerFunction`.
Our current solution is to implement a [custom knip reporter](https://knip.dev/features/reporters#custom-reporters) which is bundled as an [additional entry point](./src/reporter.ts).
Our current solution is to implement a [custom knip reporter](https://knip.dev/features/reporters#custom-reporters) which is bundled as an [additional entry point](src/lib/reporter.ts).
The produced output is internally consumed by the `RunnerConfig` and wrapped into a `PluginReport`.
2 changes: 1 addition & 1 deletion packages/plugin-knip/mocks/fixtures/raw-knip.report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const rawReport: Pick<ReporterOptions, 'report' | 'issues' | 'options'> =
'configurationGenerator|default': {
type: 'duplicates',
filePath:
'/Users/michael_hladky/WebstormProjects/quality-metrics-cli/packages/nx-plugin/src/generators/configuration/generator.ts',
'/Users/username/Projects/quality-metrics-cli/packages/nx-plugin/src/generators/configuration/generator.ts',
symbol: 'configurationGenerator|default',
symbols: [
{
Expand Down
15 changes: 13 additions & 2 deletions packages/plugin-knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
"name": "@code-pushup/knip-plugin",
"version": "0.0.0",
"license": "MIT",
"dependencies": {},
"peerDependencies": {}
"dependencies": {
"@code-pushup/models": "^0.57.0",
"@code-pushup/utils": "^0.57.0",
"ansis": "^3.8.1",
"knip": "^5.42.0",
"memfs": "^4.17.0",
"tslib": "^2.3.0",
"vite": "^5.0.0",
"vitest": "^1.3.1",
"zod": "^3.24.1"
},
"peerDependencies": {},
"devDependencies": {}
}
5 changes: 3 additions & 2 deletions packages/plugin-knip/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
"unit-test": {
"executor": "@nx/vite:test",
"options": {
"config": "packages/plugin-knip/vite.config.unit.ts"
"configFile": "packages/plugin-knip/vite.config.unit.ts"
}
},
"integration-test": {
"dependsOn": ["build"],
"executor": "@nx/vite:test",
"options": {
"config": "packages/plugin-knip/vite.config.integration.ts"
"configFile": "packages/plugin-knip/vite.config.integration.ts"
}
}
},
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-knip/src/index.ts

This file was deleted.

36 changes: 36 additions & 0 deletions packages/plugin-knip/src/lib/constants.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';
import { auditSchema, groupSchema } from '@code-pushup/models';
import {
KNIP_AUDITS,
KNIP_GROUP_ALL,
KNIP_GROUP_DEPENDENCIES,
KNIP_GROUP_EXPORTS,
KNIP_GROUP_FILES,
} from './constants';

describe('constants-AUDITS', () => {
it.each(KNIP_AUDITS.map((audit) => [audit.slug, audit]))(
'should be a valid %s audit meta info',
(_, audit) => {
expect(() => auditSchema.parse(audit)).not.toThrow();
},
);
});

describe('constants-KNIP_GROUPS', () => {
it('should be a valid file group info', () => {
expect(() => groupSchema.parse(KNIP_GROUP_FILES)).not.toThrow();
});

it('should be a valid exports group info', () => {
expect(() => groupSchema.parse(KNIP_GROUP_EXPORTS)).not.toThrow();
});

it('should be a valid dependencies group info', () => {
expect(() => groupSchema.parse(KNIP_GROUP_DEPENDENCIES)).not.toThrow();
});

it('should be a valid all group info', () => {
expect(() => groupSchema.parse(KNIP_GROUP_ALL)).not.toThrow();
});
});
186 changes: 186 additions & 0 deletions packages/plugin-knip/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,189 @@
import { Audit, Group } from '@code-pushup/models';

export const KNIP_PLUGIN_SLUG = 'knip';
export const KNIP_RAW_REPORT_NAME = 'knip-raw-report.json';
export const KNIP_REPORT_NAME = 'knip-code-pushup-report.json';

const audits = [
{
slug: 'files',
title: 'Unused Files',
description: 'Unable to find a reference to this file',
},
{
slug: 'dependencies',
title: 'Unused Dependencies',
description: 'Unable to find a reference to this dependency',
},
{
slug: 'devdependencies',
title: 'Unused Development Dependencies',
description: 'Unable to find a reference to this devDependency',
},
{
slug: 'optionalpeerdependencies',
title: 'Referenced optional peerDependencies',
description: 'Optional peer dependency is referenced',
},
{
slug: 'unlisted',
title: 'Unlisted dependencies',
description: 'Used dependencies not listed in package.json',
},
{
slug: 'binaries',
title: 'Unlisted binaries',
description: 'Binaries from dependencies not listed in package.json',
},
{
slug: 'unresolved',
title: 'Unresolved imports',
description: 'Unable to resolve this (import) specifier',
},
{
slug: 'exports',
title: 'Unused exports',
description: 'Unable to find a reference to this export',
},
{
slug: 'types',
title: 'Unused exported types',
description: 'Unable to find a reference to this exported type',
},
{
slug: 'nsexports',
title: 'Exports in used namespace',
description: 'Namespace with export is referenced, but not export itself',
},
{
slug: 'nstypes',
title: 'Exported types in used namespace',
description: 'Namespace with type is referenced, but not type itself',
},
{
slug: 'enummembers',
title: 'Unused exported enum members',
description: 'Unable to find a reference to this enum member',
},
{
slug: 'classmembers',
title: 'Unused exported class members',
description: 'Unable to find a reference to this class member',
},
{
slug: 'duplicates',
title: 'Duplicate exports',
description: 'This is exported more than once',
},
] as const satisfies Audit[]; // we use `as const satisfies` to get strict slug typing

export type KnipAudits = (typeof audits)[number]['slug'];

function docsLink(slug: KnipAudits): string {
let anchor = '#';
const base = 'https://knip.dev/guides/handling-issues';

switch (slug) {
case 'files':
anchor = '#unused-files';
break;
case 'dependencies':
case 'devdependencies':
anchor = '#unused-dependencies';
break;
case 'unlisted':
anchor = '#unlisted-dependencies';
break;
case 'optionalpeerdependencies':
anchor = '#referenced-optional-peerDependencies';
break;
case 'unresolved':
anchor = '#unresolved-imports';
break;
case 'exports':
case 'types':
case 'nsexports':
case 'nstypes':
anchor = '#unused-exports';
break;
case 'enummembers':
anchor = '#enum-members';
break;
case 'classmembers':
anchor = '#class-members';
break;
// following cases also default:
// - case 'binaries':
// - case 'duplicates':
default:
return base;
}

return `${base}${anchor}`;
}

export const KNIP_AUDITS = audits.map((audit) => ({
...audit,
docsUrl: docsLink(audit.slug),
}));

export const KNIP_GROUP_FILES = {
slug: 'files',
title: 'All file audits',
description: 'Groups all file related audits',
refs: [{ slug: 'files', weight: 1 }],
} as const satisfies Group;

export const KNIP_GROUP_DEPENDENCIES = {
slug: 'dependencies',
title: 'All dependency audits',
description: 'Groups all dependency related audits',
refs: [
{ slug: 'dependencies', weight: 1 },
{ slug: 'devdependencies', weight: 1 },
{ slug: 'binaries', weight: 1 },
// critical as potentially breaking
{ slug: 'optionalpeerdependencies', weight: 2 },
{ slug: 'unlisted', weight: 2 },
],
} as const satisfies Group;

export const KNIP_GROUP_EXPORTS = {
slug: 'exports',
title: 'All exports related audits',
description: 'Groups all dependency related knip audits',
refs: [
{ slug: 'unresolved', weight: 10 },
{ slug: 'exports', weight: 10 },
{ slug: 'types', weight: 10 },
{ slug: 'nsexports', weight: 10 },
{ slug: 'nstypes', weight: 10 },
{ slug: 'enummembers', weight: 10 },
{ slug: 'classmembers', weight: 10 },
{ slug: 'duplicates', weight: 2 },
],
} as const satisfies Group;

export const KNIP_GROUP_ALL = {
slug: 'all',
title: 'All knip audits',
description: 'Groups all knip audits into a group for easy use',
refs: [
...KNIP_GROUP_FILES.refs,
...KNIP_GROUP_EXPORTS.refs,
...KNIP_GROUP_DEPENDENCIES.refs,
],
} as const satisfies Group;

export const KNIP_GROUPS = [
KNIP_GROUP_FILES,
KNIP_GROUP_EXPORTS,
KNIP_GROUP_DEPENDENCIES,
KNIP_GROUP_ALL,
] as const satisfies Group[]; // we use `as const satisfies` to get strict slug typing;

export type KnipGroups = (typeof KNIP_GROUPS)[number]['slug'];

import { IssueType as KnipIssueType } from 'knip/dist/types/issues';

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/plugin-knip/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { knipPlugin } from './knip.plugin';

export { knipPlugin } from './knip.plugin';

export {
KNIP_GROUP_DEPENDENCIES,
KNIP_GROUP_FILES,
KNIP_GROUP_ALL,
KNIP_AUDITS,
KNIP_PLUGIN_SLUG,
KNIP_RAW_REPORT_NAME,
KNIP_REPORT_NAME,
} from './constants';

export { knipCategoryAuditRef, knipCategoryGroupRef } from './utils';
export default knipPlugin;
29 changes: 29 additions & 0 deletions packages/plugin-knip/src/lib/knip.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join } from 'node:path';
import { PluginConfig } from '@code-pushup/models';
import { KNIP_AUDITS, KNIP_GROUPS, KNIP_PLUGIN_SLUG } from './constants';
import { RunnerOptions, createRunnerConfig } from './runner';

export type PluginOptions = RunnerOptions;

export function knipPlugin(options: PluginOptions = {}): PluginConfig {
const {
outputFile = join(
'.code-pushup',
KNIP_PLUGIN_SLUG,
`knip-report-${Date.now()}.json`,
),
...runnerOptions
} = options;
return {
slug: KNIP_PLUGIN_SLUG,
title: 'Knip',
icon: 'folder-javascript',
description: 'A plugin to track dependencies and duplicates',
runner: createRunnerConfig({
...runnerOptions,
outputFile,
}),
audits: KNIP_AUDITS,
groups: KNIP_GROUPS,
};
}
21 changes: 21 additions & 0 deletions packages/plugin-knip/src/lib/knip.plugin.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest';
import { pluginConfigSchema } from '@code-pushup/models';
import { KNIP_AUDITS, KNIP_GROUPS, KNIP_PLUGIN_SLUG } from './constants';
import { knipPlugin } from './knip.plugin';

describe('knipPlugin-create-config-object', () => {
it('should return valid PluginConfig', () => {
const pluginConfig = knipPlugin({});
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow();
expect(pluginConfig).toEqual(
expect.objectContaining({
slug: KNIP_PLUGIN_SLUG,
title: 'Knip',
icon: 'folder-javascript',
description: 'A plugin to track dependencies and duplicates',
audits: KNIP_AUDITS,
groups: KNIP_GROUPS,
}),
);
});
});
Loading
Loading