-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtransform.ts
More file actions
146 lines (129 loc) · 3.89 KB
/
transform.ts
File metadata and controls
146 lines (129 loc) · 3.89 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
import axe from 'axe-core';
import type { Audit, Group } from '@code-pushup/models';
import { capitalize } from '@code-pushup/utils';
import type { AxePreset } from '../constants.js';
const WCAG_LEVEL_A_TAGS = ['wcag2a', 'wcag21a'];
const WCAG_LEVEL_AA_TAGS_21 = ['wcag2aa', 'wcag21aa'];
const WCAG_LEVEL_AA_TAGS_22 = ['wcag2aa', 'wcag21aa', 'wcag22aa'];
const CATEGORY_TITLES: Record<string, string> = {
'cat.aria': 'ARIA',
'cat.color': 'Color & Contrast',
'cat.forms': 'Forms',
'cat.keyboard': 'Keyboard',
'cat.language': 'Language',
'cat.name-role-value': 'Names & Labels',
'cat.parsing': 'Parsing',
'cat.semantics': 'Semantics',
'cat.sensory-and-visual-cues': 'Visual Cues',
'cat.structure': 'Structure',
'cat.tables': 'Tables',
'cat.text-alternatives': 'Text Alternatives',
'cat.time-and-media': 'Media',
};
export function loadAxeRules(preset: AxePreset): axe.RuleMetadata[] {
const tags = getPresetTags(preset);
return tags.length === 0 ? axe.getRules() : axe.getRules(tags);
}
export function transformRulesToAudits(rules: axe.RuleMetadata[]): Audit[] {
return rules.map(rule => ({
slug: rule.ruleId,
title: rule.help,
description: rule.description,
docsUrl: rule.helpUrl,
}));
}
export function transformRulesToGroups(
rules: axe.RuleMetadata[],
preset: AxePreset,
): Group[] {
const groups = (() => {
switch (preset) {
case 'wcag21aa':
return createWcagGroups(rules, '2.1');
case 'wcag22aa':
return createWcagGroups(rules, '2.2');
// eslint-disable-next-line sonarjs/no-duplicate-string
case 'best-practice':
return createCategoryGroups(rules);
case 'all':
return [
...createWcagGroups(rules, '2.2'),
...createCategoryGroups(rules),
];
}
})();
return groups.filter(({ refs }) => refs.length > 0);
}
/**
* Maps preset to corresponding axe-core tags.
*
* WCAG tags are non-cumulative - each rule has exactly one WCAG version tag.
* To include all rules up to a version/level, multiple tags must be combined.
*/
function getPresetTags(preset: AxePreset): string[] {
switch (preset) {
case 'wcag21aa':
return [...WCAG_LEVEL_A_TAGS, ...WCAG_LEVEL_AA_TAGS_21];
case 'wcag22aa':
return [...WCAG_LEVEL_A_TAGS, ...WCAG_LEVEL_AA_TAGS_22];
case 'best-practice':
return ['best-practice'];
case 'all':
return [];
}
}
function createGroup(
slug: string,
title: string,
rules: axe.RuleMetadata[],
): Group {
return {
slug,
title,
refs: rules.map(({ ruleId }) => ({ slug: ruleId, weight: 1 })),
};
}
function createWcagGroups(
rules: axe.RuleMetadata[],
version: '2.1' | '2.2',
): Group[] {
const aTags = WCAG_LEVEL_A_TAGS;
const aaTags =
version === '2.1' ? WCAG_LEVEL_AA_TAGS_21 : WCAG_LEVEL_AA_TAGS_22;
const levelARules = rules.filter(({ tags }) =>
tags.some(tag => aTags.includes(tag)),
);
const levelAARules = rules.filter(({ tags }) =>
tags.some(tag => aaTags.includes(tag)),
);
const versionSlug = version.replace('.', '');
return [
createGroup(
`wcag${versionSlug}-level-a`,
`WCAG ${version} Level A`,
levelARules,
),
createGroup(
`wcag${versionSlug}-level-aa`,
`WCAG ${version} Level AA`,
levelAARules,
),
];
}
function createCategoryGroups(rules: axe.RuleMetadata[]): Group[] {
const categoryTags = new Set(
rules.flatMap(({ tags }) => tags.filter(tag => tag.startsWith('cat.'))),
);
return [...categoryTags].map(tag => {
const slug = tag.replace('cat.', '');
const title = formatCategoryTitle(tag, slug);
const categoryRules = rules.filter(({ tags }) => tags.includes(tag));
return createGroup(slug, title, categoryRules);
});
}
function formatCategoryTitle(tag: string, slug: string): string {
if (CATEGORY_TITLES[tag]) {
return CATEGORY_TITLES[tag];
}
return slug.split('-').map(capitalize).join(' ');
}