diff --git a/__tests__/src/rules/img-redundant-alt-test.js b/__tests__/src/rules/img-redundant-alt-test.js index ccacb210..3bd5fcbb 100644 --- a/__tests__/src/rules/img-redundant-alt-test.js +++ b/__tests__/src/rules/img-redundant-alt-test.js @@ -74,6 +74,7 @@ ruleTester.run('img-redundant-alt', rule, { { code: 'ImageMagick;' }, { code: 'Photo of a friend' }, { code: 'Foo', settings: componentsSettings }, + { code: '画像', options: [{ words: ['イメージ'] }] }, )).map(parserOptionsMapper), invalid: parsers.all([].concat( { code: 'Photo of friend.;', errors: [expectedError] }, @@ -129,5 +130,8 @@ ruleTester.run('img-redundant-alt', rule, { { code: 'Word2;', options: array, errors: [expectedError] }, { code: 'Word1;', options: array, errors: [expectedError] }, { code: 'Word2;', options: array, errors: [expectedError] }, + + { code: 'イメージ', options: [{ words: ['イメージ'] }], errors: [expectedError] }, + { code: 'イメージです', options: [{ words: ['イメージ'] }], errors: [expectedError] }, )).map(parserOptionsMapper), }); diff --git a/src/rules/img-redundant-alt.js b/src/rules/img-redundant-alt.js index 6e2ba640..b90eda7b 100644 --- a/src/rules/img-redundant-alt.js +++ b/src/rules/img-redundant-alt.js @@ -8,6 +8,7 @@ // ---------------------------------------------------------------------------- import { getProp, getLiteralPropValue } from 'jsx-ast-utils'; +import includes from 'array-includes'; import { generateObjSchema, arraySchema } from '../util/schemas'; import getElementType from '../util/getElementType'; import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader'; @@ -25,6 +26,16 @@ const schema = generateObjSchema({ words: arraySchema, }); +function containsRedundantWord(value, redundantWords) { + const lowercaseRedundantWords = redundantWords.map((redundantWord) => redundantWord.toLowerCase()); + const isASCII = /[\x20-\x7F]+/.test(value); + + if (isASCII) { + return value.split(/\s+/).some((valueWord) => includes(lowercaseRedundantWords, valueWord.toLowerCase())); + } + return lowercaseRedundantWords.some((redundantWord) => value.toLowerCase().includes(redundantWord)); +} + export default { meta: { docs: { @@ -63,7 +74,7 @@ export default { const redundantWords = REDUNDANT_WORDS.concat(words); if (typeof value === 'string' && isVisible) { - const hasRedundancy = new RegExp(`(?!{)\\b(${redundantWords.join('|')})\\b(?!})`, 'i').test(value); + const hasRedundancy = containsRedundantWord(value, redundantWords); if (hasRedundancy === true) { context.report({