Skip to content

Commit e1dd37f

Browse files
committed
[Refactor] use es-iterator-helpers in a couple places
1 parent 37b02ac commit e1dd37f

8 files changed

+37
-19
lines changed

lib/rules/display-name.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
'use strict';
77

88
const values = require('object.values');
9+
const filter = require('es-iterator-helpers/Iterator.prototype.filter');
10+
const forEach = require('es-iterator-helpers/Iterator.prototype.forEach');
911

1012
const Components = require('../util/Components');
1113
const isCreateContext = require('../util/isCreateContext');
@@ -270,8 +272,10 @@ module.exports = {
270272
});
271273
if (checkContextObjects) {
272274
// Report missing display name for all context objects
273-
const contextsList = Array.from(contextObjects.values()).filter((v) => !v.hasDisplayName);
274-
contextsList.forEach((contextObj) => reportMissingContextDisplayName(contextObj));
275+
forEach(
276+
filter(contextObjects.values(), (v) => !v.hasDisplayName),
277+
(contextObj) => reportMissingContextDisplayName(contextObj)
278+
);
275279
}
276280
},
277281
};

lib/rules/jsx-no-leaked-render.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
'use strict';
77

8+
const find = require('es-iterator-helpers/Iterator.prototype.find');
9+
const from = require('es-iterator-helpers/Iterator.from');
10+
811
const docsUrl = require('../util/docsUrl');
912
const report = require('../util/report');
1013
const testReactVersion = require('../util/version').testReactVersion;
@@ -135,7 +138,7 @@ module.exports = {
135138
create(context) {
136139
const config = context.options[0] || {};
137140
const validStrategies = new Set(config.validStrategies || DEFAULT_VALID_STRATEGIES);
138-
const fixStrategy = Array.from(validStrategies)[0];
141+
const fixStrategy = find(from(validStrategies), () => true);
139142

140143
return {
141144
'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) {

lib/rules/jsx-no-literals.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
'use strict';
88

9+
const iterFrom = require('es-iterator-helpers/Iterator.from');
10+
const map = require('es-iterator-helpers/Iterator.prototype.map');
11+
912
const docsUrl = require('../util/docsUrl');
1013
const report = require('../util/report');
1114

@@ -67,7 +70,7 @@ module.exports = {
6770
noAttributeStrings: false,
6871
};
6972
const config = Object.assign({}, defaults, context.options[0] || {});
70-
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));
73+
config.allowedStrings = new Set(map(iterFrom(config.allowedStrings), trimIfString));
7174

7275
function defaultMessageId() {
7376
const ancestorIsJSXElement = arguments.length >= 1 && arguments[0];

lib/rules/no-invalid-html-attribute.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ const HTML_ELEMENTS = new Set([
219219
* Map between attributes and set of tags that the attribute is valid on
220220
* @type {Map<string, Set<string>>}
221221
*/
222-
const COMPONENT_ATTRIBUTE_MAP = new Map();
223-
COMPONENT_ATTRIBUTE_MAP.set('rel', new Set(['link', 'a', 'area', 'form']));
222+
const COMPONENT_ATTRIBUTE_MAP = new Map([
223+
['rel', new Set(['link', 'a', 'area', 'form'])],
224+
]);
224225

225226
const messages = {
226227
emptyIsMeaningless: 'An empty “{{attributeName}}” attribute is meaningless.',

lib/util/Components.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
const arrayIncludes = require('array-includes');
99
const fromEntries = require('object.fromentries');
1010
const values = require('object.values');
11+
const iterFrom = require('es-iterator-helpers/Iterator.from');
12+
const map = require('es-iterator-helpers/Iterator.prototype.map');
1113

1214
const variableUtil = require('./variable');
1315
const pragmaUtil = require('./pragma');
@@ -267,17 +269,15 @@ function mergeRules(rules) {
267269
});
268270
});
269271

270-
/** @type {{[key: string]: Function}} */
271-
const rule = {};
272-
handlersByKey.forEach((fns, key) => {
273-
rule[key] = function mergedHandler(node) {
274-
fns.forEach((fn) => {
272+
/** @type {{ [key: string]: Function }} */
273+
return fromEntries(map(iterFrom(handlersByKey), (entry) => [
274+
entry[0],
275+
function mergedHandler(node) {
276+
entry[1].forEach((fn) => {
275277
fn(node);
276278
});
277-
};
278-
});
279-
280-
return rule;
279+
},
280+
]));
281281
}
282282

283283
function componentRule(rule, context) {

lib/util/linkComponents.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
'use strict';
66

7+
const iterFrom = require('es-iterator-helpers/Iterator.from');
8+
const map = require('es-iterator-helpers/Iterator.prototype.map');
9+
710
/** TODO: type {(string | { name: string, linkAttribute: string })[]} */
811
/** @type {any} */
912
const DEFAULT_LINK_COMPONENTS = ['a'];
@@ -19,7 +22,7 @@ function getFormComponents(context) {
1922
const formComponents = /** @type {typeof DEFAULT_FORM_COMPONENTS} */ (
2023
DEFAULT_FORM_COMPONENTS.concat(settings.formComponents || [])
2124
);
22-
return new Map(formComponents.map((value) => {
25+
return new Map(map(iterFrom(formComponents), (value) => {
2326
if (typeof value === 'string') {
2427
return [value, DEFAULT_FORM_ATTRIBUTE];
2528
}
@@ -32,7 +35,7 @@ function getLinkComponents(context) {
3235
const linkComponents = /** @type {typeof DEFAULT_LINK_COMPONENTS} */ (
3336
DEFAULT_LINK_COMPONENTS.concat(settings.linkComponents || [])
3437
);
35-
return new Map(linkComponents.map((value) => {
38+
return new Map(map(iterFrom(linkComponents), (value) => {
3639
if (typeof value === 'string') {
3740
return [value, DEFAULT_LINK_ATTRIBUTE];
3841
}

lib/util/propWrapper.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
'use strict';
66

7+
const filter = require('es-iterator-helpers/Iterator.prototype.filter');
8+
const some = require('es-iterator-helpers/Iterator.prototype.some');
9+
710
function searchPropWrapperFunctions(name, propWrapperFunctions) {
811
const splitName = name.split('.');
9-
return Array.from(propWrapperFunctions).some((func) => {
12+
return some(propWrapperFunctions.values(), (func) => {
1013
if (splitName.length === 2 && func.object === splitName[0] && func.property === splitName[1]) {
1114
return true;
1215
}
@@ -28,7 +31,7 @@ function isPropWrapperFunction(context, name) {
2831

2932
function getExactPropWrapperFunctions(context) {
3033
const propWrapperFunctions = getPropWrapperFunctions(context);
31-
const exactPropWrappers = Array.from(propWrapperFunctions).filter((func) => func.exact === true);
34+
const exactPropWrappers = filter(propWrapperFunctions.values(), (func) => func.exact === true);
3235
return new Set(exactPropWrappers);
3336
}
3437

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"array.prototype.flatmap": "^1.3.1",
3030
"array.prototype.tosorted": "^1.1.1",
3131
"doctrine": "^2.1.0",
32+
"es-iterator-helpers": "^1.0.12",
3233
"estraverse": "^5.3.0",
3334
"jsx-ast-utils": "^2.4.1 || ^3.0.0",
3435
"minimatch": "^3.1.2",

0 commit comments

Comments
 (0)