Skip to content

Commit 24b9922

Browse files
committed
Replaces roles.json with aria-query::roles
1 parent 60c487e commit 24b9922

File tree

10 files changed

+47
-3065
lines changed

10 files changed

+47
-3065
lines changed

__mocks__/genInteractives.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { roles } from 'aria-query';
12
import JSXAttributeMock from './JSXAttributeMock';
23
import JSXElementMock from './JSXElementMock';
34
import DOMElements from '../src/util/attributes/DOM.json';
4-
import roles from '../src/util/attributes/role.json';
55

66
const pureInteractiveElements = Object.keys(DOMElements)
77
.filter(name => DOMElements[name].interactive === true)
@@ -37,12 +37,14 @@ const nonInteractiveElementsMap = {
3737
],
3838
};
3939

40-
const interactiveRoles = Object.keys(roles).filter(
41-
role => roles[role].interactive === true
40+
const roleNames = [...roles.keys()];
41+
42+
const interactiveRoles = roleNames.filter(
43+
role => roles.get(role).interactive === true
4244
);
4345

44-
const nonInteractiveRoles = Object.keys(roles).filter(
45-
role => roles[role].interactive === false
46+
const nonInteractiveRoles = roleNames.filter(
47+
role => roles.get(role).interactive === false
4648
);
4749

4850
export function genInteractiveElements () {

__tests__/src/rules/aria-role-test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// Requirements
99
// -----------------------------------------------------------------------------
1010

11+
import { roles } from 'aria-query';
1112
import { RuleTester } from 'eslint';
1213
import assign from 'object-assign';
1314
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1415
import rule from '../../../src/rules/aria-role';
15-
import ROLES from '../../../src/util/attributes/role.json';
1616

1717
// -----------------------------------------------------------------------------
1818
// Tests
@@ -25,10 +25,14 @@ const errorMessage = {
2525
type: 'JSXAttribute',
2626
};
2727

28-
const validRoles = Object.keys(ROLES).filter(role => ROLES[role].abstract === false);
29-
const invalidRoles = Object.keys(ROLES).filter(role => ROLES[role].abstract === true);
28+
const validRoles = [...roles.keys()].filter(
29+
role => roles.get(role).abstract === false,
30+
);
31+
const invalidRoles = [...roles.keys()].filter(
32+
role => roles.get(role).abstract === true,
33+
);
3034

31-
const createTests = roles => roles.map(role => ({
35+
const createTests = roleNames => roleNames.map(role => ({
3236
code: `<div role="${role.toLowerCase()}" />`,
3337
}));
3438

__tests__/src/rules/role-has-required-aria-props-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// Requirements
1010
// -----------------------------------------------------------------------------
1111

12+
import { roles } from 'aria-query';
1213
import { RuleTester } from 'eslint';
1314
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
14-
import validRoleTypes from '../../../src/util/attributes/role.json';
1515
import rule from '../../../src/rules/role-has-required-aria-props';
1616

1717
// -----------------------------------------------------------------------------
@@ -21,7 +21,7 @@ import rule from '../../../src/rules/role-has-required-aria-props';
2121
const ruleTester = new RuleTester();
2222

2323
const errorMessage = (role) => {
24-
const requiredProps = validRoleTypes[role.toUpperCase()].requiredProps.toString().toLowerCase();
24+
const requiredProps = roles.get(role).requiredProps.toString().toLowerCase();
2525

2626
return {
2727
message: `Elements with the ARIA role "${role}" must have the following ` +
@@ -32,8 +32,8 @@ const errorMessage = (role) => {
3232

3333

3434
// Create basic test cases using all valid role types.
35-
const basicValidityTests = Object.keys(validRoleTypes).map((role) => {
36-
const { requiredProps } = validRoleTypes[role];
35+
const basicValidityTests = [...roles.keys()].map((role) => {
36+
const { requiredProps } = roles.get(role);
3737
const propChain = requiredProps.join(' ').toLowerCase();
3838

3939
return {

__tests__/src/rules/role-supports-aria-props-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// Requirements
99
// -----------------------------------------------------------------------------
1010

11+
import { roles } from 'aria-query';
1112
import { RuleTester } from 'eslint';
1213
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1314
import rule from '../../../src/rules/role-supports-aria-props';
14-
import ROLES from '../../../src/util/attributes/role.json';
1515
import ARIA from '../../../src/util/attributes/ARIA.json';
1616

1717
// -----------------------------------------------------------------------------
@@ -34,11 +34,12 @@ const errorMessage = (attr, role, tag, isImplicit) => ({
3434
type: 'JSXOpeningElement',
3535
});
3636

37-
const nonAbstractRoles = Object.keys(ROLES).filter(role => ROLES[role].abstract === false);
37+
const nonAbstractRoles = [...roles.keys()].filter(role => roles.get(role).abstract === false);
3838

39-
const createTests = roles => roles.reduce((tests, role) => {
40-
const validPropsForRole = ROLES[role.toUpperCase()].props;
39+
const createTests = rolesNames => rolesNames.reduce((tests, role) => {
40+
const validPropsForRole = roles.get(role.toLowerCase()).props;
4141
const invalidPropsForRole = Object.keys(ARIA)
42+
.map(attribute => attribute.toLowerCase())
4243
.filter(attribute => validPropsForRole.indexOf(attribute) === -1);
4344
const normalRole = role.toLowerCase();
4445

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"lint": "eslint --config .eslintrc src __tests__",
2525
"lint:fix": "npm run lint -- --fix",
2626
"pretest": "npm run lint:fix && npm run flow",
27-
"test": "jest --coverage",
27+
"test": "jest --bail --coverage",
2828
"create": "node ./scripts/create-rule"
2929
},
3030
"devDependencies": {
@@ -53,6 +53,7 @@
5353
},
5454
"license": "MIT",
5555
"dependencies": {
56+
"aria-query": "^0.2.0",
5657
"ast-types-flow": "0.0.7",
5758
"damerau-levenshtein": "^1.0.0",
5859
"emoji-regex": "^6.1.0",

src/rules/aria-role.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
// Rule Definition
88
// ----------------------------------------------------------------------------
99

10+
import { roles } from 'aria-query';
1011
import { getLiteralPropValue, propName, elementType } from 'jsx-ast-utils';
1112
import { generateObjSchema } from '../util/schemas';
12-
import roles from '../util/attributes/role.json';
1313
import DOMElements from '../util/attributes/DOM.json';
1414

1515
const errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
@@ -52,8 +52,10 @@ module.exports = {
5252
// value isn't in the form of a literal.
5353
if (value === undefined || value === null) { return; }
5454

55-
const normalizedValues = String(value).toUpperCase().split(' ');
56-
const validRoles = Object.keys(roles).filter(role => roles[role].abstract === false);
55+
const normalizedValues = String(value).toLowerCase().split(' ');
56+
const validRoles = [...roles.keys()].filter(
57+
role => roles.get(role).abstract === false,
58+
);
5759
const isValid = normalizedValues.every(val => validRoles.indexOf(val) > -1);
5860

5961
if (isValid === true) { return; }

src/rules/role-has-required-aria-props.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11+
import { roles } from 'aria-query';
1112
import { getProp, getLiteralPropValue, propName } from 'jsx-ast-utils';
1213
import { generateObjSchema } from '../util/schemas';
13-
import validRoleTypes from '../util/attributes/role.json';
1414

1515
const errorMessage = (role, requiredProps) =>
1616
`Elements with the ARIA role "${role}" must have the following ` +
@@ -42,12 +42,12 @@ module.exports = {
4242
return;
4343
}
4444

45-
const normalizedValues = String(value).toUpperCase().split(' ');
45+
const normalizedValues = String(value).toLowerCase().split(' ');
4646
const validRoles = normalizedValues
47-
.filter(val => Object.keys(validRoleTypes).indexOf(val) > -1);
47+
.filter(val => [...roles.keys()].indexOf(val) > -1);
4848

4949
validRoles.forEach((role) => {
50-
const { requiredProps } = validRoleTypes[role];
50+
const { requiredProps } = roles.get(role);
5151

5252
if (requiredProps.length > 0) {
5353
const hasRequiredProps = requiredProps

src/rules/role-supports-aria-props.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11+
import { roles } from 'aria-query';
1112
import { getProp, getLiteralPropValue, elementType, propName } from 'jsx-ast-utils';
1213
import { generateObjSchema } from '../util/schemas';
13-
import ROLES from '../util/attributes/role.json';
1414
import ARIA from '../util/attributes/ARIA.json';
1515
import getImplicitRole from '../util/getImplicitRole';
1616

@@ -42,13 +42,17 @@ module.exports = {
4242
// If there is no explicit or implicit role, then assume that the element
4343
// can handle the global set of aria-* properties.
4444
// This actually isn't true - should fix in future release.
45-
if (typeof roleValue !== 'string' || ROLES[roleValue.toUpperCase()] === undefined) {
45+
if (
46+
typeof roleValue !== 'string'
47+
|| roles.get(roleValue.toLowerCase()) === undefined
48+
) {
4649
return;
4750
}
4851

4952
// Make sure it has no aria-* properties defined outside of its property set.
50-
const propertySet = ROLES[roleValue.toUpperCase()].props;
53+
const propertySet = roles.get(roleValue.toLowerCase()).props;
5154
const invalidAriaPropsForRole = Object.keys(ARIA)
55+
.map(attribute => attribute.toLowerCase())
5256
.filter(attribute => propertySet.indexOf(attribute) === -1);
5357

5458
node.attributes.forEach((prop) => {
@@ -57,7 +61,7 @@ module.exports = {
5761
}
5862

5963
const name = propName(prop);
60-
const normalizedName = name ? name.toUpperCase() : '';
64+
const normalizedName = name ? name.toLowerCase() : '';
6165

6266
if (invalidAriaPropsForRole.indexOf(normalizedName) > -1) {
6367
context.report({

0 commit comments

Comments
 (0)