Skip to content

Commit d7648ec

Browse files
sarbbottambeefancohen
authored andcommitted
fix(rule): img-has-alt - img must have an alt attribute (#102)
1 parent 4ecae6a commit d7648ec

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

__tests__/src/rules/img-has-alt-test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ const parserOptions = {
2424
const ruleTester = new RuleTester();
2525

2626
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.`,
2828
type: 'JSXOpeningElement',
2929
});
3030

3131
const altValueError = type => ({
3232
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.',
3439
type: 'JSXOpeningElement',
3540
});
3641

@@ -68,9 +73,6 @@ ruleTester.run('img-has-alt', rule, {
6873
{ code: '<img alt="" role="presentation" />', parserOptions },
6974
{ code: '<img alt="" role={`presentation`} />', parserOptions },
7075
{ 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 },
7476
{ code: '<img alt="this is lit..." role="presentation" />', parserOptions },
7577
{ code: '<img alt={error ? "not working": "working"} />', parserOptions },
7678
{ code: '<img alt={undefined ? "working": "not working"} />', parserOptions },
@@ -115,6 +117,9 @@ ruleTester.run('img-has-alt', rule, {
115117
{ code: '<img role />', errors: [missingPropError('img')], parserOptions },
116118
{ code: '<img {...this.props} />', errors: [missingPropError('img')], parserOptions },
117119
{ 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 },
118123

119124
// CUSTOM ELEMENT TESTS FOR ARRAY OPTION TESTS
120125
{

docs/rules/img-has-alt.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function Foo(props) {
7676
<img src="foo" alt={altText} />
7777
<img src="foo" alt={`${person} smiling`} />
7878
<img src="foo" alt="" />
79-
<img src="foo" role="presentation" />
8079
```
8180

8281
### Fail

src/rules/img-has-alt.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ module.exports = {
3535
const isPresentation = roleProp && typeof roleValue === 'string'
3636
&& roleValue.toLowerCase() === 'presentation';
3737

38-
if (isPresentation) {
39-
return;
40-
}
41-
4238
const altProp = getProp(node.attributes, 'alt');
4339

4440
// Missing alt prop error.
4541
if (altProp === undefined) {
42+
if (isPresentation) {
43+
context.report({
44+
node,
45+
message: 'Prefer alt="" over role="presentation". First rule of aria is to not use aria if it can be achieved via native HTML.',
46+
});
47+
return;
48+
}
4649
context.report({
4750
node,
48-
message: `${nodeType} elements must have an alt prop or use role="presentation".`,
51+
message: `${nodeType} elements must have an alt prop, either with meaningful text, or an empty string for decorative images.`,
4952
});
5053
return;
5154
}
@@ -63,7 +66,7 @@ module.exports = {
6366
node,
6467
message:
6568
`Invalid alt value for ${nodeType}. \
66-
Use alt="" or role="presentation" for presentational images.`,
69+
Use alt="" for presentational images.`,
6770
});
6871
},
6972
}),

0 commit comments

Comments
 (0)