Skip to content

Commit 2d8d557

Browse files
makototljharb
authored andcommitted
[Fix] img-redundant-alt: fixed multibyte character support
Fixes #969
1 parent 540cb7a commit 2d8d557

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ ruleTester.run('img-redundant-alt', rule, {
7474
{ code: '<img alt="ImageMagick" />;' },
7575
{ code: '<Image alt="Photo of a friend" />' },
7676
{ code: '<Image alt="Foo" />', settings: componentsSettings },
77+
{ code: '<img alt="画像" />', options: [{ words: ['イメージ'] }] },
7778
)).map(parserOptionsMapper),
7879
invalid: parsers.all([].concat(
7980
{ code: '<img alt="Photo of friend." />;', errors: [expectedError] },
@@ -129,5 +130,8 @@ ruleTester.run('img-redundant-alt', rule, {
129130
{ code: '<img alt="Word2" />;', options: array, errors: [expectedError] },
130131
{ code: '<Image alt="Word1" />;', options: array, errors: [expectedError] },
131132
{ code: '<Image alt="Word2" />;', options: array, errors: [expectedError] },
133+
134+
{ code: '<img alt="イメージ" />', options: [{ words: ['イメージ'] }], errors: [expectedError] },
135+
{ code: '<img alt="イメージです" />', options: [{ words: ['イメージ'] }], errors: [expectedError] },
132136
)).map(parserOptionsMapper),
133137
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"language-tags": "^1.0.9",
8989
"minimatch": "^3.1.2",
9090
"object.entries": "^1.1.7",
91-
"object.fromentries": "^2.0.7"
91+
"object.fromentries": "^2.0.7",
92+
"string.prototype.includes": "^2.0.0"
9293
},
9394
"peerDependencies": {
9495
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8"

src/rules/img-redundant-alt.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// ----------------------------------------------------------------------------
99

1010
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
11+
import includes from 'array-includes';
12+
import stringIncludes from 'string.prototype.includes';
1113
import { generateObjSchema, arraySchema } from '../util/schemas';
1214
import getElementType from '../util/getElementType';
1315
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
@@ -25,6 +27,16 @@ const schema = generateObjSchema({
2527
words: arraySchema,
2628
});
2729

30+
function containsRedundantWord(value, redundantWords) {
31+
const lowercaseRedundantWords = redundantWords.map((redundantWord) => redundantWord.toLowerCase());
32+
const isASCII = /[\x20-\x7F]+/.test(value);
33+
34+
if (isASCII) {
35+
return value.split(/\s+/).some((valueWord) => includes(lowercaseRedundantWords, valueWord.toLowerCase()));
36+
}
37+
return lowercaseRedundantWords.some((redundantWord) => stringIncludes(value.toLowerCase(), redundantWord));
38+
}
39+
2840
export default {
2941
meta: {
3042
docs: {
@@ -63,7 +75,7 @@ export default {
6375
const redundantWords = REDUNDANT_WORDS.concat(words);
6476

6577
if (typeof value === 'string' && isVisible) {
66-
const hasRedundancy = new RegExp(`(?!{)\\b(${redundantWords.join('|')})\\b(?!})`, 'i').test(value);
78+
const hasRedundancy = containsRedundantWord(value, redundantWords);
6779

6880
if (hasRedundancy === true) {
6981
context.report({

0 commit comments

Comments
 (0)