Skip to content

Commit d48cc0d

Browse files
author
Ethan Cohen
committed
[fix] - Fix img-redundant-alt rule to use getLiteralPropValue.
1 parent f560d8e commit d48cc0d

File tree

2 files changed

+4
-18
lines changed

2 files changed

+4
-18
lines changed

src/rules/img-redundant-alt.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Rule Definition
88
// ----------------------------------------------------------------------------
99

10-
import { getProp, getPropValue, elementType } from 'jsx-ast-utils';
10+
import { getProp, getLiteralPropValue, elementType } from 'jsx-ast-utils';
1111
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
1212

1313
const REDUNDANT_WORDS = [
@@ -20,11 +20,6 @@ const errorMessage = 'Redundant alt attribute. Screen-readers already announce '
2020
'`img` tags as an image. You don\'t need to use the words `image`, ' +
2121
'`photo,` or `picture` in the alt prop.';
2222

23-
const validTypes = [
24-
'LITERAL',
25-
'TEMPLATELITERAL',
26-
];
27-
2823
module.exports = context => ({
2924
JSXOpeningElement: node => {
3025
const type = elementType(node);
@@ -38,20 +33,10 @@ module.exports = context => ({
3833
return;
3934
}
4035

41-
// Only check literals, as we should not enforce variable names :P
42-
const normalizedType = altProp.value &&
43-
altProp.value.type.toUpperCase() === 'JSXEXPRESSIONCONTAINER' ?
44-
altProp.value.expression.type.toUpperCase() :
45-
altProp.value.type.toUpperCase();
46-
47-
if (validTypes.indexOf(normalizedType) === -1) {
48-
return;
49-
}
50-
51-
const value = getPropValue(altProp);
36+
const value = getLiteralPropValue(altProp);
5237
const isVisible = isHiddenFromScreenReader(type, node.attributes) === false;
5338

54-
if (Boolean(value) && typeof value === 'string' && isVisible) {
39+
if (typeof value === 'string' && isVisible) {
5540
const hasRedundancy = REDUNDANT_WORDS
5641
.some(word => Boolean(value.match(new RegExp(`(?!{)${word}(?!})`, 'gi'))));
5742

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ruleTester.run('img-redundant-alt', rule, {
5555
{ code: '<img aria-hidden={false} alt="Doing cool things." />', parserOptions },
5656
{ code: '<UX.Layout>test</UX.Layout>', parserOptions },
5757
{ code: '<img alt={imageAlt} />', parserOptions },
58+
{ code: '<img alt />', parserOptions },
5859
],
5960
invalid: [
6061
{ code: '<img alt="Photo of friend." />;', errors: [expectedError], parserOptions },

0 commit comments

Comments
 (0)