Skip to content

Commit

Permalink
refactor: don't spread arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Oct 25, 2024
1 parent 2b6bf78 commit 8c1e3f2
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 35 deletions.
13 changes: 5 additions & 8 deletions __mocks__/genInteractives.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import type { JSXElementMockType } from './JSXElementMock';

const { fromEntries } = Object;

const domElements = [...dom.keys()];
const roleNames = [...roles.keys()];

const interactiveElementsMap = {
a: [{ prop: 'href', value: '#' }],
area: [{ prop: 'href', value: '#' }],
Expand Down Expand Up @@ -122,19 +119,19 @@ const nonInteractiveElementsMap: {[string]: Array<{[string]: string}>} = {
ul: [],
};

const indeterminantInteractiveElementsMap: { [key: string]: Array<any> } = fromEntries(domElements.map((name) => [name, []]));
const indeterminantInteractiveElementsMap: { [key: string]: Array<any> } = fromEntries(dom.keys().map((name) => [name, []]));

Object.keys(interactiveElementsMap)
.concat(Object.keys(nonInteractiveElementsMap))
.forEach((name) => delete indeterminantInteractiveElementsMap[name]);

const abstractRoles = roleNames.filter((role) => roles.get(role).abstract);
const abstractRoles = roles.keys().filter((role) => roles.get(role).abstract);

const nonAbstractRoles = roleNames.filter((role) => !roles.get(role).abstract);
const nonAbstractRoles = roles.keys().filter((role) => !roles.get(role).abstract);

const interactiveRoles = []
.concat(
roleNames,
roles.keys(),
// 'toolbar' does not descend from widget, but it does support
// aria-activedescendant, thus in practice we treat it as a widget.
'toolbar',
Expand All @@ -144,7 +141,7 @@ const interactiveRoles = []
&& roles.get(role).superClass.some((klasses) => klasses.includes('widget'))
));

