Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/utils/get-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ||
Expand All @@ -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})
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/a11y-role-supports-aria-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ ruleTester.run('a11y-role-supports-aria-props', rule, {
{code: '<div role />'},
{code: '<div role="presentation" {...props} />'},
{code: '<Foo.Bar baz={true} />'},
{code: '<Foo as="a" href={url} aria-label={`Issue #${title}`} />'},
{code: '<Link href="#" aria-checked />'},
// Don't try to evaluate expression
{code: '<Box aria-labelledby="some-id" role={role} />'},
{code: '<Box aria-labelledby="some-id"as={isNavigationOpen ? "div" : "nav"} />'},

{code: '<Box aria-labelledby="some-id" as={isNavigationOpen ? "div" : "nav"} />'},
// IMPLICIT ROLE TESTS
// A TESTS - implicit role is `link`
{code: '<a href="#" aria-expanded />'},
Expand Down
21 changes: 21 additions & 0 deletions tests/utils/get-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ describe('getRole', function () {
expect(getRole({}, node)).to.equal('link')
})

it('returns link role for <Foo> 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 <Foo> 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 <Foo> 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 <section> with aria-label', function () {
const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-label', 'something')])
expect(getRole({}, node)).to.equal('region')
Expand Down