Skip to content

Commit f270961

Browse files
committed
[Fix]: Fixed no error with multibyte characters specified in words option.
Fixes #969
1 parent 1adec35 commit f270961

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-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

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ const schema = generateObjSchema({
2525
words: arraySchema,
2626
});
2727

28+
function containsRedundantWord(value, redundantWords) {
29+
const lowercaseRedundantWords = redundantWords.map((redundantWord) => redundantWord.toLowerCase());
30+
const isASCII = /[\x20-\x7F]+/.test(value);
31+
32+
if (isASCII) {
33+
return value.split(/\s+/).some((valueWord) => lowercaseRedundantWords.includes(valueWord.toLowerCase()));
34+
}
35+
return lowercaseRedundantWords.some((redundantWord) => value.toLowerCase().includes(redundantWord));
36+
}
37+
2838
export default {
2939
meta: {
3040
docs: {
@@ -63,7 +73,7 @@ export default {
6373
const redundantWords = REDUNDANT_WORDS.concat(words);
6474

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

6878
if (hasRedundancy === true) {
6979
context.report({

0 commit comments

Comments
 (0)