Skip to content

Commit ce4daa6

Browse files
committed
refactor static ts error data
1 parent 73232cd commit ce4daa6

File tree

13 files changed

+169
-321
lines changed

13 files changed

+169
-321
lines changed

code-pushup.preset.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,8 @@ import {
1717
type TypescriptPluginOptions,
1818
typescriptPlugin,
1919
} from './packages/plugin-typescript/src/index.js';
20-
import {
21-
BUILD_EMIT_OPTIONS,
22-
CONTROL_FLOW_OPTIONS,
23-
INTEROP_CONSTRAINTS,
24-
LANGUAGE_ENVIRONMENT_OPTIONS,
25-
MODULE_RESOLUTION,
26-
PROJECT_REFERENCES,
27-
STRICT_CHECKS,
28-
TYPE_CHECKING_BEHAVIOR,
29-
WATCH_OPTIONS
30-
} from './packages/plugin-typescript/src/lib/runner/known-ts-error-codes.js';
31-
import {filterAuditsBySlug, filterGroupsByAuditSlug} from './packages/plugin-typescript/src/lib/utils.js';
32-
import {GROUPS} from "./packages/plugin-typescript/src/lib/constants";
20+
import { GROUPS } from './packages/plugin-typescript/src/lib/constants';
21+
import { filterGroupsByAuditSlug } from './packages/plugin-typescript/src/lib/utils.js';
3322

