Skip to content

Commit d5591b7

Browse files
authored
Merge pull request #483 from github/kh-bug-in-get-role
Bug: Don't fallback to checking implicit role if `role` is set
2 parents 8ae414c + 3ecb9f1 commit d5591b7

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

lib/utils/get-role.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function getRole(context, node) {
4646
const explicitRole = getLiteralPropValue(getProp(node.attributes, 'role'))
4747
if (explicitRole) {
4848
return explicitRole
49+
} else if (getProp(node.attributes, 'role')) {
50+
// If role is set to anything other than a literal prop
51+
return undefined
4952
}
5053

5154
// Assemble a key for looking-up the element’s role in the `elementRolesMap`

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ function getErrorMessage(attribute, role) {
2929

3030
ruleTester.run('a11y-role-supports-aria-props', rule, {
3131
valid: [
32+
{
33+
code: `
34+
<div
35+
id={id}
36+
role={
37+
sectionHasHeader && rowIndex.row === 0 ? 'presentation' : 'option'
38+
}
39+
aria-label={this.props.ariaLabel}
40+
>
41+
{children}
42+
</div>`,
43+
},
3244
{code: '<Foo bar />'},
3345
{code: '<div />'},
3446
{code: '<div id="main" />'},

tests/utils/get-role.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ describe('getRole', function () {
1313
})
1414

1515
it('returns undefined when role is set to non-literal expression', function () {
16-
// <Box role={isNavigationOpen ? 'generic' : 'navigation'} />
17-
const node = mockJSXOpeningElement('Box', [
16+
// <div role={isNavigationOpen ? 'generic' : 'navigation'} />
17+
const node = mockJSXOpeningElement('div', [
1818
mockJSXConditionalAttribute('role', 'isNavigationOpen', 'generic', 'navigation'),
1919
])
2020
expect(getRole({}, node)).to.equal(undefined)

0 commit comments

Comments
 (0)