Skip to content

Commit 7ecb376

Browse files
committed
wip
1 parent 7c67433 commit 7ecb376

File tree

8 files changed

+165
-176
lines changed

8 files changed

+165
-176
lines changed

packages/plugin-typescript/src/lib/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Audit, Group } from '@code-pushup/models';
2-
import { camelCaseToKebabCase, formatSlugToTitle } from '@code-pushup/utils';
2+
import { camelCaseToKebabCase, kebabCaseToSentence } from '@code-pushup/utils';
33
import {
44
GROUPS_DESCRIPTIONS,
55
TS_ERROR_CODES,
@@ -11,7 +11,7 @@ export const AUDITS = Object.values(TS_ERROR_CODES)
1111
.flatMap(i => Object.entries(i))
1212
.reduce<Audit[]>((audits, [name]) => {
1313
const slug = camelCaseToKebabCase(name);
14-
const title = formatSlugToTitle(name);
14+
const title = kebabCaseToSentence(name);
1515
return [
1616
...audits,
1717
{
@@ -32,7 +32,7 @@ const weights = {
3232
export const GROUPS: Group[] = Object.entries(TS_ERROR_CODES).map(
3333
([groupSlug, auditMap]) => ({
3434
slug: camelCaseToKebabCase(groupSlug),
35-
title: formatSlugToTitle(groupSlug),
35+
title: kebabCaseToSentence(groupSlug),
3636
description:
3737
GROUPS_DESCRIPTIONS[groupSlug as keyof typeof GROUPS_DESCRIPTIONS],
3838
refs: Object.keys(auditMap).map(audit => ({

packages/plugin-typescript/src/lib/runner/utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
flattenDiagnosticMessageText,
55
} from 'typescript';
66
import type { Issue } from '@code-pushup/models';
7+
import { camelCaseToKebabCase } from '@code-pushup/utils';
78
import type { AuditSlug } from '../types.js';
8-
import { camelCaseToKebabCase } from '../utils.js';
99
import { TS_ERROR_CODES } from './ts-error-codes.js';
1010

1111
/** Build Reverse Lookup Map. It will a map with key as the error code and value as the audit slug. */
@@ -58,9 +58,7 @@ export function getSeverity(category: DiagnosticCategory): Issue['severity'] {
5858
* @returns The issue.
5959
* @throws Error if the diagnostic is global (e.g., invalid compiler option).
6060
*/
61-
export function getIssueFromDiagnostic(
62-
diag: Diagnostic,
63-
): Omit<Issue, 'source'> & { source: Required<NonNullable<Issue['source']>> } {
61+
export function getIssueFromDiagnostic(diag: Diagnostic) {
6462
const message = `${flattenDiagnosticMessageText(diag.messageText, '\n')}`;
6563

6664
// If undefined, the error might be global (e.g., invalid compiler option).
@@ -71,16 +69,20 @@ export function getIssueFromDiagnostic(
7169
const startLine =
7270
diag.start !== undefined
7371
? diag.file.getLineAndCharacterOfPosition(diag.start).line + 1
74-
: 1;
72+
: undefined;
7573

7674
return {
7775
severity: getSeverity(diag.category),
7876
message,
7977
source: {
8078
file: diag.file.fileName,
81-
position: {
82-
startLine,
83-
},
79+
...(startLine
80+
? {
81+
position: {
82+
startLine,
83+
},
84+
}
85+
: {}),
8486
},
85-
};
87+
} satisfies Issue;
8688
}

packages/plugin-typescript/src/lib/runner/utils.unit.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
import { type Diagnostic, DiagnosticCategory } from 'typescript';
1+
import { DiagnosticCategory } from 'typescript';
22
import { describe, expect } from 'vitest';
33
import {
44
getIssueFromDiagnostic,
55
getSeverity,
6-
transformTSErrorCodeToAuditSlug,
6+
tSCodeToAuditSlug,
77
} from './utils.js';
88

9-
describe('transformTSErrorCodeToAuditSlug', () => {
9+
describe('tSCodeToAuditSlug', () => {
1010
it.each(Object.entries({}))(
1111
'should transform supported code to readable audit',
1212
(code, slug) => {
13-
expect(transformTSErrorCodeToAuditSlug(Number.parseInt(code, 10))).toBe(
14-
slug,
15-
);
13+
expect(tSCodeToAuditSlug(Number.parseInt(code, 10))).toBe(slug);
1614
},
1715
);
1816

1917
it('should throw error for unknown code', () => {
20-
expect(() => transformTSErrorCodeToAuditSlug(1111)).toThrow(
21-
'Code 1111 not supported.',
22-
);
18+
expect(() => tSCodeToAuditSlug(1111)).toThrow('Code 1111 not supported.');
2319
});
2420
});
2521

@@ -39,7 +35,7 @@ describe('getSeverity', () => {
3935
});
4036

4137
describe('getIssueFromDiagnostic', () => {
42-
const diagnositcMock = {
38+
const diagnosticMock = {
4339
code: 222,
4440
category: DiagnosticCategory.Error,
4541
messageText: "Type 'number' is not assignable to type 'string'.",
@@ -105,6 +101,6 @@ describe('getIssueFromDiagnostic', () => {
105101
it('position.startLine should be 1 if start is undefined', () => {
106102
diagnosticMock.start = undefined;
107103
const result = getIssueFromDiagnostic(diagnosticMock);
108-
expect(result.source.position.startLine).toBe(1);
104+
expect(result.source.position?.startLine).toBe(1);
109105
});
110106
});

packages/plugin-typescript/src/lib/utils.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,3 @@ export function filterGroupsByAuditSlug(slugs?: string[]) {
1717
return true;
1818
};
1919
}
20-
21-
export function camelCaseToKebabCase(string: string): string {
22-
return string
23-
.replace(/([a-z])([A-Z])/g, '$1-$2')
24-
.replace(/[\s_]+/g, '-')
25-
.toLowerCase();
26-
}
27-
28-
export function formatTitle(description: string = '') {
29-
return description
30-
.replace(/-/g, ' ')
31-
.replace(/\b\w/g, letter => letter.toUpperCase());
32-
}
Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,76 @@
11
import { describe, expect, it } from 'vitest';
22
import type { Audit, Group } from '@code-pushup/models';
3-
import { filterAuditsBySlug, filterGroupsByAuditSlug } from './utils';
3+
import { filterAuditsBySlug, filterGroupsByAuditSlug } from './utils.js';
44

5-
describe('utils', () => {
6-
describe('filterAuditsBySlug', () => {
7-
const mockAudits: Audit[] = [
8-
{ slug: 'test-1', title: 'Test 1' },
9-
{ slug: 'test-2', title: 'Test 2' },
10-
{ slug: 'test-3', title: 'Test 3' },
11-
];
5+
describe('filterAuditsBySlug', () => {
6+
const mockAudits: Audit[] = [
7+
{ slug: 'test-1', title: 'Test 1' },
8+
{ slug: 'test-2', title: 'Test 2' },
9+
{ slug: 'test-3', title: 'Test 3' },
10+
];
1211

13-
it.each([
14-
[undefined, mockAudits, [true, true, true]],
15-
[[], mockAudits, [true, true, true]],
16-
[['test-1', 'test-2'], mockAudits, [true, true, false]],
17-
])(
18-
'should filter audits correctly when slugs is %p',
19-
(slugs, audits, expected) => {
20-
const filter = filterAuditsBySlug(slugs);
21-
audits.forEach((audit, index) => {
22-
expect(filter(audit)).toBe(expected[index]);
23-
});
24-
},
25-
);
26-
});
12+
it.each([
13+
[undefined, mockAudits, [true, true, true]],
14+
[[], mockAudits, [true, true, true]],
15+
[['test-1', 'test-2'], mockAudits, [true, true, false]],
16+
])(
17+
'should filter audits correctly when slugs is %p',
18+
(slugs, audits, expected) => {
19+
const filter = filterAuditsBySlug(slugs);
20+
audits.forEach((audit, index) => {
21+
expect(filter(audit)).toBe(expected[index]);
22+
});
23+
},
24+
);
25+
});
2726

28-
describe('filterGroupsByAuditSlug', () => {
29-
const mockGroups: Group[] = [
30-
{
31-
slug: 'group-1',
32-
title: 'Group 1',
33-
refs: [
34-
{ slug: 'audit-1', weight: 1 },
35-
{ slug: 'audit-2', weight: 1 },
36-
],
37-
},
38-
{
39-
slug: 'group-2',
40-
title: 'Group 2',
41-
refs: [{ slug: 'audit-3', weight: 1 }],
42-
},
43-
{
44-
slug: 'group-3',
45-
title: 'Group 3',
46-
refs: [
47-
{ slug: 'audit-4', weight: 1 },
48-
{ slug: 'audit-5', weight: 1 },
49-
],
50-
},
51-
];
27+
describe('filterGroupsByAuditSlug', () => {
28+
const mockGroups: Group[] = [
29+
{
30+
slug: 'group-1',
31+
title: 'Group 1',
32+
refs: [
33+
{ slug: 'audit-1', weight: 1 },
34+
{ slug: 'audit-2', weight: 1 },
35+
],
36+
},
37+
{
38+
slug: 'group-2',
39+
title: 'Group 2',
40+
refs: [{ slug: 'audit-3', weight: 1 }],
41+
},
42+
{
43+
slug: 'group-3',
44+
title: 'Group 3',
45+
refs: [
46+
{ slug: 'audit-4', weight: 1 },
47+
{ slug: 'audit-5', weight: 1 },
48+
],
49+
},
50+
];
5251

53-
it.each(mockGroups)(
54-
'should return true for group %# when no slugs provided',
55-
group => {
56-
const filter = filterGroupsByAuditSlug();
57-
expect(filter(group)).toBe(true);
58-
},
59-
);
52+
it.each(mockGroups)(
53+
'should return true for group %# when no slugs provided',
54+
group => {
55+
const filter = filterGroupsByAuditSlug();
56+
expect(filter(group)).toBe(true);
57+
},
58+
);
6059

61-
it.each(mockGroups)(
62-
'should return true for group %# when empty slugs array provided',
63-
group => {
64-
const filter = filterGroupsByAuditSlug([]);
65-
expect(filter(group)).toBe(true);
66-
},
67-
);
60+
it.each(mockGroups)(
61+
'should return true for group %# when empty slugs array provided',
62+
group => {
63+
const filter = filterGroupsByAuditSlug([]);
64+
expect(filter(group)).toBe(true);
65+
},
66+
);
6867

69-
it.each([
70-
[mockGroups[0], true],
71-
[mockGroups[1], true],
72-
[mockGroups[2], false],
73-
])('should filter group %# by audit slugs', (group, expected) => {
74-
const filter = filterGroupsByAuditSlug(['audit-1', 'audit-3']);
75-
expect(filter(group!)).toBe(expected);
76-
});
68+
it.each([
69+
[mockGroups[0], true],
70+
[mockGroups[1], true],
71+
[mockGroups[2], false],
72+
])('should filter group %# by audit slugs', (group, expected) => {
73+
const filter = filterGroupsByAuditSlug(['audit-1', 'audit-3']);
74+
expect(filter(group!)).toBe(expected);
7775
});
7876
});

packages/utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export {
9797
export { isSemver, normalizeSemver, sortSemvers } from './lib/semver.js';
9898
export {
9999
camelCaseToKebabCase,
100-
formatSlugToTitle,
100+
kebabCaseToSentence,
101101
kebabCaseToCamelCase,
102102
} from './lib/string.js';
103103
export * from './lib/text-formats/index.js';

packages/utils/src/lib/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function camelCaseToKebabCase(string: string): string {
3232
* @param slug - The slug to format.
3333
* @returns The formatted title.
3434
*/
35-
export function formatSlugToTitle(slug: string = '') {
35+
export function kebabCaseToSentence(slug: string = '') {
3636
return slug
3737
.replace(/-/g, ' ')
3838
.replace(/\b\w/g, letter => letter.toUpperCase());

0 commit comments

Comments
 (0)