@@ -25,19 +25,16 @@ const parserOptions = {
25
25
26
26
const ruleTester = new RuleTester ( ) ;
27
27
28
- const customMissingPropError = type => ( {
29
- message : `${ type } elements must have an alt prop.` ,
28
+ const missingPropError = type => ( {
29
+ message : `${ type } elements must have an alt prop or use role="presentation" .` ,
30
30
type : 'JSXOpeningElement'
31
31
} ) ;
32
32
33
- const customAltValueError = type => ( {
34
- message : `${ type } alt prop must have a value. You can set alt="" if role="presentation" is applied .` ,
33
+ const altValueError = type => ( {
34
+ message : `Invalid alt value for ${ type } . Use alt="" or role="presentation" for presentational images .` ,
35
35
type : 'JSXOpeningElement'
36
36
} ) ;
37
37
38
- const expectedMissingPropError = customMissingPropError ( 'img' ) ;
39
- const expectedAltValueError = customAltValueError ( 'img' ) ;
40
-
41
38
const string = [ 'Avatar' ] ;
42
39
const array = [ [ 'Thumbnail' , 'Image' ] ] ;
43
40
@@ -65,11 +62,15 @@ ruleTester.run('img-uses-alt', rule, {
65
62
{ code : '<img alt={foo.bar || ""} />' , parserOptions } ,
66
63
{ code : '<img alt={bar() || ""} />' , parserOptions } ,
67
64
{ code : '<img alt={foo.bar() || ""} />' , parserOptions } ,
68
- { code : '<img alt="" role="presentation" />' , parserOptions } , // Allow alt to be undefined if role="presentation"
65
+ { code : '<img alt="" />' , parserOptions } ,
66
+ { code : '<img alt=" " />' , parserOptions } ,
67
+ { code : '<img alt="" role="presentation" />' , parserOptions } ,
69
68
{ code : '<img alt="" role={`presentation`} />' , parserOptions } ,
70
69
{ code : '<img alt="" role={"presentation"} />' , parserOptions } ,
70
+ { code : '<img role="presentation" />;' , parserOptions } ,
71
+ { code : '<img alt={undefined} role="presentation" />;' , parserOptions } ,
72
+ { code : '<img alt role="presentation" />;' , parserOptions } ,
71
73
{ code : '<img alt="this is lit..." role="presentation" />' , parserOptions } ,
72
- { code : '<img alt=" " />' , parserOptions } , // For decorative images.
73
74
74
75
// CUSTOM ELEMENT TESTS FOR STRING OPTION
75
76
{ code : '<Avatar alt="foo" />;' , options : string , parserOptions } ,
@@ -86,6 +87,7 @@ ruleTester.run('img-uses-alt', rule, {
86
87
{ code : '<Avatar alt={() => void 0} />' , options : string , parserOptions } ,
87
88
{ code : '<AVATAR />' , options : string , parserOptions } ,
88
89
{ code : '<Avatar alt={alt || "foo" } />' , options : string , parserOptions } ,
90
+ { code : '<Avatar alt="" />' , options : string , parserOptions } ,
89
91
90
92
// CUSTOM ELEMENT TESTS FOR ARRAY OPTION TESTS
91
93
{ code : '<Thumbnail alt="foo" />;' , options : array , parserOptions } ,
@@ -119,66 +121,59 @@ ruleTester.run('img-uses-alt', rule, {
119
121
] ,
120
122
invalid : [
121
123
// DEFAULT ELEMENT 'img' TESTS
122
- { code : '<img />;' , errors : [ expectedMissingPropError ] , parserOptions } ,
123
- { code : '<img alt />;' , errors : [ expectedAltValueError ] , parserOptions } ,
124
- { code : '<img alt={undefined} />;' , errors : [ expectedAltValueError ] , parserOptions } ,
125
- { code : '<img alt={`${undefined}`} />;' , errors : [ expectedAltValueError ] , parserOptions } ,
126
- { code : '<img alt="" />;' , errors : [ expectedAltValueError ] , parserOptions } ,
127
- { code : '<img role="presentation" />;' , errors : [ expectedMissingPropError ] , parserOptions } ,
128
- { code : '<img alt={undefined} role="presentation" />;' , errors : [ expectedAltValueError ] , parserOptions } ,
129
- { code : '<img alt role="presentation" />;' , errors : [ expectedAltValueError ] , parserOptions } ,
130
- { code : '<img src="xyz" />' , errors : [ expectedMissingPropError ] , parserOptions } ,
131
- { code : '<img {...this.props} />' , errors : [ expectedMissingPropError ] , parserOptions } ,
132
- { code : '<img alt={false || false} />' , errors : [ expectedAltValueError ] , parserOptions } ,
124
+ { code : '<img />;' , errors : [ missingPropError ( 'img' ) ] , parserOptions } ,
125
+ { code : '<img alt />;' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
126
+ { code : '<img alt={undefined} />;' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
127
+ { code : '<img alt={`${undefined}`} />;' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
128
+ { code : '<img src="xyz" />' , errors : [ missingPropError ( 'img' ) ] , parserOptions } ,
129
+ { code : '<img {...this.props} />' , errors : [ missingPropError ( 'img' ) ] , parserOptions } ,
130
+ { code : '<img alt={false || false} />' , errors : [ altValueError ( 'img' ) ] , parserOptions } ,
133
131
134
132
// CUSTOM ELEMENT TESTS FOR STRING OPTION
135
133
{
136
134
code : '<Avatar />;' ,
137
- errors : [ customMissingPropError ( 'Avatar' ) ] ,
135
+ errors : [ missingPropError ( 'Avatar' ) ] ,
138
136
options : string ,
139
137
parserOptions
140
138
} ,
141
- { code : '<Avatar alt />;' , errors : [ customAltValueError ( 'Avatar' ) ] , options : string , parserOptions } ,
142
- { code : '<Avatar alt={undefined} />;' , errors : [ customAltValueError ( 'Avatar' ) ] , options : string , parserOptions } ,
139
+ { code : '<Avatar alt />;' , errors : [ altValueError ( 'Avatar' ) ] , options : string , parserOptions } ,
140
+ { code : '<Avatar alt={undefined} />;' , errors : [ altValueError ( 'Avatar' ) ] , options : string , parserOptions } ,
143
141
{
144
142
code : '<Avatar alt={`${undefined}`} />;' ,
145
- errors : [ customAltValueError ( 'Avatar' ) ] ,
143
+ errors : [ altValueError ( 'Avatar' ) ] ,
146
144
options : string ,
147
145
parserOptions
148
146
} ,
149
- { code : '<Avatar alt="" />;' , errors : [ customAltValueError ( 'Avatar' ) ] , options : string , parserOptions } ,
150
- { code : '<Avatar src="xyz" />' , errors : [ customMissingPropError ( 'Avatar' ) ] , options : string , parserOptions } ,
151
- { code : '<Avatar {...this.props} />' , errors : [ customMissingPropError ( 'Avatar' ) ] , options : string , parserOptions } ,
147
+ { code : '<Avatar src="xyz" />' , errors : [ missingPropError ( 'Avatar' ) ] , options : string , parserOptions } ,
148
+ { code : '<Avatar {...this.props} />' , errors : [ missingPropError ( 'Avatar' ) ] , options : string , parserOptions } ,
152
149
153
150
// CUSTOM ELEMENT TESTS FOR ARRAY OPTION TESTS
154
- { code : '<Thumbnail />;' , errors : [ customMissingPropError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
155
- { code : '<Thumbnail alt />;' , errors : [ customAltValueError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
151
+ { code : '<Thumbnail />;' , errors : [ missingPropError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
152
+ { code : '<Thumbnail alt />;' , errors : [ altValueError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
156
153
{
157
154
code : '<Thumbnail alt={undefined} />;' ,
158
- errors : [ customAltValueError ( 'Thumbnail' ) ] ,
155
+ errors : [ altValueError ( 'Thumbnail' ) ] ,
159
156
options : array ,
160
157
parserOptions
161
158
} ,
162
159
{
163
160
code : '<Thumbnail alt={`${undefined}`} />;' ,
164
- errors : [ customAltValueError ( 'Thumbnail' ) ] ,
161
+ errors : [ altValueError ( 'Thumbnail' ) ] ,
165
162
options : array ,
166
163
parserOptions
167
164
} ,
168
- { code : '<Thumbnail alt="" />;' , errors : [ customAltValueError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
169
- { code : '<Thumbnail src="xyz" />' , errors : [ customMissingPropError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
165
+ { code : '<Thumbnail src="xyz" />' , errors : [ missingPropError ( 'Thumbnail' ) ] , options : array , parserOptions } ,
170
166
{
171
167
code : '<Thumbnail {...this.props} />' ,
172
- errors : [ customMissingPropError ( 'Thumbnail' ) ] ,
168
+ errors : [ missingPropError ( 'Thumbnail' ) ] ,
173
169
options : array ,
174
170
parserOptions
175
171
} ,
176
- { code : '<Image />;' , errors : [ customMissingPropError ( 'Image' ) ] , options : array , parserOptions } ,
177
- { code : '<Image alt />;' , errors : [ customAltValueError ( 'Image' ) ] , options : array , parserOptions } ,
178
- { code : '<Image alt={undefined} />;' , errors : [ customAltValueError ( 'Image' ) ] , options : array , parserOptions } ,
179
- { code : '<Image alt={`${undefined}`} />;' , errors : [ customAltValueError ( 'Image' ) ] , options : array , parserOptions } ,
180
- { code : '<Image alt="" />;' , errors : [ customAltValueError ( 'Image' ) ] , options : array , parserOptions } ,
181
- { code : '<Image src="xyz" />' , errors : [ customMissingPropError ( 'Image' ) ] , options : array , parserOptions } ,
182
- { code : '<Image {...this.props} />' , errors : [ customMissingPropError ( 'Image' ) ] , options : array , parserOptions }
172
+ { code : '<Image />;' , errors : [ missingPropError ( 'Image' ) ] , options : array , parserOptions } ,
173
+ { code : '<Image alt />;' , errors : [ altValueError ( 'Image' ) ] , options : array , parserOptions } ,
174
+ { code : '<Image alt={undefined} />;' , errors : [ altValueError ( 'Image' ) ] , options : array , parserOptions } ,
175
+ { code : '<Image alt={`${undefined}`} />;' , errors : [ altValueError ( 'Image' ) ] , options : array , parserOptions } ,
176
+ { code : '<Image src="xyz" />' , errors : [ missingPropError ( 'Image' ) ] , options : array , parserOptions } ,
177
+ { code : '<Image {...this.props} />' , errors : [ missingPropError ( 'Image' ) ] , options : array , parserOptions }
183
178
]
184
179
} ) ;
0 commit comments