-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgroups.ts
More file actions
168 lines (150 loc) · 4.86 KB
/
groups.ts
File metadata and controls
168 lines (150 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type { Rule } from 'eslint';
import type { Group, GroupRef } from '@code-pushup/models';
import { logger, objectToKeys, slugify } from '@code-pushup/utils';
import type { CustomGroup } from '../config.js';
import { ruleToSlug } from './hash.js';
import { type RuleData, parseRuleId } from './parse.js';
import { expandWildcardRules } from './rules.js';
type RuleType = NonNullable<Rule.RuleMetaData['type']>;
// docs on meta.type: https://eslint.org/docs/latest/extend/custom-rules#rule-structure
const typeGroups: Record<RuleType, Omit<Group, 'refs'>> = {
problem: {
slug: 'problems',
title: 'Problems',
description:
'Code that either will cause an error or may cause confusing behavior. Developers should consider this a high priority to resolve.',
},
suggestion: {
slug: 'suggestions',
title: 'Suggestions',
description:
"Something that could be done in a better way but no errors will occur if the code isn't changed.",
},
layout: {
slug: 'formatting',
title: 'Formatting',
description:
'Primarily about whitespace, semicolons, commas, and parentheses, all the parts of the program that determine how the code looks rather than how it executes.',
},
};
export function groupsFromRuleTypes(rules: RuleData[]): Group[] {
const allTypes = objectToKeys(typeGroups);
const auditSlugsMap = rules.reduce<Partial<Record<RuleType, string[]>>>(
(acc, rule) =>
rule.meta.type == null
? acc
: {
...acc,
[rule.meta.type]: [
...(acc[rule.meta.type] ?? []),
ruleToSlug(rule),
],
},
{},
);
return allTypes
.map(type => ({
...typeGroups[type],
refs:
auditSlugsMap[type]?.map((slug): GroupRef => ({ slug, weight: 1 })) ??
[],
}))
.filter(group => group.refs.length);
}
export function groupsFromRuleCategories(rules: RuleData[]): Group[] {
const categoriesMap = rules.reduce<Record<string, Record<string, string[]>>>(
(acc, rule) => {
// meta.docs.category still used by some popular plugins (e.g. import, react, functional)
const category = rule.meta.docs?.category;
if (!category) {
return acc;
}
const { plugin = '' } = parseRuleId(rule.id);
return {
...acc,
[plugin]: {
...acc[plugin],
[category]: [...(acc[plugin]?.[category] ?? []), ruleToSlug(rule)],
},
};
},
{},
);
const groups = Object.entries(categoriesMap).flatMap(([plugin, categories]) =>
Object.entries(categories).map(
([category, slugs]): Group => ({
slug: `${slugify(plugin)}-${slugify(category)}`,
title: `${category} (${plugin})`,
refs: slugs.map(slug => ({ slug, weight: 1 })),
}),
),
);
return groups.toSorted((a, b) => a.slug.localeCompare(b.slug));
}
export function groupsFromCustomConfig(
rules: RuleData[],
groups: CustomGroup[],
): Group[] {
const rulesMap = createRulesMap(rules);
return groups.map(group => {
const groupRules = Array.isArray(group.rules)
? Object.fromEntries(group.rules.map(rule => [rule, 1]))
: group.rules;
const { refs, invalidRules } = resolveGroupRefs(groupRules, rulesMap);
if (invalidRules.length > 0 && Object.entries(groupRules).length > 0) {
if (refs.length === 0) {
throw new Error(
`Invalid rule configuration in group ${group.slug}. All rules are invalid.`,
);
}
logger.warn(
`Some rules in group ${group.slug} are invalid: ${invalidRules.join(', ')}`,
);
}
return {
slug: group.slug,
title: group.title,
refs,
};
});
}
export function createRulesMap(rules: RuleData[]): Record<string, RuleData[]> {
return rules.reduce<Record<string, RuleData[]>>(
(acc, rule) => ({
...acc,
[rule.id]: [...(acc[rule.id] || []), rule],
}),
{},
);
}
export function resolveGroupRefs(
groupRules: Record<string, number>,
rulesMap: Record<string, RuleData[]>,
): { refs: Group['refs']; invalidRules: string[] } {
return Object.entries(groupRules).reduce<{
refs: Group['refs'];
invalidRules: string[];
}>(
(acc, [rule, weight]) => {
const matchedRuleIds = rule.endsWith('*')
? expandWildcardRules(rule, Object.keys(rulesMap))
: [rule];
const matchedRefs = matchedRuleIds.flatMap(ruleId => {
const matchingRules = rulesMap[ruleId] || [];
const weightPerRule = weight / matchingRules.length;
return matchingRules.map(ruleData => ({
slug: ruleToSlug(ruleData),
weight: weightPerRule,
}));
});
return {
refs: [...acc.refs, ...matchedRefs],
invalidRules:
matchedRefs.length > 0
? acc.invalidRules
: [...acc.invalidRules, rule],
};
},
{ refs: [], invalidRules: [] },
);
}