Skip to content

Commit d5b2221

Browse files
committed
refactor: wip
1 parent 3a4b527 commit d5b2221

4 files changed

Lines changed: 88 additions & 63 deletions

File tree

e2e/plugin-knip-e2e/tests/reporter.e2e.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('knip reporter', () => {
2525
'lib',
2626
'reporter.js',
2727
);
28+
2829
beforeAll(async () => {
2930
await cp(fixturesDir, testFileDir, { recursive: true });
3031
await restoreNxIgnoredFiles(testFileDir);

packages/plugin-knip/src/lib/reporter/__snapshots__/reporter.unit.test.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ exports[`knipReporter > should produce valid audit outputs 1`] = `
4747
],
4848
},
4949
"score": 0,
50-
"slug": "devdependencies",
50+
"slug": "dev-dependencies",
5151
"value": 1,
5252
},
5353
{
@@ -63,7 +63,7 @@ exports[`knipReporter > should produce valid audit outputs 1`] = `
6363
],
6464
},
6565
"score": 0,
66-
"slug": "optionalpeerdependencies",
66+
"slug": "optional-peer-dependencies",
6767
"value": 1,
6868
},
6969
{
@@ -136,7 +136,7 @@ exports[`knipReporter > should produce valid audit outputs 1`] = `
136136
},
137137
{
138138
"score": 1,
139-
"slug": "nsexports",
139+
"slug": "ns-exports",
140140
"value": 0,
141141
},
142142
{
@@ -161,7 +161,7 @@ exports[`knipReporter > should produce valid audit outputs 1`] = `
161161
},
162162
{
163163
"score": 1,
164-
"slug": "nstypes",
164+
"slug": "ns-types",
165165
"value": 0,
166166
},
167167
{
@@ -181,7 +181,7 @@ exports[`knipReporter > should produce valid audit outputs 1`] = `
181181
],
182182
},
183183
"score": 0,
184-
"slug": "enummembers",
184+
"slug": "enum-members",
185185
"value": 1,
186186
},
187187
{
@@ -201,7 +201,7 @@ exports[`knipReporter > should produce valid audit outputs 1`] = `
201201
],
202202
},
203203
"score": 0,
204-
"slug": "classmembers",
204+
"slug": "class-members",
205205
"value": 1,
206206
},
207207
{

packages/plugin-knip/src/lib/reporter/utils.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ import type {
1313
IssueSeverity as CondPushupIssueSeverity,
1414
Issue as CpIssue,
1515
} from '@code-pushup/models';
16-
import { formatGitPath, getGitRoot } from '@code-pushup/utils';
16+
import {
17+
formatGitPath,
18+
getGitRoot,
19+
slugify,
20+
toSentenceCase,
21+
} from '@code-pushup/utils';
1722
import {
1823
ISSUE_RECORDS_TYPES,
1924
ISSUE_SET_TYPES,
2025
ISSUE_TYPES,
2126
} from '../constants.js';
22-
import {
23-
ISSUE_TYPE_MESSAGE,
24-
ISSUE_TYPE_TITLE,
25-
ISSUE_TYPE_TO_SLUG,
26-
} from './constants.js';
27+
import { ISSUE_TYPE_MESSAGE } from './constants.js';
2728

2829
const severityMap: Record<KnipSeverity | 'unknown', CondPushupIssueSeverity> = {
2930
unknown: 'info',
@@ -129,22 +130,15 @@ export function knipToCpReport({
129130
issues: rawIssues,
130131
report,
131132
}: Pick<ReporterOptions, 'issues' | 'report'>): Promise<AuditOutputs> {
132-
// When used as a knip reporter, always return ALL audits to match plugin configuration
133-
// When called directly (tests), filter based on what's provided
134-
const shouldReturnAllAudits = report != null;
135-
const issueTypesToInclude = shouldReturnAllAudits
136-
? ISSUE_TYPES
137-
: ISSUE_TYPES.filter((issueType) => rawIssues[issueType] != null);
138-
139133
return Promise.all(
140-
issueTypesToInclude.map(async (issueType): Promise<AuditOutput> => {
141-
// Only process issues if this type is enabled in the report (or if no report filter)
134+
ISSUE_TYPES.map(async (issueType): Promise<AuditOutput> => {
135+
// Only process issues if this type is enabled in the report
142136
const isEnabled = !report || report[issueType];
143137
const issues = isEnabled ? await toIssues(issueType, rawIssues) : [];
144138

145139
return {
146-
slug: ISSUE_TYPE_TO_SLUG[issueType],
147-
score: issues.length === 0 ? 1 : 0,
140+
slug: slugify(toSentenceCase(issueType)),
141+
score: issues.length === 0 && isEnabled ? 1 : 0,
148142
value: issues.length,
149143
...(issues.length > 0 ? { details: { issues } } : {}),
150144
};

packages/plugin-knip/src/lib/reporter/utils.unit.test.ts

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ describe('toIssues', () => {
264264
files: new Set([
265265
'/User/projects/code-pushup-cli/packages/utils/src/index.js',
266266
]),
267-
}),
267+
} as KnipIssues),
268268
).resolves.toStrictEqual([
269269
expect.objectContaining({
270270
message: expect.stringMatching('Unused file'),
@@ -294,10 +294,10 @@ describe('toIssues', () => {
294294
'/User/projects/code-pushup-cli/packages/utils/src/index.js',
295295
symbol: 'CliUi',
296296
severity: 'error',
297-
},
297+
} as unknown as KnipIssue,
298298
},
299299
},
300-
}),
300+
} as unknown as KnipIssues),
301301
).resolves.toStrictEqual([
302302
expect.objectContaining({
303303
message: expect.stringMatching('CliUi'),
@@ -312,59 +312,88 @@ describe('toIssues', () => {
312312

313313
describe('knipToCpReport', () => {
314314
it('should return empty audits if no report is flagged positive', async () => {
315-
await expect(
316-
knipToCpReport({
317-
issues: {},
318-
} as ReporterOptions),
319-
).resolves.toStrictEqual([]);
315+
const result = await knipToCpReport({
316+
issues: {},
317+
report: {},
318+
} as ReporterOptions);
319+
320+
expect(result).toEqual(
321+
expect.arrayContaining([expect.objectContaining({ score: 0, value: 0 })]),
322+
);
320323
});
321324

322325
it('should return only audits flagged in report object', async () => {
323-
await expect(
324-
knipToCpReport({
325-
issues: {
326-
dependencies: {},
327-
},
328-
} as ReporterOptions),
329-
).resolves.toStrictEqual(
326+
const result = await knipToCpReport({
327+
issues: {
328+
dependencies: {},
329+
},
330+
report: { dependencies: true },
331+
} as ReporterOptions);
332+
333+
expect(result).toEqual(
330334
expect.arrayContaining([
331335
expect.objectContaining({ slug: 'dependencies' }),
332336
]),
333337
);
334338
});
335339

340+
it('should return readable audit slug derived from knip issueType', async () => {
341+
const result = await knipToCpReport({
342+
issues: {
343+
optionalPeerDependencies: {},
344+
},
345+
report: { optionalPeerDependencies: true },
346+
} as ReporterOptions);
347+
348+
expect(result).toEqual(
349+
expect.arrayContaining([
350+
expect.objectContaining({ slug: 'optional-peer-dependencies' }),
351+
]),
352+
);
353+
});
354+
336355
it('should return audit result with number of issues as value', async () => {
337-
await expect(
338-
knipToCpReport({
339-
issues: { files: new Set(['a.js', 'b.js', 'c.js']) },
340-
} as ReporterOptions),
341-
).resolves.toStrictEqual([expect.objectContaining({ value: 3 })]);
356+
const result = await knipToCpReport({
357+
issues: { files: new Set(['a.js', 'b.js', 'c.js']) },
358+
report: { files: true },
359+
} as ReporterOptions);
360+
361+
expect(result).toEqual(
362+
expect.arrayContaining([
363+
expect.objectContaining({ slug: 'files', value: 3 }),
364+
]),
365+
);
342366
});
343367

344368
it('should return audit result without display value', async () => {
345-
await expect(
346-
knipToCpReport({
347-
issues: { files: new Set(['main.js']) },
348-
} as ReporterOptions),
349-
).resolves.toStrictEqual([
350-
expect.not.objectContaining({ displayValue: expect.any(String) }),
351-
]);
369+
const result = await knipToCpReport({
370+
issues: { files: new Set(['main.js']) },
371+
report: { files: true },
372+
} as ReporterOptions);
373+
374+
// Check that the files audit doesn't have displayValue
375+
const filesAudit = result.find((audit) => audit.slug === 'files');
376+
expect(filesAudit).not.toHaveProperty('displayValue');
352377
});
353378

354379
it('should score audits with empty issues with 1', async () => {
355-
await expect(
356-
knipToCpReport({
357-
issues: { files: new Set() },
358-
} as ReporterOptions),
359-
).resolves.toStrictEqual([expect.objectContaining({ score: 1 })]);
380+
const result = await knipToCpReport({
381+
issues: { files: new Set() },
382+
report: { files: true },
383+
} as ReporterOptions);
384+
385+
const filesAudit = result.find((audit) => audit.slug === 'files');
386+
expect(filesAudit).toEqual(expect.objectContaining({ score: 1, value: 0 }));
360387
});
361388

362389
it('should score audits with issues with 0', async () => {
363-
await expect(
364-
knipToCpReport({
365-
issues: { files: new Set(['main.js']) },
366-
} as ReporterOptions),
367-
).resolves.toStrictEqual([expect.objectContaining({ score: 0 })]);
390+
const result = await knipToCpReport({
391+
issues: { files: new Set(['main.js']) },
392+
report: { files: true },
393+
} as ReporterOptions);
394+
395+
const filesAudit = result.find((audit) => audit.slug === 'files');
396+
expect(filesAudit).toEqual(expect.objectContaining({ score: 0, value: 1 }));
368397
});
369398

370399
it('should return valid outputs schema', async () => {
@@ -384,7 +413,7 @@ describe('knipToCpReport', () => {
384413
col: 0,
385414
symbols: [],
386415
parentSymbol: '',
387-
},
416+
} as unknown as KnipIssue,
388417
},
389418
'/User/username/code-pushup-cli/examples/plugins/.eslintrc.json': {
390419
'jsonc-eslint-parser': {
@@ -393,11 +422,12 @@ describe('knipToCpReport', () => {
393422
filePath:
394423
'/User/username/code-pushup-cli/packages/utils/package.json',
395424
severity: 'error',
396-
},
425+
} as unknown as KnipIssue,
397426
},
398427
} as IssueRecords,
399428
} as ReporterOptions['issues'],
400-
});
429+
report: { files: true, unlisted: true },
430+
} as ReporterOptions);
401431
expect(() => auditOutputsSchema.parse(result)).not.toThrowError();
402432
});
403433
});

0 commit comments

Comments
 (0)