Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for presence of attribute in getRole rather than the value #463

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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