Skip to content

Commit 4a2dd98

Browse files
v1rtlljharb
authored andcommitted
[Refactor] remove array.prototype.flatmap
1 parent 2b6bf78 commit 4a2dd98

File tree

9 files changed

+16
-50
lines changed

9 files changed

+16
-50
lines changed

__tests__/__util__/helpers/parsers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path';
22
import semver from 'semver';
33
import { version } from 'eslint/package.json';
4-
import flatMap from 'array.prototype.flatmap';
4+
5+
const { entries } = Object;
56

67
let tsParserVersion;
78
try {
@@ -22,7 +23,7 @@ function minEcmaVersion(features, parserOptions) {
2223
const result = Math.max(
2324
...[].concat(
2425
(parserOptions && parserOptions.ecmaVersion) || [],
25-
flatMap(Object.entries(minEcmaVersionForFeatures), (entry) => {
26+
entries(minEcmaVersionForFeatures).flatMap((entry) => {
2627
const f = entry[0];
2728
const y = entry[1];
2829
return features.has(f) ? y : [];
@@ -68,7 +69,7 @@ const parsers = {
6869
};
6970
},
7071
all: function all(tests) {
71-
const t = flatMap(tests, (test) => {
72+
const t = tests.flatMap((test) => {
7273
/* eslint no-param-reassign: 0 */
7374
if (typeof test === 'string') {
7475
test = { code: test };

__tests__/__util__/ruleOptionsMapperFactory.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* @flow
33
*/
44

5-
import flatMap from 'array.prototype.flatmap';
6-
75
const { fromEntries, entries } = Object;
86

97
type ESLintTestRunnerTestCase = {
@@ -25,7 +23,7 @@ export default function ruleOptionsMapperFactory(ruleOptions: Array<mixed> = [])
2523
code,
2624
errors,
2725
// Flatten the array of objects in an array of one object.
28-
options: [fromEntries(flatMap((options || []).concat(ruleOptions), (item) => entries(item)))],
26+
options: [fromEntries((options || []).concat(ruleOptions).flatMap((item) => entries(item)))],
2927
parserOptions,
3028
settings,
3129
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"license": "MIT",
7575
"dependencies": {
7676
"aria-query": "^5.3.2",
77-
"array.prototype.flatmap": "^1.3.2",
7877
"ast-types-flow": "^0.0.8",
7978
"axe-core": "^4.10.0",
8079
"axobject-query": "^4.1.0",

src/rules/alt-text.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
getPropValue,
1313
getLiteralPropValue,
1414
} from 'jsx-ast-utils';
15-
import flatMap from 'array.prototype.flatmap';
1615

1716
import { generateObjSchema, arraySchema } from '../util/schemas';
1817
import getElementType from '../util/getElementType';
@@ -222,10 +221,8 @@ export default {
222221
// Elements to validate for alt text.
223222
const elementOptions = options.elements || DEFAULT_ELEMENTS;
224223
// Get custom components for just the elements that will be tested.
225-
const customComponents = flatMap(
226-
elementOptions,
227-
(element) => options[element],
228-
);
224+
const customComponents = elementOptions.flatMap((element) => options[element]);
225+
229226
const typesToValidate = new Set(
230227
[].concat(
231228
customComponents,

src/rules/media-has-caption.js

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

1111
import type { JSXElement, JSXOpeningElement, Node } from 'ast-types-flow';
1212
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
13-
import flatMap from 'array.prototype.flatmap';
1413

1514
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
1615
import { generateObjSchema, arraySchema } from '../util/schemas';
@@ -29,7 +28,7 @@ const schema = generateObjSchema({
2928
const isMediaType = (context, type) => {
3029
const options = context.options[0] || {};
3130
return MEDIA_TYPES
32-
.concat(flatMap(MEDIA_TYPES, (mediaType) => options[mediaType]))
31+
.concat(MEDIA_TYPES.flatMap((mediaType) => options[mediaType]))
3332
.some((typeToCheck) => typeToCheck === type);
3433
};
3534

src/util/isInteractiveElement.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
AXObjects,
1212
elementAXObjects,
1313
} from 'axobject-query';
14-
import flatMap from 'array.prototype.flatmap';
1514

1615
import attributesComparator from './attributesComparator';
1716

@@ -50,22 +49,13 @@ const interactiveRoles = new Set(roleKeys
5049
'toolbar',
5150
));
5251

53-
const interactiveElementRoleSchemas = flatMap(
54-
elementRoleEntries,
55-
([elementSchema, rolesArr]) => (rolesArr.some((role): boolean => interactiveRoles.has(role)) ? [elementSchema] : []),
56-
);
52+
const interactiveElementRoleSchemas = elementRoleEntries.flatMap(([elementSchema, rolesArr]) => (rolesArr.some((role): boolean => interactiveRoles.has(role)) ? [elementSchema] : []));
5753

58-
const nonInteractiveElementRoleSchemas = flatMap(
59-
elementRoleEntries,
60-
([elementSchema, rolesArr]) => (rolesArr.every((role): boolean => nonInteractiveRoles.has(role)) ? [elementSchema] : []),
61-
);
54+
const nonInteractiveElementRoleSchemas = elementRoleEntries.flatMap(([elementSchema, rolesArr]) => (rolesArr.every((role): boolean => nonInteractiveRoles.has(role)) ? [elementSchema] : []));
6255

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

65-
const interactiveElementAXObjectSchemas = flatMap(
66-
[...elementAXObjects],
67-
([elementSchema, AXObjectsArr]) => (AXObjectsArr.every((role): boolean => interactiveAXObjects.has(role)) ? [elementSchema] : []),
68-
);
58+
const interactiveElementAXObjectSchemas = [...elementAXObjects].flatMap(([elementSchema, AXObjectsArr]) => (AXObjectsArr.every((role): boolean => interactiveAXObjects.has(role)) ? [elementSchema] : []));
6959

7060
function checkIsInteractiveElement(tagName, attributes): boolean {
7161
function elementSchemaMatcher(elementSchema) {

src/util/isInteractiveRole.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { roles as rolesMap } from 'aria-query';
33
import type { Node } from 'ast-types-flow';
44
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
5-
import flatMap from 'array.prototype.flatmap';
65

76
const roles = [...rolesMap.keys()];
87
const interactiveRoles = roles.filter((name) => (
@@ -39,10 +38,7 @@ const isInteractiveRole = (
3938

4039
let isInteractive = false;
4140
const normalizedValues = String(value).toLowerCase().split(' ');
42-
const validRoles = flatMap(
43-
normalizedValues,
44-
(name: string) => (roles.includes(name) ? [name] : []),
45-
);
41+
const validRoles = normalizedValues.flatMap((name) => (roles.includes(name) ? [name] : []));
4642
if (validRoles.length > 0) {
4743
// The first role value is a series takes precedence.
4844
isInteractive = interactiveRoles.includes(validRoles[0]);

src/util/isNonInteractiveElement.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
elementAXObjects,
1313
} from 'axobject-query';
1414
import type { Node } from 'ast-types-flow';
15-
import flatMap from 'array.prototype.flatmap';
1615

1716
import attributesComparator from './attributesComparator';
1817

@@ -57,22 +56,13 @@ const interactiveRoles = new Set(roleKeys
5756
'toolbar',
5857
));
5958

60-
const interactiveElementRoleSchemas = flatMap(
61-
elementRoleEntries,
62-
([elementSchema, rolesArr]) => (rolesArr.some((role): boolean => interactiveRoles.has(role)) ? [elementSchema] : []),
63-
);
59+
const interactiveElementRoleSchemas = elementRoleEntries.flatMap(([elementSchema, rolesArr]) => (rolesArr.some((role): boolean => interactiveRoles.has(role)) ? [elementSchema] : []));
6460

65-
const nonInteractiveElementRoleSchemas = flatMap(
66-
elementRoleEntries,
67-
([elementSchema, rolesArr]) => (rolesArr.every((role): boolean => nonInteractiveRoles.has(role)) ? [elementSchema] : []),
68-
);
61+
const nonInteractiveElementRoleSchemas = elementRoleEntries.flatMap(([elementSchema, rolesArr]) => (rolesArr.every((role): boolean => nonInteractiveRoles.has(role)) ? [elementSchema] : []));
6962

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

72-
const nonInteractiveElementAXObjectSchemas = flatMap(
73-
[...elementAXObjects],
74-
([elementSchema, AXObjectsArr]) => (AXObjectsArr.every((role): boolean => nonInteractiveAXObjects.has(role)) ? [elementSchema] : []),
75-
);
65+
const nonInteractiveElementAXObjectSchemas = [...elementAXObjects].flatMap(([elementSchema, AXObjectsArr]) => (AXObjectsArr.every((role): boolean => nonInteractiveAXObjects.has(role)) ? [elementSchema] : []));
7666

7767
function checkIsNonInteractiveElement(tagName, attributes): boolean {
7868
function elementSchemaMatcher(elementSchema) {

src/util/isNonInteractiveRole.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from 'aria-query';
99
import type { Node } from 'ast-types-flow';
1010
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
11-
import flatMap from 'array.prototype.flatmap';
1211

1312
const nonInteractiveRoles = [...rolesMap.keys()].filter((name) => (
1413
!rolesMap.get(name).abstract
@@ -47,10 +46,7 @@ const isNonInteractiveRole = (
4746

4847
let isNonInteractive = false;
4948
const normalizedValues = String(role).toLowerCase().split(' ');
50-
const validRoles = flatMap(
51-
normalizedValues,
52-
(name: string) => (rolesMap.has(name) ? [name] : []),
53-
);
49+
const validRoles = normalizedValues.flatMap((name: string) => (rolesMap.has(name) ? [name] : []));
5450
if (validRoles.length > 0) {
5551
// The first role value is a series takes precedence.
5652
isNonInteractive = nonInteractiveRoles.includes(validRoles[0]);

0 commit comments

Comments
 (0)