@@ -24,13 +24,18 @@ const parserOptions = {
24
24
const ruleTester = new RuleTester ( ) ;
25
25
26
26
const missingPropError = type => ( {
27
- message : `${ type } elements must have an alt prop or use role="presentation" .` ,
27
+ message : `${ type } elements must have an alt prop, either with meaningful text, or an empty string for decorative images .` ,
28
28
type : 'JSXOpeningElement' ,
29
29
} ) ;
30
30
31
31
const altValueError = type => ( {
32
32
message : `Invalid alt value for ${ type } . \
33
- Use alt="" or role="presentation" for presentational images.` ,
33
+ Use alt="" for presentational images.` ,
34
+ type : 'JSXOpeningElement' ,
35
+ } ) ;
36
+
37
+ const preferAltError = ( ) => ( {
38
+ message : 'Prefer alt="" over role="presentation". First rule of aria is to not use aria if it can be achieved via native HTML.' ,
34
39
type : 'JSXOpeningElement' ,
35
40
} ) ;
36
41
@@ -68,9 +73,6 @@ ruleTester.run('img-has-alt', rule, {
68
73
{ code : '<img alt="" role="presentation" />' , parserOptions } ,
69
74
{ code : '<img alt="" role={`presentation`} />' , parserOptions } ,
70
75
{ code : '<img alt="" role={"presentation"} />' , parserOptions } ,
71
- { code : '<img role="presentation" />;' , parserOptions } ,
72
- { code : '<img alt={undefined} role="presentation" />;' , parserOptions } ,
73
- { code : '<img alt role="presentation" />;' , parserOptions } ,
74
76
{ code : '<img alt="this is lit..." role="presentation" />' , parserOptions } ,
75
77
{ code : '<img alt={error ? "not working": "working"} />' , parserOptions } ,
76
78
{ code : '<img alt={undefined ? "working": "not working"} />' , parserOptions } ,
@@ -115,6 +117,9 @@ ruleTester.run('img-has-alt', rule, {
115
117
{ code : '<img role />' , errors : [ missingPropError ( 'img' ) ] , parserOptions } ,
116
118
{ code : '<img {...this.props} />' , errors : [ missingPropError ( 'img' ) ] , parserOptions } ,
117
119
{ code : '<img alt={false || false} />' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
120
+ { code : '<img alt={undefined} role="presentation" />;' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
121
+ { code : '<img alt role="presentation" />;' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
122
+ { code : '<img role="presentation" />;' , errors : [ preferAltError ( ) ] , parserOptions } ,
118
123
119
124
// CUSTOM ELEMENT TESTS FOR ARRAY OPTION TESTS
120
125
{
0 commit comments