Skip to content

Commit

Permalink
refactor: remove es-iterator-helpers dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey authored and ljharb committed Oct 25, 2024
1 parent 83150db commit ffa0523
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 49 deletions.
7 changes: 2 additions & 5 deletions __tests__/src/rules/role-has-required-aria-props-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

import { roles } from 'aria-query';
import { RuleTester } from 'eslint';
import iterFrom from 'es-iterator-helpers/Iterator.from';
import map from 'es-iterator-helpers/Iterator.prototype.map';
import toArray from 'es-iterator-helpers/Iterator.prototype.toArray';

import parserOptionsMapper from '../../__util__/parserOptionsMapper';
import parsers from '../../__util__/helpers/parsers';
Expand Down Expand Up @@ -42,7 +39,7 @@ const componentsSettings = {
};

// Create basic test cases using all valid role types.
const basicValidityTests = toArray(map(iterFrom(roles.keys()), (role) => {
const basicValidityTests = roles.keys().map((role) => {
const {
requiredProps: requiredPropKeyValues,
} = roles.get(role);
Expand All @@ -52,7 +49,7 @@ const basicValidityTests = toArray(map(iterFrom(roles.keys()), (role) => {
return {
code: `<div role="${role.toLowerCase()}" ${propChain} />`,
};
}));
});

ruleTester.run('role-has-required-aria-props', rule, {
valid: parsers.all([].concat(
Expand Down
13 changes: 4 additions & 9 deletions __tests__/src/rules/role-supports-aria-props-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import {
roles,
} from 'aria-query';
import { RuleTester } from 'eslint';
import iterFrom from 'es-iterator-helpers/Iterator.from';
import filter from 'es-iterator-helpers/Iterator.prototype.filter';
import map from 'es-iterator-helpers/Iterator.prototype.map';
import toArray from 'es-iterator-helpers/Iterator.prototype.toArray';

import parserOptionsMapper from '../../__util__/parserOptionsMapper';
import parsers from '../../__util__/helpers/parsers';
Expand Down Expand Up @@ -48,15 +44,14 @@ const componentsSettings = {
},
};

const nonAbstractRoles = toArray(filter(iterFrom(roles.keys()), (role) => roles.get(role).abstract === false));
const nonAbstractRoles = roles.keys().filter((role) => roles.get(role).abstract === false);

const createTests = (rolesNames) => rolesNames.reduce((tests, role) => {
const {
props: propKeyValues,
} = roles.get(role);
const validPropsForRole = Object.keys(propKeyValues);
const invalidPropsForRole = filter(
map(iterFrom(aria.keys()), (attribute) => attribute.toLowerCase()),
const invalidPropsForRole = aria.keys().map((attribute) => attribute.toLowerCase()).filter(
(attribute) => validPropsForRole.indexOf(attribute) === -1,
);
const normalRole = role.toLowerCase();
Expand All @@ -65,10 +60,10 @@ const createTests = (rolesNames) => rolesNames.reduce((tests, role) => {
tests[0].concat(validPropsForRole.map((prop) => ({
code: `<div role="${normalRole}" ${prop.toLowerCase()} />`,
}))),
tests[1].concat(toArray(map(invalidPropsForRole, (prop) => ({
tests[1].concat(invalidPropsForRole.map((prop) => ({
code: `<div role="${normalRole}" ${prop.toLowerCase()} />`,
errors: [errorMessage(prop.toLowerCase(), normalRole, 'div', false)],
})))),
}))),
];
}, [[], []]);

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"axe-core": "^4.10.0",
"axobject-query": "^4.1.0",
"damerau-levenshtein": "^1.0.8",
"es-iterator-helpers": "^1.1.0",
"hasown": "^2.0.2",
"jsx-ast-utils": "^3.3.5",
"language-tags": "^1.0.9",
Expand Down
4 changes: 1 addition & 3 deletions src/rules/aria-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import { dom, roles } from 'aria-query';
import { getLiteralPropValue, propName } from 'jsx-ast-utils';
import iterFrom from 'es-iterator-helpers/Iterator.from';
import filter from 'es-iterator-helpers/Iterator.prototype.filter';

import getElementType from '../util/getElementType';
import { generateObjSchema } from '../util/schemas';
Expand All @@ -31,7 +29,7 @@ const schema = generateObjSchema({
},
});

const validRoles = new Set(filter(iterFrom(roles.keys()), (role) => roles.get(role).abstract === false));
const validRoles = new Set(roles.keys().filter((role) => roles.get(role).abstract === false));

export default {
meta: {
Expand Down
3 changes: 1 addition & 2 deletions src/rules/interactive-supports-focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import getTabIndex from '../util/getTabIndex';
// ----------------------------------------------------------------------------

const schema = generateObjSchema({
// TODO: convert to use iterFilter and iterFrom
tabbable: enumArraySchema([...roles.keys()].filter((name) => (
tabbable: enumArraySchema(roles.keys().filter((name) => (
!roles.get(name).abstract
&& roles.get(name).superClass.some((klasses) => klasses.includes('widget'))
))),
Expand Down
6 changes: 2 additions & 4 deletions src/rules/role-supports-aria-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
getPropValue,
propName,
} from 'jsx-ast-utils';
import iterFrom from 'es-iterator-helpers/Iterator.from';
import filter from 'es-iterator-helpers/Iterator.prototype.filter';

import { generateObjSchema } from '../util/schemas';
import getElementType from '../util/getElementType';
Expand Down Expand Up @@ -64,11 +62,11 @@ export default {
return;
}

// Make sure it has no aria-* properties defined outside of its property set.
// Make sure it has no aria-* properties defined outside its property set.
const {
props: propKeyValues,
} = roles.get(roleValue);
const invalidAriaPropsForRole = new Set(filter(iterFrom(aria.keys()), (attribute) => !(attribute in propKeyValues)));
const invalidAriaPropsForRole = new Set(aria.keys().filter((attribute) => !(attribute in propKeyValues)));

node.attributes.filter((prop) => (
getPropValue(prop) != null // Ignore the attribute if its value is null or undefined.
Expand Down
4 changes: 1 addition & 3 deletions src/util/isAbstractRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import {
roles,
} from 'aria-query';
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import iterFrom from 'es-iterator-helpers/Iterator.from';
import filter from 'es-iterator-helpers/Iterator.prototype.filter';

const abstractRoles = new Set(filter(iterFrom(roles.keys()), (role) => roles.get(role).abstract));
const abstractRoles = new Set(roles.keys().filter((role) => roles.get(role).abstract));

const DOMElements = new Set(dom.keys());

Expand Down
15 changes: 4 additions & 11 deletions src/util/isInteractiveElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import {
elementAXObjects,
} from 'axobject-query';
import flatMap from 'array.prototype.flatmap';
import iterFrom from 'es-iterator-helpers/Iterator.from';
// import iterFlatMap from 'es-iterator-helpers/Iterator.prototype.flatMap';
import filter from 'es-iterator-helpers/Iterator.prototype.filter';
import some from 'es-iterator-helpers/Iterator.prototype.some';

import attributesComparator from './attributesComparator';

Expand Down Expand Up @@ -54,21 +50,18 @@ const interactiveRoles = new Set(roleKeys
'toolbar',
));

// TODO: convert to use iterFlatMap and iterFrom
const interactiveElementRoleSchemas = flatMap(
elementRoleEntries,
([elementSchema, rolesArr]) => (rolesArr.some((role): boolean => interactiveRoles.has(role)) ? [elementSchema] : []),
);

// TODO: convert to use iterFlatMap and iterFrom
const nonInteractiveElementRoleSchemas = flatMap(
elementRoleEntries,
([elementSchema, rolesArr]) => (rolesArr.every((role): boolean => nonInteractiveRoles.has(role)) ? [elementSchema] : []),
);

const interactiveAXObjects = new Set(filter(iterFrom(AXObjects.keys()), (name) => AXObjects.get(name).type === 'widget'));
const interactiveAXObjects = new Set(AXObjects.keys().filter((name) => AXObjects.get(name).type === 'widget'));

// TODO: convert to use iterFlatMap and iterFrom
const interactiveElementAXObjectSchemas = flatMap(
[...elementAXObjects],
([elementSchema, AXObjectsArr]) => (AXObjectsArr.every((role): boolean => interactiveAXObjects.has(role)) ? [elementSchema] : []),
Expand All @@ -84,18 +77,18 @@ function checkIsInteractiveElement(tagName, attributes): boolean {

// Check in elementRoles for inherent interactive role associations for
// this element.
const isInherentInteractiveElement = some(iterFrom(interactiveElementRoleSchemas), elementSchemaMatcher);
const isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentInteractiveElement) {
return true;
}
// Check in elementRoles for inherent non-interactive role associations for
// this element.
const isInherentNonInteractiveElement = some(iterFrom(nonInteractiveElementRoleSchemas), elementSchemaMatcher);
const isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentNonInteractiveElement) {
return false;
}
// Check in elementAXObjects for AX Tree associations for this element.
const isInteractiveAXElement = some(iterFrom(interactiveElementAXObjectSchemas), elementSchemaMatcher);
const isInteractiveAXElement = interactiveElementAXObjectSchemas.some(elementSchemaMatcher);
if (isInteractiveAXElement) {
return true;
}
Expand Down
15 changes: 4 additions & 11 deletions src/util/isNonInteractiveElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import {
} from 'axobject-query';
import type { Node } from 'ast-types-flow';
import flatMap from 'array.prototype.flatmap';
import iterFrom from 'es-iterator-helpers/Iterator.from';
// import iterFlatMap from 'es-iterator-helpers/Iterator.prototype.flatMap';
import filter from 'es-iterator-helpers/Iterator.prototype.filter';
import some from 'es-iterator-helpers/Iterator.prototype.some';

import attributesComparator from './attributesComparator';

Expand Down Expand Up @@ -61,21 +57,18 @@ const interactiveRoles = new Set(roleKeys
'toolbar',
));

// TODO: convert to use iterFlatMap and iterFrom
const interactiveElementRoleSchemas = flatMap(
elementRoleEntries,
([elementSchema, rolesArr]) => (rolesArr.some((role): boolean => interactiveRoles.has(role)) ? [elementSchema] : []),
);

// TODO: convert to use iterFlatMap and iterFrom
const nonInteractiveElementRoleSchemas = flatMap(
elementRoleEntries,
([elementSchema, rolesArr]) => (rolesArr.every((role): boolean => nonInteractiveRoles.has(role)) ? [elementSchema] : []),
);

const nonInteractiveAXObjects = new Set(filter(iterFrom(AXObjects.keys()), (name) => ['window', 'structure'].includes(AXObjects.get(name).type)));
const nonInteractiveAXObjects = new Set(AXObjects.keys().filter((name) => ['window', 'structure'].includes(AXObjects.get(name).type)));

// TODO: convert to use iterFlatMap and iterFrom
const nonInteractiveElementAXObjectSchemas = flatMap(
[...elementAXObjects],
([elementSchema, AXObjectsArr]) => (AXObjectsArr.every((role): boolean => nonInteractiveAXObjects.has(role)) ? [elementSchema] : []),
Expand All @@ -91,18 +84,18 @@ function checkIsNonInteractiveElement(tagName, attributes): boolean {
}
// Check in elementRoles for inherent non-interactive role associations for
// this element.
const isInherentNonInteractiveElement = some(iterFrom(nonInteractiveElementRoleSchemas), elementSchemaMatcher);
const isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentNonInteractiveElement) {
return true;
}
// Check in elementRoles for inherent interactive role associations for
// this element.
const isInherentInteractiveElement = some(iterFrom(interactiveElementRoleSchemas), elementSchemaMatcher);
const isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentInteractiveElement) {
return false;
}
// Check in elementAXObjects for AX Tree associations for this element.
const isNonInteractiveAXElement = some(iterFrom(nonInteractiveElementAXObjectSchemas), elementSchemaMatcher);
const isNonInteractiveAXElement = nonInteractiveElementAXObjectSchemas.some(elementSchemaMatcher);
if (isNonInteractiveAXElement) {
return true;
}
Expand Down

0 comments on commit ffa0523

Please sign in to comment.