diff --git a/lib/utils/get-role.js b/lib/utils/get-role.js index df98b39e..8fea9178 100644 --- a/lib/utils/get-role.js +++ b/lib/utils/get-role.js @@ -81,7 +81,7 @@ function getRole(context, node) { } const value = getLiteralPropValue(propOnNode) - if (value || (value === '' && prop === 'alt')) { + if (propOnNode) { if ( prop === 'href' || prop === 'aria-labelledby' || @@ -90,7 +90,7 @@ function getRole(context, node) { (prop === 'alt' && value !== '') ) { key.attributes.push({name: prop, constraints: ['set']}) - } else { + } else if (value || (value === '' && prop === 'alt')) { key.attributes.push({name: prop, value}) } } diff --git a/tests/a11y-role-supports-aria-props.js b/tests/a11y-role-supports-aria-props.js index c4bbbc5e..a367e454 100644 --- a/tests/a11y-role-supports-aria-props.js +++ b/tests/a11y-role-supports-aria-props.js @@ -35,11 +35,11 @@ ruleTester.run('a11y-role-supports-aria-props', rule, { {code: '
'}, {code: '
'}, {code: ''}, + {code: ''}, {code: ''}, // Don't try to evaluate expression {code: ''}, - {code: ''}, - + {code: ''}, // IMPLICIT ROLE TESTS // A TESTS - implicit role is `link` {code: ''}, diff --git a/tests/utils/get-role.js b/tests/utils/get-role.js index 9dff372a..c8d2aa5b 100644 --- a/tests/utils/get-role.js +++ b/tests/utils/get-role.js @@ -46,6 +46,27 @@ describe('getRole', function () { expect(getRole({}, node)).to.equal('link') }) + it('returns link role for with polymorphic prop set to "a" and conditional href', function () { + const node = mockJSXOpeningElement('Foo', [ + mockJSXAttribute('as', 'a'), + mockJSXConditionalAttribute('href', 'getUrl', '#', 'https://github.com/'), + ]) + expect(getRole({}, node)).to.equal('link') + }) + + it('returns link role for with polymorphic prop set to "a" and literal href', function () { + const node = mockJSXOpeningElement('Foo', [ + mockJSXAttribute('as', 'a'), + mockJSXAttribute('href', 'https://github.com/'), + ]) + expect(getRole({}, node)).to.equal('link') + }) + + it('returns generic role for with polymorphic prop set to "a" and no href', function () { + const node = mockJSXOpeningElement('Foo', [mockJSXAttribute('as', 'a')]) + expect(getRole({}, node)).to.equal('generic') + }) + it('returns region role for
with aria-label', function () { const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-label', 'something')]) expect(getRole({}, node)).to.equal('region')