Skip to content

Commit 0c0441f

Browse files
authored
Merge pull request #463 from github/kh-use-prop-value
Check for presence of attribute in `getRole` rather than the value
2 parents 125ac51 + 8c5f809 commit 0c0441f

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

lib/utils/get-role.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function getRole(context, node) {
8181
}
8282

8383
const value = getLiteralPropValue(propOnNode)
84-
if (value || (value === '' && prop === 'alt')) {
84+
if (propOnNode) {
8585
if (
8686
prop === 'href' ||
8787
prop === 'aria-labelledby' ||
@@ -90,7 +90,7 @@ function getRole(context, node) {
9090
(prop === 'alt' && value !== '')
9191
) {
9292
key.attributes.push({name: prop, constraints: ['set']})
93-
} else {
93+
} else if (value || (value === '' && prop === 'alt')) {
9494
key.attributes.push({name: prop, value})
9595
}
9696
}

tests/a11y-role-supports-aria-props.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ ruleTester.run('a11y-role-supports-aria-props', rule, {
3535
{code: '<div role />'},
3636
{code: '<div role="presentation" {...props} />'},
3737
{code: '<Foo.Bar baz={true} />'},
38+
{code: '<Foo as="a" href={url} aria-label={`Issue #${title}`} />'},
3839
{code: '<Link href="#" aria-checked />'},
3940
// Don't try to evaluate expression
4041
{code: '<Box aria-labelledby="some-id" role={role} />'},
41-
{code: '<Box aria-labelledby="some-id"as={isNavigationOpen ? "div" : "nav"} />'},
42-
42+
{code: '<Box aria-labelledby="some-id" as={isNavigationOpen ? "div" : "nav"} />'},
4343
// IMPLICIT ROLE TESTS
4444
// A TESTS - implicit role is `link`
4545
{code: '<a href="#" aria-expanded />'},

tests/utils/get-role.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ describe('getRole', function () {
4646
expect(getRole({}, node)).to.equal('link')
4747
})
4848

49+
it('returns link role for <Foo> with polymorphic prop set to "a" and conditional href', function () {
50+
const node = mockJSXOpeningElement('Foo', [
51+
mockJSXAttribute('as', 'a'),
52+
mockJSXConditionalAttribute('href', 'getUrl', '#', 'https://github.com/'),
53+
])
54+
expect(getRole({}, node)).to.equal('link')
55+
})
56+
57+
it('returns link role for <Foo> with polymorphic prop set to "a" and literal href', function () {
58+
const node = mockJSXOpeningElement('Foo', [
59+
mockJSXAttribute('as', 'a'),
60+
mockJSXAttribute('href', 'https://github.com/'),
61+
])
62+
expect(getRole({}, node)).to.equal('link')
63+
})
64+
65+
it('returns generic role for <Foo> with polymorphic prop set to "a" and no href', function () {
66+
const node = mockJSXOpeningElement('Foo', [mockJSXAttribute('as', 'a')])
67+
expect(getRole({}, node)).to.equal('generic')
68+
})
69+
4970
it('returns region role for <section> with aria-label', function () {
5071
const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-label', 'something')])
5172
expect(getRole({}, node)).to.equal('region')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy