Skip to content

Commit 52de824

Browse files
committed
[Refactor] use es-iterator-helpers
- also, maximally avoid iterator spreads
1 parent 9a8edde commit 52de824

22 files changed

+94
-90
lines changed

__tests__/src/rules/aria-unsupported-elements-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const invalidAriaValidityTests = domElements
6969
}));
7070

7171
ruleTester.run('aria-unsupported-elements', rule, {
72-
valid: parsers.all([].concat(roleValidityTests.concat(ariaValidityTests))).map(parserOptionsMapper),
73-
invalid: parsers.all([].concat(invalidRoleValidityTests.concat(invalidAriaValidityTests)))
72+
valid: parsers.all([].concat(roleValidityTests, ariaValidityTests)).map(parserOptionsMapper),
73+
invalid: parsers.all([].concat(invalidRoleValidityTests, invalidAriaValidityTests))
7474
.map(parserOptionsMapper),
7575
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
import { roles } from 'aria-query';
1212
import { RuleTester } from 'eslint';
13+
import iterFrom from 'es-iterator-helpers/Iterator.from';
14+
import map from 'es-iterator-helpers/Iterator.prototype.map';
15+
import toArray from 'es-iterator-helpers/Iterator.prototype.toArray';
16+
1317
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1418
import parsers from '../../__util__/helpers/parsers';
1519
import rule from '../../../src/rules/role-has-required-aria-props';
@@ -38,7 +42,7 @@ const componentsSettings = {
3842
};
3943

4044
// Create basic test cases using all valid role types.
41-
const basicValidityTests = [...roles.keys()].map((role) => {
45+
const basicValidityTests = toArray(map(iterFrom(roles.keys()), (role) => {
4246
const {
4347
requiredProps: requiredPropKeyValues,
4448
} = roles.get(role);
@@ -48,7 +52,7 @@ const basicValidityTests = [...roles.keys()].map((role) => {
4852
return {
4953
code: `<div role="${role.toLowerCase()}" ${propChain} />`,
5054
};
51-
});
55+
}));
5256

5357
ruleTester.run('role-has-required-aria-props', rule, {
5458
valid: parsers.all([].concat(

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import {
1414
import { RuleTester } from 'eslint';
1515
import { version as eslintVersion } from 'eslint/package.json';
1616
import semver from 'semver';
17+
import iterFrom from 'es-iterator-helpers/Iterator.from';
18+
import filter from 'es-iterator-helpers/Iterator.prototype.filter';
19+
import map from 'es-iterator-helpers/Iterator.prototype.map';
20+
import toArray from 'es-iterator-helpers/Iterator.prototype.toArray';
21+
1722
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1823
import parsers from '../../__util__/helpers/parsers';
1924
import rule from '../../../src/rules/role-supports-aria-props';
@@ -26,8 +31,7 @@ const ruleTester = new RuleTester();
2631

2732
const generateErrorMessage = (attr, role, tag, isImplicit) => {
2833
if (isImplicit) {
29-
return `The attribute ${attr} is not supported by the role ${role}. \
30-
This role is implicit on the element ${tag}.`;
34+
return `The attribute ${attr} is not supported by the role ${role}. This role is implicit on the element ${tag}.`;
3135
}
3236

3337
return `The attribute ${attr} is not supported by the role ${role}.`;
@@ -46,30 +50,28 @@ const componentsSettings = {
4650
},
4751
};
4852

49-
const nonAbstractRoles = [...roles.keys()].filter((role) => roles.get(role).abstract === false);
53+
const nonAbstractRoles = toArray(filter(iterFrom(roles.keys()), (role) => roles.get(role).abstract === false));
5054

5155
const createTests = (rolesNames) => rolesNames.reduce((tests, role) => {
5256
const {
5357
props: propKeyValues,
5458
} = roles.get(role);
5559
const validPropsForRole = Object.keys(propKeyValues);
56-
const invalidPropsForRole = [...aria.keys()]
57-
.map((attribute) => attribute.toLowerCase())
58-
.filter((attribute) => validPropsForRole.indexOf(attribute) === -1);
60+
const invalidPropsForRole = filter(
61+
map(iterFrom(aria.keys()), (attribute) => attribute.toLowerCase()),
62+
(attribute) => validPropsForRole.indexOf(attribute) === -1,
63+
);
5964
const normalRole = role.toLowerCase();
6065

61-
const allTests = [];
62-
63-
allTests[0] = tests[0].concat(validPropsForRole.map((prop) => ({
64-
code: `<div role="${normalRole}" ${prop.toLowerCase()} />`,
65-
})));
66-
67-
allTests[1] = tests[1].concat(invalidPropsForRole.map((prop) => ({
68-
code: `<div role="${normalRole}" ${prop.toLowerCase()} />`,
69-
errors: [errorMessage(prop.toLowerCase(), normalRole, 'div', false)],
70-
})));
71-
72-
return allTests;
66+
return [
67+
tests[0].concat(validPropsForRole.map((prop) => ({
68+
code: `<div role="${normalRole}" ${prop.toLowerCase()} />`,
69+
}))),
70+
tests[1].concat(toArray(map(invalidPropsForRole, (prop) => ({
71+
code: `<div role="${normalRole}" ${prop.toLowerCase()} />`,
72+
errors: [errorMessage(prop.toLowerCase(), normalRole, 'div', false)],
73+
})))),
74+
];
7375
}, [[], []]);
7476

7577
const [validTests, invalidTests] = createTests(nonAbstractRoles);

__tests__/src/util/isDOMElement-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { elementType } from 'jsx-ast-utils';
44
import isDOMElement from '../../../src/util/isDOMElement';
55
import JSXElementMock from '../../../__mocks__/JSXElementMock';
66

7-
const domElements = [...dom.keys()];
8-
97
describe('isDOMElement', () => {
108
describe('DOM elements', () => {
11-
domElements.forEach((el) => {
9+
dom.forEach((_, el) => {
1210
it(`should identify ${el} as a DOM element`, () => {
1311
const element = JSXElementMock(el);
1412
expect(isDOMElement(elementType(element.openingElement)))

__tests__/src/util/isFocusable-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock';
1010

1111
function mergeTabIndex(index, attributes) {
12-
return [...attributes, JSXAttributeMock('tabIndex', index)];
12+
return [].concat(attributes, JSXAttributeMock('tabIndex', index));
1313
}
1414

1515
describe('isFocusable', () => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"axobject-query": "^3.2.1",
8484
"damerau-levenshtein": "^1.0.8",
8585
"emoji-regex": "^9.2.2",
86+
"es-iterator-helpers": "^1.0.15",
8687
"hasown": "^2.0.0",
8788
"jsx-ast-utils": "^3.3.5",
8889
"language-tags": "^1.0.9",

src/rules/aria-activedescendant-has-tabindex.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const errorMessage = 'An element that manages focus with `aria-activedescendant`
1818

1919
const schema = generateObjSchema();
2020

21-
const domElements = [...dom.keys()];
22-
2321
export default {
2422
meta: {
2523
docs: {
@@ -42,7 +40,7 @@ export default {
4240
const type = elementType(node);
4341
// Do not test higher level JSX components, as we do not know what
4442
// low-level DOM element this maps to.
45-
if (domElements.indexOf(type) === -1) {
43+
if (!dom.has(type)) {
4644
return;
4745
}
4846
const tabIndex = getTabIndex(getProp(attributes, 'tabIndex'));

src/rules/aria-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default {
4545
return;
4646
}
4747

48-
const isValid = ariaAttributes.indexOf(name) > -1;
48+
const isValid = aria.has(name);
4949

5050
if (isValid === false) {
5151
context.report({

src/rules/aria-role.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import { dom, roles } from 'aria-query';
1111
import { getLiteralPropValue, propName } from 'jsx-ast-utils';
12+
import iterFrom from 'es-iterator-helpers/Iterator.from';
13+
import filter from 'es-iterator-helpers/Iterator.prototype.filter';
14+
1215
import getElementType from '../util/getElementType';
1316
import { generateObjSchema } from '../util/schemas';
1417

@@ -28,7 +31,7 @@ const schema = generateObjSchema({
2831
},
2932
});
3033

31-
const validRoles = new Set([...roles.keys()].filter((role) => roles.get(role).abstract === false));
34+
const validRoles = new Set(filter(iterFrom(roles.keys()), (role) => roles.get(role).abstract === false));
3235

3336
export default {
3437
meta: {

src/rules/aria-unsupported-elements.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default {
4747
return;
4848
}
4949

50-
const invalidAttributes = [...aria.keys(), 'role'];
50+
const invalidAttributes = new Set([...aria.keys(), 'role']);
5151

5252
node.attributes.forEach((prop) => {
5353
if (prop.type === 'JSXSpreadAttribute') {
@@ -56,7 +56,7 @@ export default {
5656

5757
const name = propName(prop).toLowerCase();
5858

59-
if (invalidAttributes.indexOf(name) > -1) {
59+
if (invalidAttributes.has(name)) {
6060
context.report({
6161
node,
6262
message: errorMessage(name),

0 commit comments

Comments
 (0)