Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Dec 25, 2024
1 parent 7c67433 commit 7ecb376
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 176 deletions.
6 changes: 3 additions & 3 deletions packages/plugin-typescript/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Audit, Group } from '@code-pushup/models';
import { camelCaseToKebabCase, formatSlugToTitle } from '@code-pushup/utils';
import { camelCaseToKebabCase, kebabCaseToSentence } from '@code-pushup/utils';
import {
GROUPS_DESCRIPTIONS,
TS_ERROR_CODES,
Expand All @@ -11,7 +11,7 @@ export const AUDITS = Object.values(TS_ERROR_CODES)
.flatMap(i => Object.entries(i))
.reduce<Audit[]>((audits, [name]) => {
const slug = camelCaseToKebabCase(name);
const title = formatSlugToTitle(name);
const title = kebabCaseToSentence(name);
return [
...audits,
{
Expand All @@ -32,7 +32,7 @@ const weights = {
export const GROUPS: Group[] = Object.entries(TS_ERROR_CODES).map(
([groupSlug, auditMap]) => ({
slug: camelCaseToKebabCase(groupSlug),
title: formatSlugToTitle(groupSlug),
title: kebabCaseToSentence(groupSlug),
description:
GROUPS_DESCRIPTIONS[groupSlug as keyof typeof GROUPS_DESCRIPTIONS],
refs: Object.keys(auditMap).map(audit => ({
Expand Down
20 changes: 11 additions & 9 deletions packages/plugin-typescript/src/lib/runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
flattenDiagnosticMessageText,
} from 'typescript';
import type { Issue } from '@code-pushup/models';
import { camelCaseToKebabCase } from '@code-pushup/utils';
import type { AuditSlug } from '../types.js';
import { camelCaseToKebabCase } from '../utils.js';
import { TS_ERROR_CODES } from './ts-error-codes.js';

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

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

return {
severity: getSeverity(diag.category),
message,
source: {
file: diag.file.fileName,
position: {
startLine,
},
...(startLine
? {
position: {
startLine,
},
}
: {}),
},
};
} satisfies Issue;
}
18 changes: 7 additions & 11 deletions packages/plugin-typescript/src/lib/runner/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { type Diagnostic, DiagnosticCategory } from 'typescript';
import { DiagnosticCategory } from 'typescript';
import { describe, expect } from 'vitest';
import {
getIssueFromDiagnostic,
getSeverity,
transformTSErrorCodeToAuditSlug,
tSCodeToAuditSlug,
} from './utils.js';

describe('transformTSErrorCodeToAuditSlug', () => {
describe('tSCodeToAuditSlug', () => {
it.each(Object.entries({}))(
'should transform supported code to readable audit',
(code, slug) => {
expect(transformTSErrorCodeToAuditSlug(Number.parseInt(code, 10))).toBe(
slug,
);
expect(tSCodeToAuditSlug(Number.parseInt(code, 10))).toBe(slug);
},
);

it('should throw error for unknown code', () => {
expect(() => transformTSErrorCodeToAuditSlug(1111)).toThrow(
'Code 1111 not supported.',
);
expect(() => tSCodeToAuditSlug(1111)).toThrow('Code 1111 not supported.');
});
});

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

describe('getIssueFromDiagnostic', () => {
const diagnositcMock = {
const diagnosticMock = {
code: 222,
category: DiagnosticCategory.Error,
messageText: "Type 'number' is not assignable to type 'string'.",
Expand Down Expand Up @@ -105,6 +101,6 @@ describe('getIssueFromDiagnostic', () => {
it('position.startLine should be 1 if start is undefined', () => {
diagnosticMock.start = undefined;
const result = getIssueFromDiagnostic(diagnosticMock);
expect(result.source.position.startLine).toBe(1);
expect(result.source.position?.startLine).toBe(1);
});
});
13 changes: 0 additions & 13 deletions packages/plugin-typescript/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,3 @@ export function filterGroupsByAuditSlug(slugs?: string[]) {
return true;
};
}

export function camelCaseToKebabCase(string: string): string {
return string
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]+/g, '-')
.toLowerCase();
}

export function formatTitle(description: string = '') {
return description
.replace(/-/g, ' ')
.replace(/\b\w/g, letter => letter.toUpperCase());
}
134 changes: 66 additions & 68 deletions packages/plugin-typescript/src/lib/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,76 @@
import { describe, expect, it } from 'vitest';
import type { Audit, Group } from '@code-pushup/models';
import { filterAuditsBySlug, filterGroupsByAuditSlug } from './utils';
import { filterAuditsBySlug, filterGroupsByAuditSlug } from './utils.js';

describe('utils', () => {
describe('filterAuditsBySlug', () => {
const mockAudits: Audit[] = [
{ slug: 'test-1', title: 'Test 1' },
{ slug: 'test-2', title: 'Test 2' },
{ slug: 'test-3', title: 'Test 3' },
];
describe('filterAuditsBySlug', () => {
const mockAudits: Audit[] = [
{ slug: 'test-1', title: 'Test 1' },
{ slug: 'test-2', title: 'Test 2' },
{ slug: 'test-3', title: 'Test 3' },
];

it.each([
[undefined, mockAudits, [true, true, true]],
[[], mockAudits, [true, true, true]],
[['test-1', 'test-2'], mockAudits, [true, true, false]],
])(
'should filter audits correctly when slugs is %p',
(slugs, audits, expected) => {
const filter = filterAuditsBySlug(slugs);
audits.forEach((audit, index) => {
expect(filter(audit)).toBe(expected[index]);
});
},
);
});
it.each([
[undefined, mockAudits, [true, true, true]],
[[], mockAudits, [true, true, true]],
[['test-1', 'test-2'], mockAudits, [true, true, false]],
])(
'should filter audits correctly when slugs is %p',
(slugs, audits, expected) => {
const filter = filterAuditsBySlug(slugs);
audits.forEach((audit, index) => {
expect(filter(audit)).toBe(expected[index]);
});
},
);
});

describe('filterGroupsByAuditSlug', () => {
const mockGroups: Group[] = [
{
slug: 'group-1',
title: 'Group 1',
refs: [
{ slug: 'audit-1', weight: 1 },
{ slug: 'audit-2', weight: 1 },
],
},
{
slug: 'group-2',
title: 'Group 2',
refs: [{ slug: 'audit-3', weight: 1 }],
},
{
slug: 'group-3',
title: 'Group 3',
refs: [
{ slug: 'audit-4', weight: 1 },
{ slug: 'audit-5', weight: 1 },
],
},
];
describe('filterGroupsByAuditSlug', () => {
const mockGroups: Group[] = [
{
slug: 'group-1',
title: 'Group 1',
refs: [
{ slug: 'audit-1', weight: 1 },
{ slug: 'audit-2', weight: 1 },
],
},
{
slug: 'group-2',
title: 'Group 2',
refs: [{ slug: 'audit-3', weight: 1 }],
},
{
slug: 'group-3',
title: 'Group 3',
refs: [
{ slug: 'audit-4', weight: 1 },
{ slug: 'audit-5', weight: 1 },
],
},
];

it.each(mockGroups)(
'should return true for group %# when no slugs provided',
group => {
const filter = filterGroupsByAuditSlug();
expect(filter(group)).toBe(true);
},
);
it.each(mockGroups)(
'should return true for group %# when no slugs provided',
group => {
const filter = filterGroupsByAuditSlug();
expect(filter(group)).toBe(true);
},
);

it.each(mockGroups)(
'should return true for group %# when empty slugs array provided',
group => {
const filter = filterGroupsByAuditSlug([]);
expect(filter(group)).toBe(true);
},
);
it.each(mockGroups)(
'should return true for group %# when empty slugs array provided',
group => {
const filter = filterGroupsByAuditSlug([]);
expect(filter(group)).toBe(true);
},
);

it.each([
[mockGroups[0], true],
[mockGroups[1], true],
[mockGroups[2], false],
])('should filter group %# by audit slugs', (group, expected) => {
const filter = filterGroupsByAuditSlug(['audit-1', 'audit-3']);
expect(filter(group!)).toBe(expected);
});
it.each([
[mockGroups[0], true],
[mockGroups[1], true],
[mockGroups[2], false],
])('should filter group %# by audit slugs', (group, expected) => {
const filter = filterGroupsByAuditSlug(['audit-1', 'audit-3']);
expect(filter(group!)).toBe(expected);
});
});
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export {
export { isSemver, normalizeSemver, sortSemvers } from './lib/semver.js';
export {
camelCaseToKebabCase,
formatSlugToTitle,
kebabCaseToSentence,
kebabCaseToCamelCase,
} from './lib/string.js';
export * from './lib/text-formats/index.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function camelCaseToKebabCase(string: string): string {
* @param slug - The slug to format.
* @returns The formatted title.
*/
export function formatSlugToTitle(slug: string = '') {
export function kebabCaseToSentence(slug: string = '') {
return slug
.replace(/-/g, ' ')
.replace(/\b\w/g, letter => letter.toUpperCase());
Expand Down
Loading

0 comments on commit 7ecb376

Please sign in to comment.