Skip to content

Commit e8414ef

Browse files
committed
Merge branch 'alt-text' of https://github.com/jwyung/eslint-plugin-jsx-a11y into jwyung-alt-text
2 parents fda3c49 + ab2e0de commit e8414ef

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

__tests__/src/rules/alt-text-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ ruleTester.run('alt-text', rule, {
8383
{ code: '<img alt={error ? "not working": "working"} />' },
8484
{ code: '<img alt={undefined ? "working": "not working"} />' },
8585
{ code: '<img alt={plugin.name + " Logo"} />' },
86+
{ code: '<img aria-label="foo" />' },
87+
{ code: '<img aria-labelledby="id1" />' },
8688

8789
// DEFAULT <object> TESTS
8890
{ code: '<object aria-label="foo" />' },

src/rules/alt-text.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ const schema = generateObjSchema({
3434

3535
const ruleByElement = {
3636
img(context, node) {
37-
const nodeType = elementType(node);
37+
// Check for label props (`aria-label` and `aria-labelledby`) to provide text alternative
38+
const ariaLabelProp = getProp(node.attributes, 'aria-label');
39+
const arialLabelledByProp = getProp(node.attributes, 'aria-labelledby');
40+
const hasLabel = ariaLabelProp !== undefined || arialLabelledByProp !== undefined;
3841

42+
if (hasLabel) {
43+
return;
44+
}
45+
46+
const nodeType = elementType(node);
3947
const altProp = getProp(node.attributes, 'alt');
4048

4149
// Missing alt prop error.

0 commit comments

Comments
 (0)