Skip to content

Commit 13d4b7e

Browse files
badtantbeefancohen
authored andcommitted
[new] - Add support for custom words in img-redundant-alt (#75)
1 parent f9b0d82 commit 13d4b7e

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

docs/rules/img-redundant-alt.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
Enforce img alt attribute does not contain the word image, picture, or photo. Screenreaders already announce `img` elements as an image. There is no need to use words such as *image*, *photo*, and/or *picture*.
44

55
## Rule details
6+
This rule takes one optional argument of type string or array of strings. These strings can be used to specify custom words that should be checked for in the alt prop. Useful for specifying words in other languages.
67

7-
This rule takes no arguments. This rule will first check if `aria-hidden` is true to determine whether to enforce the rule. If the image is hidden, then rule will always succeed.
8+
The rule will first check if `aria-hidden` is true to determine whether to enforce the rule. If the image is hidden, then rule will always succeed.
9+
10+
To tell this plugin to also check for custom words , specify this in your `.eslintrc` file:
11+
12+
```json
13+
{
14+
"rules": {
15+
"jsx-a11y/img-redundant-alt": [ 2, "Bild" ], // OR
16+
"jsx-a11y/img-redundant-alt": [ 2, [ "Bild", "Foto" ] ]
17+
}
18+
}
19+
```
820

921
### Succeed
1022
```jsx

src/rules/img-redundant-alt.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,38 @@ const REDUNDANT_WORDS = [
1818

1919
const errorMessage = 'Redundant alt attribute. Screen-readers already announce ' +
2020
'`img` tags as an image. You don\'t need to use the words `image`, ' +
21-
'`photo,` or `picture` in the alt prop.';
21+
'`photo,` or `picture` (or any specified custom words) in the alt prop.';
2222

2323
module.exports = {
2424
meta: {
2525
docs: {},
2626

2727
schema: [
28-
{ type: 'object' },
28+
{
29+
oneOf: [
30+
{ type: 'string' },
31+
{
32+
type: 'array',
33+
items: {
34+
type: 'string',
35+
},
36+
minItems: 1,
37+
uniqueItems: true,
38+
},
39+
],
40+
},
2941
],
3042
},
3143

3244
create: context => ({
3345
JSXOpeningElement: node => {
46+
let REDUNDANT_WORDS_EXTENDED;
47+
48+
if (context.options[0]) {
49+
REDUNDANT_WORDS_EXTENDED = REDUNDANT_WORDS.concat(context.options[0]);
50+
} else {
51+
REDUNDANT_WORDS_EXTENDED = REDUNDANT_WORDS;
52+
}
3453
const type = elementType(node);
3554
if (type !== 'img') {
3655
return;
@@ -46,7 +65,7 @@ module.exports = {
4665
const isVisible = isHiddenFromScreenReader(type, node.attributes) === false;
4766

4867
if (typeof value === 'string' && isVisible) {
49-
const hasRedundancy = REDUNDANT_WORDS
68+
const hasRedundancy = REDUNDANT_WORDS_EXTENDED
5069
.some(word => Boolean(value.match(new RegExp(`(?!{)${word}(?!})`, 'gi'))));
5170

5271
if (hasRedundancy === true) {

tests/src/rules/img-redundant-alt.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ const parserOptions = {
2121
// Tests
2222
// -----------------------------------------------------------------------------
2323

24+
const string = ['Word'];
25+
const array = [['Word1', 'Word2']];
26+
2427
const ruleTester = new RuleTester();
2528

2629
const expectedError = {
2730
message: 'Redundant alt attribute. Screen-readers already announce `img` tags as an image. ' +
28-
'You don\'t need to use the words `image`, `photo,` or `picture` in the alt prop.',
31+
'You don\'t need to use the words `image`, `photo,` or `picture` ' +
32+
'(or any specified custom words) in the alt prop.',
2933
type: 'JSXOpeningElement',
3034
};
3135

@@ -113,5 +117,12 @@ ruleTester.run('img-redundant-alt', rule, {
113117
errors: [expectedError],
114118
parserOptions,
115119
},
120+
121+
// TESTS FOR STRING OPTION
122+
{ code: '<img alt="Word" />;', options: string, errors: [expectedError], parserOptions },
123+
124+
// TESTS FOR ARRAY OPTION TESTS
125+
{ code: '<img alt="Word1" />;', options: array, errors: [expectedError], parserOptions },
126+
{ code: '<img alt="Word2" />;', options: array, errors: [expectedError], parserOptions },
116127
],
117128
});

0 commit comments

Comments
 (0)