Skip to content

Commit 9539f9b

Browse files
Merge pull request #2501 from johanrd/fix/2471
Post-merge review of #2471 (`template-no-invalid-aria-attributes`)
2 parents 0d34619 + c634dfa commit 9539f9b

2 files changed

Lines changed: 46 additions & 12 deletions

File tree

lib/rules/template-no-invalid-aria-attributes.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,37 @@ function isValidAriaValue(attrName, value) {
6262
}
6363
}
6464

65+
function getExpectedTypeDescription(attrName) {
66+
const attrDef = aria.get(attrName);
67+
if (!attrDef) {
68+
return 'a valid value';
69+
}
70+
switch (attrDef.type) {
71+
case 'tristate': {
72+
return 'a boolean or the string "mixed".';
73+
}
74+
case 'token': {
75+
const vals = attrDef.values.map((v) => (typeof v === 'boolean' ? v.toString() : v));
76+
return `a single token from the following: ${vals.join(', ')}.`;
77+
}
78+
case 'tokenlist': {
79+
return `a list of one or more tokens from the following: ${attrDef.values.join(', ')}.`;
80+
}
81+
case 'idlist': {
82+
return 'a list of strings that represent DOM element IDs (idlist)';
83+
}
84+
case 'id': {
85+
return 'a string that represents a DOM element ID';
86+
}
87+
case 'integer': {
88+
return 'an integer.';
89+
}
90+
default: {
91+
return `a ${attrDef.type}.`;
92+
}
93+
}
94+
}
95+
6596
/** @type {import('eslint').Rule.RuleModule} */
6697
module.exports = {
6798
meta: {
@@ -75,7 +106,7 @@ module.exports = {
75106
schema: [],
76107
messages: {
77108
noInvalidAriaAttribute: 'Invalid ARIA attribute: {{attribute}}',
78-
invalidAriaAttributeValue: 'Invalid value for ARIA attribute {{attribute}}.',
109+
invalidAriaAttributeValue: 'The value for {{attribute}} must be {{expectedType}}',
79110
},
80111
originallyFrom: {
81112
name: 'ember-template-lint',
@@ -118,7 +149,10 @@ module.exports = {
118149
context.report({
119150
node,
120151
messageId: 'invalidAriaAttributeValue',
121-
data: { attribute: node.name },
152+
data: {
153+
attribute: node.name,
154+
expectedType: getExpectedTypeDescription(node.name),
155+
},
122156
});
123157
}
124158
}

tests/lib/rules/template-no-invalid-aria-attributes.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,52 +171,52 @@ hbsRuleTester.run('template-no-invalid-aria-attributes', rule, {
171171
{
172172
code: '<span role="checkbox" aria-checked="bad-value" tabindex="0" aria-label="Forget me"></span>',
173173
output: null,
174-
errors: [{ message: 'Invalid value for ARIA attribute aria-checked.' }],
174+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
175175
},
176176
{
177177
code: '<button type="submit" disabled="true" aria-disabled="123">Submit</button>',
178178
output: null,
179-
errors: [{ message: 'Invalid value for ARIA attribute aria-disabled.' }],
179+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
180180
},
181181
{
182182
code: '<input type="text" disabled="true" aria-errormessage="false" />',
183183
output: null,
184-
errors: [{ message: 'Invalid value for ARIA attribute aria-errormessage.' }],
184+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
185185
},
186186
{
187187
code: '<button type="submit" aria-describedby="blah false">Continue at your own risk</button>',
188188
output: null,
189-
errors: [{ message: 'Invalid value for ARIA attribute aria-describedby.' }],
189+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
190190
},
191191
{
192192
code: '<div role="heading" aria-level="bogus">Inaccessible heading</div>',
193193
output: null,
194-
errors: [{ message: 'Invalid value for ARIA attribute aria-level.' }],
194+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
195195
},
196196
{
197197
code: '<div role="heading" aria-level="true">Another inaccessible heading</div>',
198198
output: null,
199-
errors: [{ message: 'Invalid value for ARIA attribute aria-level.' }],
199+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
200200
},
201201
{
202202
code: '<div role="slider" aria-valuenow=(2*2) aria-valuemax="100" aria-valuemin="30">Broken slider</div>',
203203
output: null,
204-
errors: [{ message: 'Invalid value for ARIA attribute aria-valuenow.' }],
204+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
205205
},
206206
{
207207
code: '<div role="region" aria-live="no-such-value">Inaccessible live region</div>',
208208
output: null,
209-
errors: [{ message: 'Invalid value for ARIA attribute aria-live.' }],
209+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
210210
},
211211
{
212212
code: '<div role="region" aria-live="polite" aria-relevant="additions errors">Inaccessible live region</div>',
213213
output: null,
214-
errors: [{ message: 'Invalid value for ARIA attribute aria-relevant.' }],
214+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
215215
},
216216
{
217217
code: '<input type="text" aria-required="undefined" />',
218218
output: null,
219-
errors: [{ message: 'Invalid value for ARIA attribute aria-required.' }],
219+
errors: [{ messageId: 'invalidAriaAttributeValue' }],
220220
},
221221
],
222222
});

0 commit comments

Comments
 (0)