Skip to content

Commit e393661

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

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
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
});

src/rules/img-redundant-alt.js

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

1010
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
11+
import includes from 'array-includes';
1112
import { generateObjSchema, arraySchema } from '../util/schemas';
1213
import getElementType from '../util/getElementType';
1314
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
@@ -25,6 +26,16 @@ const schema = generateObjSchema({
2526
words: arraySchema,
2627
});
2728

29+
function containsRedundantWord(value, redundantWords) {
30+
const lowercaseRedundantWords = redundantWords.map((redundantWord) => redundantWord.toLowerCase());
31+
const isASCII = /[\x20-\x7F]+/.test(value);
32+
33+
if (isASCII) {
34+
return value.split(/\s+/).some((valueWord) => includes(lowercaseRedundantWords, valueWord.toLowerCase()));
35+
}
36+
return lowercaseRedundantWords.some((redundantWord) => includes(value.toLowerCase(), redundantWord));
37+
}
38+
2839
export default {
2940
meta: {
3041
docs: {
@@ -63,7 +74,7 @@ export default {
6374
const redundantWords = REDUNDANT_WORDS.concat(words);
6475

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

6879
if (hasRedundancy === true) {
6980
context.report({

0 commit comments

Comments
 (0)