Skip to content

Commit

Permalink
[Refactor] use es-iterator-helpers in a couple places
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 8, 2023
1 parent 3b400d9 commit e9958d1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
8 changes: 6 additions & 2 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)
);
}
},
};
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/jsx-no-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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];
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-invalid-html-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ const HTML_ELEMENTS = new Set([
* Map between attributes and set of tags that the attribute is valid on
* @type {Map<string, Set<string>>}
*/
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.',
Expand Down
18 changes: 9 additions & 9 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions lib/util/linkComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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];
}
Expand All @@ -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];
}
Expand Down
7 changes: 5 additions & 2 deletions lib/util/propWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e9958d1

Please sign in to comment.