const nonInteractiveRoles = roleNames
const nonInteractiveRoles = roles.keys()
.filter((role) => (
!roles.get(role).abstract
&& !roles.get(role).superClass.some((klasses) => klasses.includes('widget'))
Expand Down
5 changes: 2 additions & 3 deletions __tests__/src/rules/aria-props-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import getSuggestion from '../../../src/util/getSuggestion';
// -----------------------------------------------------------------------------

const ruleTester = new RuleTester();
const ariaAttributes = [...aria.keys()];

const errorMessage = (name) => {
const suggestions = getSuggestion(name, ariaAttributes);
const suggestions = getSuggestion(name, aria.keys());

if (suggestions.length > 0) {
return {
Expand All @@ -43,7 +42,7 @@ const errorMessage = (name) => {
};

// Create basic test cases using all valid role types.
const basicValidityTests = ariaAttributes.map((prop) => ({
const basicValidityTests = aria.keys().map((prop) => ({
code: `<div ${prop.toLowerCase()}="foobar" />`,
}));

Expand Down
6 changes: 2 additions & 4 deletions __tests__/src/rules/aria-role-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ const errorMessage = {
type: 'JSXAttribute',
};

const roleKeys = [...roles.keys()];

const validRoles = roleKeys.filter((role) => roles.get(role).abstract === false);
const invalidRoles = roleKeys.filter((role) => roles.get(role).abstract === true);
const validRoles = roles.keys().filter((role) => roles.get(role).abstract === false);
const invalidRoles = roles.keys().filter((role) => roles.get(role).abstract === true);

const createTests = (roleNames) => roleNames.map((role) => ({
code: `<div role="${role.toLowerCase()}" />`,
Expand Down
9 changes: 4 additions & 5 deletions __tests__/src/rules/aria-unsupported-elements-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ const errorMessage = (invalidProp) => ({
type: 'JSXOpeningElement',
});

const domElements = [...dom.keys()];
// Generate valid test cases
const roleValidityTests = domElements.map((element) => {
const roleValidityTests = dom.keys().map((element) => {
const isReserved = dom.get(element).reserved || false;
const role = isReserved ? '' : 'role';

Expand All @@ -37,7 +36,7 @@ const roleValidityTests = domElements.map((element) => {
};
});

const ariaValidityTests = domElements.map((element) => {
const ariaValidityTests = dom.keys().map((element) => {
const isReserved = dom.get(element).reserved || false;
const aria = isReserved ? '' : 'aria-hidden';

Expand All @@ -50,7 +49,7 @@ const ariaValidityTests = domElements.map((element) => {
});

// Generate invalid test cases.
const invalidRoleValidityTests = domElements
const invalidRoleValidityTests = dom.keys()
.filter((element) => dom.get(element).reserved)
.map((reservedElem) => ({
code: `<${reservedElem} role {...props} />`,
Expand All @@ -61,7 +60,7 @@ const invalidRoleValidityTests = domElements
settings: { 'jsx-a11y': { components: { Meta: 'meta' } } },
});

const invalidAriaValidityTests = domElements
const invalidAriaValidityTests = dom.keys()
.filter((element) => dom.get(element).reserved)
.map((reservedElem) => ({
code: `<${reservedElem} aria-hidden aria-role="none" {...props} />`,
Expand Down
4 changes: 1 addition & 3 deletions src/rules/aria-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { propName } from 'jsx-ast-utils';
import { generateObjSchema } from '../util/schemas';
import getSuggestion from '../util/getSuggestion';

const ariaAttributes = aria.keys();

const schema = generateObjSchema();

export default {
Expand Down Expand Up @@ -41,7 +39,7 @@ export default {
const isValid = aria.has(name);

if (isValid === false) {
const suggestions = getSuggestion(name, ariaAttributes);
const suggestions = getSuggestion(name, aria.keys());

context.report({
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/aria-unsupported-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {
return;
}

const invalidAttributes = new Set([...aria.keys(), 'role']);
const invalidAttributes = new Set(aria.keys().concat('role'));

node.attributes.forEach((prop) => {
if (prop.type === 'JSXSpreadAttribute') {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/role-has-required-aria-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default {

const normalizedValues = String(roleAttrValue).toLowerCase().split(' ');
const validRoles = normalizedValues
.filter((val) => [...roles.keys()].indexOf(val) > -1);
.filter((val) => roles.keys().indexOf(val) > -1);

// Check semantic DOM elements
// For example, <input type="checkbox" role="switch" />
Expand Down
5 changes: 2 additions & 3 deletions src/util/isInteractiveElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import flatMap from 'array.prototype.flatmap';

import attributesComparator from './attributesComparator';

const roleKeys = [...roles.keys()];
const elementRoleEntries = [...elementRoles];

const nonInteractiveRoles = new Set(roleKeys
const nonInteractiveRoles = new Set(roles.keys()
.filter((name) => {
const role = roles.get(name);
return (
Expand All @@ -34,7 +33,7 @@ const nonInteractiveRoles = new Set(roleKeys
'progressbar',
));

const interactiveRoles = new Set(roleKeys
const interactiveRoles = new Set(roles.keys()
.filter((name) => {
const role = roles.get(name);
return (
Expand Down
5 changes: 2 additions & 3 deletions src/util/isInteractiveRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type { Node } from 'ast-types-flow';
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import flatMap from 'array.prototype.flatmap';

const roles = [...rolesMap.keys()];
const interactiveRoles = roles.filter((name) => (
const interactiveRoles = rolesMap.keys().filter((name) => (
!rolesMap.get(name).abstract
&& rolesMap.get(name).superClass.some((klasses) => klasses.includes('widget'))
));
Expand Down Expand Up @@ -41,7 +40,7 @@ const isInteractiveRole = (
const normalizedValues = String(value).toLowerCase().split(' ');
const validRoles = flatMap(
normalizedValues,
(name: string) => (roles.includes(name) ? [name] : []),
(name: string) => (rolesMap.keys().includes(name) ? [name] : []),
);
if (validRoles.length > 0) {
// The first role value is a series takes precedence.
Expand Down
5 changes: 2 additions & 3 deletions src/util/isNonInteractiveElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import flatMap from 'array.prototype.flatmap';

import attributesComparator from './attributesComparator';

const roleKeys = [...roles.keys()];
const elementRoleEntries = [...elementRoles];

const nonInteractiveRoles = new Set(roleKeys
const nonInteractiveRoles = new Set(roles.keys()
.filter((name) => {
const role = roles.get(name);
return (
Expand All @@ -38,7 +37,7 @@ const nonInteractiveRoles = new Set(roleKeys
'progressbar',
));

const interactiveRoles = new Set(roleKeys
const interactiveRoles = new Set(roles.keys()
.filter((name) => {
const role = roles.get(name);
return (
Expand Down
2 changes: 1 addition & 1 deletion src/util/isNonInteractiveRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Node } from 'ast-types-flow';
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import flatMap from 'array.prototype.flatmap';

const nonInteractiveRoles = [...rolesMap.keys()].filter((name) => (
const nonInteractiveRoles = rolesMap.keys().filter((name) => (
!rolesMap.get(name).abstract
&& !rolesMap.get(name).superClass.some((klasses) => klasses.includes('widget'))
));
Expand Down

0 comments on commit 8c1e3f2

Please sign in to comment.