@@ -25,19 +25,16 @@ const parserOptions = {
2525
2626const ruleTester = new RuleTester ( ) ;
2727
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" .` ,
3030 type : 'JSXOpeningElement'
3131} ) ;
3232
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 .` ,
3535 type : 'JSXOpeningElement'
3636} ) ;
3737
38- const expectedMissingPropError = customMissingPropError ( 'img' ) ;
39- const expectedAltValueError = customAltValueError ( 'img' ) ;
40-
4138const string = [ 'Avatar' ] ;
4239const array = [ [ 'Thumbnail' , 'Image' ] ] ;
4340
@@ -65,11 +62,15 @@ ruleTester.run('img-uses-alt', rule, {
6562 { code : '<img alt={foo.bar || ""} />' , parserOptions } ,
6663 { code : '<img alt={bar() || ""} />' , parserOptions } ,
6764 { 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 } ,
6968 { code : '<img alt="" role={`presentation`} />' , parserOptions } ,
7069 { 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 } ,
7173 { code : '<img alt="this is lit..." role="presentation" />' , parserOptions } ,
72- { code : '<img alt=" " />' , parserOptions } , // For decorative images.
7374
7475 // CUSTOM ELEMENT TESTS FOR STRING OPTION
7576 { code : '<Avatar alt="foo" />;' , options : string , parserOptions } ,
@@ -86,6 +87,7 @@ ruleTester.run('img-uses-alt', rule, {
8687 { code : '<Avatar alt={() => void 0} />' , options : string , parserOptions } ,
8788 { code : '<AVATAR />' , options : string , parserOptions } ,
8889 { code : '<Avatar alt={alt || "foo" } />' , options : string , parserOptions } ,
90+ { code : '<Avatar alt="" />' , options : string , parserOptions } ,
8991
9092 // CUSTOM ELEMENT TESTS FOR ARRAY OPTION TESTS
9193 { code : '<Thumbnail alt="foo" />;' , options : array , parserOptions } ,
@@ -119,66 +121,59 @@ ruleTester.run('img-uses-alt', rule, {
119121 ] ,
120122 invalid : [
121123 // 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 } ,
133131
134132 // CUSTOM ELEMENT TESTS FOR STRING OPTION
135133 {
136134 code : '<Avatar />;' ,
137- errors : [ customMissingPropError ( 'Avatar' ) ] ,
135+ errors : [ missingPropError ( 'Avatar' ) ] ,
138136 options : string ,
139137 parserOptions
140138 } ,
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 } ,
143141 {
144142 code : '<Avatar alt={`${undefined}`} />;' ,
145- errors : [ customAltValueError ( 'Avatar' ) ] ,
143+ errors : [ altValueError ( 'Avatar' ) ] ,
146144 options : string ,
147145 parserOptions
148146 } ,
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 } ,
152149
153150 // 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 } ,
156153 {
157154 code : '<Thumbnail alt={undefined} />;' ,
158- errors : [ customAltValueError ( 'Thumbnail' ) ] ,
155+ errors : [ altValueError ( 'Thumbnail' ) ] ,
159156 options : array ,
160157 parserOptions
161158 } ,
162159 {
163160 code : '<Thumbnail alt={`${undefined}`} />;' ,
164- errors : [ customAltValueError ( 'Thumbnail' ) ] ,
161+ errors : [ altValueError ( 'Thumbnail' ) ] ,
165162 options : array ,
166163 parserOptions
167164 } ,
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 } ,
170166 {
171167 code : '<Thumbnail {...this.props} />' ,
172- errors : [ customMissingPropError ( 'Thumbnail' ) ] ,
168+ errors : [ missingPropError ( 'Thumbnail' ) ] ,
173169 options : array ,
174170 parserOptions
175171 } ,
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 }
183178 ]
184179} ) ;
0 commit comments