diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 9a3d7e4e24..be0e2985b3 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -6,6 +6,8 @@ 'use strict'; const values = require('object.values'); +const filter = require('es-iterator-helpers/Iterator.prototype.filter'); +const forEach = require('es-iterator-helpers/Iterator.prototype.forEach'); const Components = require('../util/Components'); const isCreateContext = require('../util/isCreateContext'); @@ -270,8 +272,10 @@ module.exports = { }); if (checkContextObjects) { // Report missing display name for all context objects - const contextsList = Array.from(contextObjects.values()).filter((v) => !v.hasDisplayName); - contextsList.forEach((contextObj) => reportMissingContextDisplayName(contextObj)); + forEach( + filter(contextObjects.values(), (v) => !v.hasDisplayName), + (contextObj) => reportMissingContextDisplayName(contextObj) + ); } }, }; diff --git a/lib/rules/jsx-no-leaked-render.js b/lib/rules/jsx-no-leaked-render.js index c36042bc1d..0fcf407fbd 100644 --- a/lib/rules/jsx-no-leaked-render.js +++ b/lib/rules/jsx-no-leaked-render.js @@ -5,6 +5,9 @@ 'use strict'; +const find = require('es-iterator-helpers/Iterator.prototype.find'); +const from = require('es-iterator-helpers/Iterator.from'); + const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); const testReactVersion = require('../util/version').testReactVersion; @@ -135,7 +138,7 @@ module.exports = { create(context) { const config = context.options[0] || {}; const validStrategies = new Set(config.validStrategies || DEFAULT_VALID_STRATEGIES); - const fixStrategy = Array.from(validStrategies)[0]; + const fixStrategy = find(from(validStrategies), () => true); return { 'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) { diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index 4349b0829f..41e6d0b828 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -6,6 +6,9 @@ 'use strict'; +const iterFrom = require('es-iterator-helpers/Iterator.from'); +const map = require('es-iterator-helpers/Iterator.prototype.map'); + const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -67,7 +70,7 @@ module.exports = { noAttributeStrings: false, }; const config = Object.assign({}, defaults, context.options[0] || {}); - config.allowedStrings = new Set(config.allowedStrings.map(trimIfString)); + config.allowedStrings = new Set(map(iterFrom(config.allowedStrings), trimIfString)); function defaultMessageId() { const ancestorIsJSXElement = arguments.length >= 1 && arguments[0]; diff --git a/lib/rules/no-invalid-html-attribute.js b/lib/rules/no-invalid-html-attribute.js index 77f6d4de6e..01d4927d2b 100644 --- a/lib/rules/no-invalid-html-attribute.js +++ b/lib/rules/no-invalid-html-attribute.js @@ -219,8 +219,9 @@ const HTML_ELEMENTS = new Set([ * Map between attributes and set of tags that the attribute is valid on * @type {Map>} */ -const COMPONENT_ATTRIBUTE_MAP = new Map(); -COMPONENT_ATTRIBUTE_MAP.set('rel', new Set(['link', 'a', 'area', 'form'])); +const COMPONENT_ATTRIBUTE_MAP = new Map([ + ['rel', new Set(['link', 'a', 'area', 'form'])], +]); const messages = { emptyIsMeaningless: 'An empty “{{attributeName}}” attribute is meaningless.', diff --git a/lib/util/Components.js b/lib/util/Components.js index c661883095..5e9e831845 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -8,6 +8,8 @@ const arrayIncludes = require('array-includes'); const fromEntries = require('object.fromentries'); const values = require('object.values'); +const iterFrom = require('es-iterator-helpers/Iterator.from'); +const map = require('es-iterator-helpers/Iterator.prototype.map'); const variableUtil = require('./variable'); const pragmaUtil = require('./pragma'); @@ -267,17 +269,15 @@ function mergeRules(rules) { }); }); - /** @type {{[key: string]: Function}} */ - const rule = {}; - handlersByKey.forEach((fns, key) => { - rule[key] = function mergedHandler(node) { - fns.forEach((fn) => { + /** @type {{ [key: string]: Function }} */ + return fromEntries(map(iterFrom(handlersByKey), (entry) => [ + entry[0], + function mergedHandler(node) { + entry[1].forEach((fn) => { fn(node); }); - }; - }); - - return rule; + }, + ])); } function componentRule(rule, context) { diff --git a/lib/util/linkComponents.js b/lib/util/linkComponents.js index 409e3283f5..46d9e0e27c 100644 --- a/lib/util/linkComponents.js +++ b/lib/util/linkComponents.js @@ -4,6 +4,9 @@ 'use strict'; +const iterFrom = require('es-iterator-helpers/Iterator.from'); +const map = require('es-iterator-helpers/Iterator.prototype.map'); + /** TODO: type {(string | { name: string, linkAttribute: string })[]} */ /** @type {any} */ const DEFAULT_LINK_COMPONENTS = ['a']; @@ -19,7 +22,7 @@ function getFormComponents(context) { const formComponents = /** @type {typeof DEFAULT_FORM_COMPONENTS} */ ( DEFAULT_FORM_COMPONENTS.concat(settings.formComponents || []) ); - return new Map(formComponents.map((value) => { + return new Map(map(iterFrom(formComponents), (value) => { if (typeof value === 'string') { return [value, DEFAULT_FORM_ATTRIBUTE]; } @@ -32,7 +35,7 @@ function getLinkComponents(context) { const linkComponents = /** @type {typeof DEFAULT_LINK_COMPONENTS} */ ( DEFAULT_LINK_COMPONENTS.concat(settings.linkComponents || []) ); - return new Map(linkComponents.map((value) => { + return new Map(map(iterFrom(linkComponents), (value) => { if (typeof value === 'string') { return [value, DEFAULT_LINK_ATTRIBUTE]; } diff --git a/lib/util/propWrapper.js b/lib/util/propWrapper.js index 6b24c4eb18..66dac8803d 100644 --- a/lib/util/propWrapper.js +++ b/lib/util/propWrapper.js @@ -4,9 +4,12 @@ 'use strict'; +const filter = require('es-iterator-helpers/Iterator.prototype.filter'); +const some = require('es-iterator-helpers/Iterator.prototype.some'); + function searchPropWrapperFunctions(name, propWrapperFunctions) { const splitName = name.split('.'); - return Array.from(propWrapperFunctions).some((func) => { + return some(propWrapperFunctions.values(), (func) => { if (splitName.length === 2 && func.object === splitName[0] && func.property === splitName[1]) { return true; } @@ -28,7 +31,7 @@ function isPropWrapperFunction(context, name) { function getExactPropWrapperFunctions(context) { const propWrapperFunctions = getPropWrapperFunctions(context); - const exactPropWrappers = Array.from(propWrapperFunctions).filter((func) => func.exact === true); + const exactPropWrappers = filter(propWrapperFunctions.values(), (func) => func.exact === true); return new Set(exactPropWrappers); } diff --git a/package.json b/package.json index e37bae08e4..82fd7d9c32 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "array.prototype.flatmap": "^1.3.1", "array.prototype.tosorted": "^1.1.1", "doctrine": "^2.1.0", + "es-iterator-helpers": "^1.0.12", "estraverse": "^5.3.0", "jsx-ast-utils": "^2.4.1 || ^3.0.0", "minimatch": "^3.1.2",