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

Flag aria-label and aria-labelledby on non-interactive elements #911

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 21 additions & 2 deletions __tests__/src/rules/role-supports-aria-props-test.js
Expand Up @@ -50,11 +50,14 @@ const nonAbstractRoles = [...roles.keys()].filter((role) => roles.get(role).abst
const createTests = (rolesNames) => rolesNames.reduce((tests, role) => {
const {
props: propKeyValues,
prohibitedProps,
} = roles.get(role);
const validPropsForRole = Object.keys(propKeyValues);
const validPropsForRole = Object.keys(propKeyValues)
.filter((attribute) => prohibitedProps.indexOf(attribute) === -1);
const invalidPropsForRole = [...aria.keys()]
.map((attribute) => attribute.toLowerCase())
.filter((attribute) => validPropsForRole.indexOf(attribute) === -1);
.filter((attribute) => validPropsForRole.indexOf(attribute) === -1)
.concat(prohibitedProps);
const normalRole = role.toLowerCase();

const allTests = [];
Expand Down Expand Up @@ -568,5 +571,21 @@ ruleTester.run('role-supports-aria-props', rule, {
errors: [errorMessage('aria-checked', 'link', 'a', true)],
settings: componentsSettings,
},
{
code: '<span aria-label />',
errors: [errorMessage('aria-label', 'generic', 'span', true)],
},
{
code: '<span aria-labelledby />',
errors: [errorMessage('aria-labelledby', 'generic', 'span', true)],
},
{
code: '<div aria-label />',
errors: [errorMessage('aria-label', 'generic', 'div', true)],
},
{
code: '<div aria-labelledby />',
errors: [errorMessage('aria-labelledby', 'generic', 'div', true)],
},
].concat(invalidTests).map(parserOptionsMapper),
});
6 changes: 3 additions & 3 deletions __tests__/src/util/getComputedRole-test.js
Expand Up @@ -24,7 +24,7 @@ describe('getComputedRole', () => {
describe('lacks implicit', () => {
it('should return null', () => {
expect(getComputedRole(
'div',
'custom-element',
[JSXAttributeMock('role', 'beeswax')],
)).toBeNull();
});
Expand All @@ -43,7 +43,7 @@ describe('getComputedRole', () => {
describe('lacks implicit', () => {
it('should return null', () => {
expect(getComputedRole(
'div',
'custom-element',
[],
)).toBeNull();
});
Expand All @@ -62,7 +62,7 @@ describe('getComputedRole', () => {
describe('lacks implicit', () => {
it('should return null', () => {
expect(getComputedRole(
'div',
'custom-element',
[],
)).toBeNull();
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/src/util/getExplicitRole-test.js
Expand Up @@ -14,15 +14,15 @@ describe('getExplicitRole', () => {
describe('invalid role', () => {
it('should return null', () => {
expect(getExplicitRole(
'div',
'custom-element',
[JSXAttributeMock('role', 'beeswax')],
)).toBeNull();
});
});
describe('no role', () => {
it('should return null', () => {
expect(getExplicitRole(
'div',
'custom-element',
[],
)).toBeNull();
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/src/util/getImplicitRole-test.js
Expand Up @@ -13,7 +13,7 @@ describe('getImplicitRole', () => {
describe('lacks implicit', () => {
it('should return null', () => {
expect(getImplicitRole(
'div',
'custom-element',
[],
)).toBeNull();
});
Expand Down
4 changes: 3 additions & 1 deletion src/rules/role-supports-aria-props.js
Expand Up @@ -65,9 +65,11 @@ export default {
// Make sure it has no aria-* properties defined outside of its property set.
const {
props: propKeyValues,
prohibitedProps,
} = roles.get(roleValue);
const invalidAriaPropsForRole = [...aria.keys()]
.filter((attribute) => !(attribute in propKeyValues));
.filter((attribute) => !(attribute in propKeyValues))
.concat(prohibitedProps);

node.attributes.filter((prop) => (
getPropValue(prop) != null // Ignore the attribute if its value is null or undefined.
Expand Down
6 changes: 6 additions & 0 deletions src/util/implicitRoles/div.js
@@ -0,0 +1,6 @@
/**
* Returns the implicit role for a div tag.
*/
export default function getImplicitRoleForDiv() {
return 'generic';
}
4 changes: 4 additions & 0 deletions src/util/implicitRoles/index.js
Expand Up @@ -7,6 +7,7 @@ import button from './button';
import datalist from './datalist';
import details from './details';
import dialog from './dialog';
import div from './div';
import form from './form';
import h1 from './h1';
import h2 from './h2';
Expand All @@ -29,6 +30,7 @@ import output from './output';
import progress from './progress';
import section from './section';
import select from './select';
import span from './span';
import tbody from './tbody';
import textarea from './textarea';
import tfoot from './tfoot';
Expand All @@ -45,6 +47,7 @@ export default {
datalist,
details,
dialog,
div,
form,
h1,
h2,
Expand All @@ -67,6 +70,7 @@ export default {
progress,
section,
select,
span,
tbody,
textarea,
tfoot,
Expand Down
6 changes: 6 additions & 0 deletions src/util/implicitRoles/span.js
@@ -0,0 +1,6 @@
/**
* Returns the implicit role for a span tag.
*/
export default function getImplicitRoleForSpan() {
return 'generic';
}