3423
export const jsPackagesCategories: CategoryConfig[] = [
3524
{
@@ -88,14 +77,14 @@ export const eslintCategories: CategoryConfig[] = [
8877
slug: 'bug-prevention',
8978
title: 'Bug prevention',
9079
description: 'Lint rules that find **potential bugs** in your code.',
91-
refs: [{type: 'group', plugin: 'eslint', slug: 'problems', weight: 1}],
80+
refs: [{ type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 }],
9281
},
9382
{
9483
slug: 'code-style',
9584
title: 'Code style',
9685
description:
9786
'Lint rules that promote **good practices** and consistency in your code.',
98-
refs: [{type: 'group', plugin: 'eslint', slug: 'suggestions', weight: 1}],
87+
refs: [{ type: 'group', plugin: 'eslint', slug: 'suggestions', weight: 1 }],
9988
},
10089
];
10190

@@ -160,7 +149,7 @@ export const typescriptPluginConfigNx = async (
160149
slug: 'typescript',
161150
title: 'Typescript',
162151
refs: GROUPS.filter(filterGroupsByAuditSlug(opt.onlyAudits)).map(
163-
({slug}) => ({
152+
({ slug }) => ({
164153
plugin: 'typescript',
165154
type: 'group' as const,
166155
slug,

packages/plugin-typescript/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ For more infos visit the [official docs](https://developer.chrome.com/docs/types
4242
plugins: [
4343
// ...
4444
await typescriptPlugin({
45-
tsConfigPath: './tsconfig.json'
45+
tsConfigPath: './tsconfig.json',
4646
}),
4747
],
4848
};
@@ -64,9 +64,7 @@ import { typescriptGroupRef } from './utils';
6464

6565
export default {
6666
// ...
67-
categories: [
68-
69-
],
67+
categories: [],
7068
};
7169
```
7270

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from 'zod';
2+
import { AUDITS } from './constants.js';
23
import type { AuditSlug } from './types.js';
3-
import {AUDITS} from "./constants.js";
44

55
const auditSlugs = AUDITS.map(({ slug }) => slug) as [string, ...string[]];
66
export const typescriptPluginConfigSchema = z.object({
Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,38 @@
1-
import {
2-
BUILD_EMIT_OPTIONS,
3-
CONTROL_FLOW_OPTIONS,
4-
INTEROP_CONSTRAINTS,
5-
LANGUAGE_ENVIRONMENT_OPTIONS,
6-
MODULE_RESOLUTION,
7-
PROJECT_REFERENCES,
8-
STRICT_CHECKS,
9-
TYPE_CHECKING_BEHAVIOR,
10-
WATCH_OPTIONS
11-
} from './runner/ts-error-codes.js';
12-
import type {Audit, Group} from "@code-pushup/models";
13-
import {formatTitle, kebabCaseToCamelCase} from "./utils.js";
1+
import type { Audit, Group } from '@code-pushup/models';
2+
import { TS_ERROR_CODES } from './runner/ts-error-codes.js';
3+
import { camelCaseToKebabCase, formatTitle } from './utils.js';
144

155
export const TYPESCRIPT_PLUGIN_SLUG = 'typescript';
166

17-
export const AUDITS = [
18-
STRICT_CHECKS, BUILD_EMIT_OPTIONS,
19-
CONTROL_FLOW_OPTIONS, TYPE_CHECKING_BEHAVIOR,
20-
MODULE_RESOLUTION, PROJECT_REFERENCES,
21-
WATCH_OPTIONS, INTEROP_CONSTRAINTS, LANGUAGE_ENVIRONMENT_OPTIONS
22-
].flatMap(i => Object.entries(i)).reduce<Audit[]>(
23-
(audits, [slug]) => {
24-
const anchorText = kebabCaseToCamelCase(slug);
25-
const title = formatTitle(slug);
7+
export const AUDITS = Object.values(TS_ERROR_CODES)
8+
.flatMap(i => Object.entries(i))
9+
.reduce<Audit[]>((audits, [name]) => {
10+
const slug = camelCaseToKebabCase(name);
11+
const title = formatTitle(name);
2612
return [
2713
...audits,
2814
{
2915
slug,
3016
title,
31-
docsUrl: `https://www.typescriptlang.org/tsconfig/#${anchorText}`
32-
}];
17+
docsUrl: `https://www.typescriptlang.org/tsconfig/#${name}`,
18+
},
19+
];
3320
}, []);
3421

35-
export const GROUPS: Group[] = Object.entries({
36-
'strict-checks': Object.keys(STRICT_CHECKS).map((slug) => ({slug, weight: 3})),
37-
'type-checking-behavior': Object.keys(TYPE_CHECKING_BEHAVIOR).map((slug) => ({slug, weight: 2})),
38-
'control-flow-options': Object.keys(CONTROL_FLOW_OPTIONS).map((slug) => ({slug, weight: 2})),
39-
'interop-constraints': Object.keys(INTEROP_CONSTRAINTS).map((slug) => ({slug, weight: 2})),
40-
'module-resolution': Object.keys(MODULE_RESOLUTION).map((slug) => ({slug, weight: 2})),
41-
'build-emit-options': Object.keys(BUILD_EMIT_OPTIONS).map((slug) => ({slug, weight: 1})),
42-
'project-references': Object.keys(PROJECT_REFERENCES).map((slug) => ({slug, weight: 1})),
43-
'watch-options': Object.keys(WATCH_OPTIONS).map((slug) => ({slug, weight: 1})),
44-
'language-environment-options': Object.keys(LANGUAGE_ENVIRONMENT_OPTIONS).map((slug) => ({slug, weight: 1}))
45-
})
46-
.reduce((groups, [slug, refs]) => {
47-
const group: Group = {
48-
slug,
49-
title: formatTitle(slug),
50-
refs
51-
};
52-
return [
53-
...groups,
54-
group
55-
]
56-
}, [] as Group[]);
22+
const weights = {
23+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
24+
strictChecks: 3,
25+
typeCheckingBehavior: 2,
26+
controlFlowOptions: 2,
27+
interopConstraints: 2,
28+
};
29+
export const GROUPS: Group[] = Object.entries(TS_ERROR_CODES).map(
30+
([groupSlug, auditMap]) => ({
31+
slug: camelCaseToKebabCase(groupSlug),
32+
title: formatTitle(groupSlug),
33+
refs: Object.keys(auditMap).map(audit => ({
34+
slug: camelCaseToKebabCase(audit),
35+
weight: weights[audit as keyof typeof weights] ?? 1,
36+
})),
37+
}),
38+
);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
RunnerFunction,
99
} from '@code-pushup/models';
1010
import type { TypescriptPluginOptions } from '../config.js';
11+
import { AUDITS } from '../constants.js';
1112
import type { AuditSlug } from '../types.js';
1213
import { filterAuditsBySlug } from '../utils.js';
1314
import { getDiagnostics } from './typescript-runner.js';
@@ -16,10 +17,9 @@ import {
1617
getIssueFromDiagnostic,
1718
transformTSErrorCodeToAuditSlug,
1819
} from './utils.js';
19-
import {AUDITS} from "../constants.js";
2020

2121
export function createRunnerFunction(
22-
options: TypescriptPluginOptions & { audits: Audit[]},
22+
options: TypescriptPluginOptions & { audits: Audit[] },
2323
): RunnerFunction {
2424
return async (): Promise<AuditOutputs> => {
2525
const diagnostics = await getDiagnostics(options);
@@ -34,7 +34,7 @@ export function createRunnerFunction(
3434
category === DiagnosticCategory.Error,
3535
)
3636
// filter out unsupported errors
37-
.filter(({code}) => AUDIT_LOOKUP.get(code) !== undefined)
37+
.filter(({ code }) => AUDIT_LOOKUP.get(code) !== undefined)
3838
.reduce(
3939
(acc, diag) => {
4040
const slug = transformTSErrorCodeToAuditSlug(diag.code);
@@ -59,9 +59,7 @@ export function createRunnerFunction(
5959
>,
6060
);
6161

62-
return AUDITS
63-
.filter(filterAuditsBySlug(options.onlyAudits))
64-
.map(audit => {
62+
return AUDITS.filter(filterAuditsBySlug(options.onlyAudits)).map(audit => {
6563
const { details } = result[audit.slug as AuditSlug] ?? {};
6664
const issues = details?.issues ?? [];
6765
return {

0 commit comments

Comments
 (0)