forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-redundant-role.js
More file actions
174 lines (156 loc) · 4.62 KB
/
template-no-redundant-role.js
File metadata and controls
174 lines (156 loc) · 4.62 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
169
170
171
172
173
174
const DEFAULT_CONFIG = {
checkAllHTMLElements: true,
};
function parseConfig(config) {
if (config === true) {
return DEFAULT_CONFIG;
}
return { ...DEFAULT_CONFIG, ...config };
}
function createErrorMessageLandmarkElement(element, role) {
return `Use of redundant or invalid role: ${role} on <${element}> detected. If a landmark element is used, any role provided will either be redundant or incorrect.`;
}
function createErrorMessageAnyElement(element, role) {
return `Use of redundant or invalid role: ${role} on <${element}> detected.`;
}
// https://www.w3.org/TR/html-aria/#docconformance
const LANDMARK_ROLES = new Set([
'banner',
'main',
'complementary',
'search',
'form',
'navigation',
'contentinfo',
]);
const ALLOWED_ELEMENT_ROLES = [
{ name: 'nav', role: 'navigation' },
{ name: 'form', role: 'search' },
{ name: 'ol', role: 'list' },
{ name: 'ul', role: 'list' },
{ name: 'a', role: 'link' },
{ name: 'input', role: 'combobox' },
];
// Mapping of roles to their corresponding HTML elements
// From https://www.w3.org/TR/html-aria/
const ROLE_TO_ELEMENTS = {
article: ['article'],
banner: ['header'],
button: ['button'],
cell: ['td'],
checkbox: ['input'],
columnheader: ['th'],
complementary: ['aside'],
contentinfo: ['footer'],
definition: ['dd'],
dialog: ['dialog'],
document: ['body'],
figure: ['figure'],
form: ['form'],
grid: ['table'],
gridcell: ['td'],
group: ['details', 'fieldset', 'optgroup'],
heading: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
img: ['img'],
link: ['a'],
list: ['ol', 'ul'],
listbox: ['select'],
listitem: ['li'],
main: ['main'],
navigation: ['nav'],
option: ['option'],
radio: ['input'],
region: ['section'],
row: ['tr'],
rowgroup: ['tbody', 'tfoot', 'thead'],
rowheader: ['th'],
search: ['search'],
searchbox: ['input'],
separator: ['hr'],
slider: ['input'],
spinbutton: ['input'],
status: ['output'],
table: ['table'],
term: ['dfn', 'dt'],
textbox: ['input', 'textarea'],
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow redundant role attributes',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-redundant-role.md',
templateMode: 'both',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
checkAllHTMLElements: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-redundant-role.js',
docs: 'docs/rule/no-redundant-role.md',
tests: 'test/unit/rules/no-redundant-role-test.js',
},
},
create(context) {
const config = parseConfig(context.options[0]);
return {
GlimmerElementNode(node) {
const roleAttr = node.attributes?.find((attr) => attr.name === 'role');
if (!roleAttr) {
return;
}
let roleValue;
if (roleAttr.value && roleAttr.value.type === 'GlimmerTextNode') {
roleValue = roleAttr.value.chars || '';
} else {
// Skip dynamic role values
return;
}
const isLandmarkRole = LANDMARK_ROLES.has(roleValue);
if (!config.checkAllHTMLElements && !isLandmarkRole) {
return;
}
const elementsWithRole = ROLE_TO_ELEMENTS[roleValue];
if (!elementsWithRole) {
return;
}
const isRedundant =
elementsWithRole.includes(node.tag) &&
!ALLOWED_ELEMENT_ROLES.some((e) => e.name === node.tag && e.role === roleValue);
if (isRedundant) {
const errorMessage = isLandmarkRole
? createErrorMessageLandmarkElement(node.tag, roleValue)
: createErrorMessageAnyElement(node.tag, roleValue);
context.report({
node,
message: errorMessage,
fix(fixer) {
const sourceCode = context.getSourceCode();
const elementText = sourceCode.getText(node);
const roleAttrText = sourceCode.getText(roleAttr);
// Find the role attribute in the element text and remove it along with preceding space
const roleAttrPattern = new RegExp(
`\\s+${roleAttrText.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&')}`
);
const fixedText = elementText.replace(roleAttrPattern, '');
return fixer.replaceText(node, fixedText);
},
});
}
},
};
},
};