Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
#414 fix type error in react-ally-img-has-alt rule
Browse files Browse the repository at this point in the history
Add warning throwing test for undefined roles

Coerce possibly undefined role attribute to string

closes #414
closes #390
closes #410
  • Loading branch information
garrow authored and HamletDRC committed Feb 13, 2018
1 parent ed832e7 commit b81ad57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/reactA11yImgHasAltRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ImgHasAltWalker extends Lint.RuleWalker {
} else {
const roleAttribute: ts.JsxAttribute = attributes[ROLE_STRING];
const roleAttributeValue: string = roleAttribute ? getStringLiteral(roleAttribute) : '';
const isPresentationRole: boolean = !!roleAttributeValue.toLowerCase().match(/\bpresentation\b/);
const isPresentationRole: boolean = !!String(roleAttributeValue).toLowerCase().match(/\bpresentation\b/);
const isEmptyAlt: boolean = isEmpty(altAttribute) || getStringLiteral(altAttribute) === '';
const allowNonEmptyAltWithRolePresentation: boolean = options.length > 1
? options[1].allowNonEmptyAltWithRolePresentation
Expand Down
5 changes: 5 additions & 0 deletions src/tests/reactA11yImgHasAltRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ describe('reactA11yImgHasAlt', () => {
TestHelper.assertNoViolation(ruleName, fileName);
});

it('when the img element has non-empty alt value and undefined presentation role', () => {
const fileName: string = fileDirectory + 'ImgElementHasNonEmptyAltValueAndUndefinedPresentationRole.tsx';
TestHelper.assertNoViolation(ruleName, fileName);
});

it('when the img element has non-empty alt value and presentation role when option is enabled', () => {
const fileName: string = fileDirectory + 'ImgElementHasNonEmptyAltValueAndPresentationRole.tsx';
const ruleOptions: any[] = [[], { allowNonEmptyAltWithRolePresentation: true }];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React = require('react');

let validAltValue;
const a = <img role={undefined} alt='validAltValue' />
const b = <img role={undefined} Alt={validAltValue} />
const c = <img role={undefined} ALT={'validAltValue'} />
const d = <img role={undefined} alt={validAltValue + 'validAltValue'} />

0 comments on commit b81ad57

Please sign in to comment.