Skip to content

Commit

Permalink
Fix up rules in issue collector and reporter (resolves #713)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jul 4, 2024
1 parent 4c4e3e1 commit 7775213
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/knip/fixtures/rules/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const used = 1;
export const unused = 1;

export type UsedType = unknown;
export type UnusedType = unknown;

export default used;

export class MyClass {
unused: 1;
}

export enum MyEnum {
used = 1,
unused = 1,
}
10 changes: 10 additions & 0 deletions packages/knip/fixtures/rules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'used';
import '@dev/used';
import 'optional-peer-dep';
import './unresolved';
import 'unlisted';
import * as NS from './ns';
import default_, { used, type UsedType, MyClass, MyEnum } from './exports';

const x: UsedType | NS.UsedType = [default_, used, MyEnum.used, NS.used];
const y = new MyClass();
5 changes: 5 additions & 0 deletions packages/knip/fixtures/rules/ns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const used = 1;
export const unused = 1;

export type UsedType = unknown;
export type UnusedType = unknown;
41 changes: 41 additions & 0 deletions packages/knip/fixtures/rules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@fixtures/rules",
"dependencies": {
"used": "*",
"unused": "*"
},
"devDependencies": {
"@dev/used": "*",
"@dev/unused": "*"
},
"peerDependencies": {
"optional-peer-dep": "*"
},
"peerDependenciesMeta": {
"optional-peer-dep": {
"optional": true
}
},
"scripts": {
"unlisted-binary": "unlisted"
},
"knip": {
"include": ["classMembers", "nsExports", "nsTypes"],
"rules": {
"files": "warn",
"dependencies": "warn",
"devDependencies": "warn",
"optionalPeerDependencies": "warn",
"unlisted": "warn",
"binaries": "warn",
"unresolved": "warn",
"exports": "warn",
"types": "warn",
"nsExports": "warn",
"nsTypes": "warn",
"duplicates": "warn",
"enumMembers": "warn",
"classMembers": "warn"
}
}
}
Empty file.
1 change: 1 addition & 0 deletions packages/knip/src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const issueTypeSchema = z.union([
z.literal('files'),
z.literal('dependencies'),
z.literal('devDependencies'),
z.literal('optionalPeerDependencies'),
z.literal('unlisted'),
z.literal('binaries'),
z.literal('unresolved'),
Expand Down
4 changes: 3 additions & 1 deletion packages/knip/src/IssueCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ export class IssueCollector {
if (this.filters.dir && !filePath.startsWith(`${this.filters.dir}/`)) continue;
if (this.referencedFiles.has(filePath)) continue;
if (this.isMatch(filePath)) continue;

this.issues.files.add(filePath);
// @ts-expect-error TODO Fix up in next major
this.issues._files.add({ type: 'files', filePath, symbol: relative(filePath) });
this.issues._files.add({ type: 'files', filePath, symbol: relative(filePath), severity: this.rules.files });

this.counters.files++;
this.counters.processed++;
}
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/reporters/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default ({ report, issues, tagHints, configurationHints, noConfigHints, i
for (const issue of issuesForType) {
const relPath = toRelative(issue.filePath);
if (issue.isFixed) console.log(picocolors.gray(`${relPath} (deleted)`));
else if (issue.severity === 'warn') console.log(picocolors.gray(relPath));
else console.log(relPath);
}
totalIssues = totalIssues + issuesForType.length;
Expand Down
38 changes: 38 additions & 0 deletions packages/knip/test/rules.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../src/index.js';
import { getValuesByKeyDeep } from '../src/util/object.js';
import { resolve } from '../src/util/path.js';
import baseArguments from './helpers/baseArguments.js';
import baseCounters from './helpers/baseCounters.js';

const cwd = resolve('fixtures/rules');

test('Respect warnings in rules', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

const severities = getValuesByKeyDeep(issues, 'severity');

assert(severities.every(severity => severity === 'warn'));

assert.deepEqual(counters, {
...baseCounters,
files: 1,
dependencies: 1,
devDependencies: 1,
optionalPeerDependencies: 1,
unlisted: 1,
binaries: 1,
unresolved: 1,
exports: 2,
types: 2,
duplicates: 1,
enumMembers: 1,
classMembers: 1,
processed: 4,
total: 4,
});
});

0 comments on commit 7775213

Please sign in to comment.