Skip to content

Commit

Permalink
[Refactor] use toSorted, flatMap, filter, etc where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 18, 2022
1 parent 902d555 commit f17395e
Show file tree
Hide file tree
Showing 24 changed files with 158 additions and 153 deletions.
14 changes: 7 additions & 7 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

const values = require('object.values');

const Components = require('../util/Components');
const propsUtil = require('../util/props');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -362,10 +364,8 @@ module.exports = {
return;
}

const list = components.list();

Object.keys(list).forEach((component) => {
const annotation = getComponentTypeAnnotation(list[component]);
values(components.list()).forEach((component) => {
const annotation = getComponentTypeAnnotation(component);

if (annotation) {
let propType;
Expand All @@ -380,15 +380,15 @@ module.exports = {
if (propType) {
[].concat(propType).forEach((prop) => {
validatePropNaming(
list[component].node,
component.node,
prop.properties || prop.members
);
});
}
}

if (list[component].invalidProps && list[component].invalidProps.length > 0) {
reportInvalidNaming(list[component]);
if (component.invalidProps && component.invalidProps.length > 0) {
reportInvalidNaming(component);
}
});

Expand Down
18 changes: 10 additions & 8 deletions lib/rules/default-props-match-prop-types.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 Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -91,15 +93,15 @@ module.exports = {

return {
'Program:exit'() {
const list = components.list();

// If no defaultProps could be found, we don't report anything.
Object.keys(list).filter((component) => list[component].defaultProps).forEach((component) => {
reportInvalidDefaultProps(
list[component].declaredPropTypes,
list[component].defaultProps || {}
);
});
values(components.list())
.filter((component) => component.defaultProps)
.forEach((component) => {
reportInvalidDefaultProps(
component.declaredPropTypes,
component.defaultProps || {}
);
});
},
};
}),
Expand Down
6 changes: 1 addition & 5 deletions lib/rules/jsx-max-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ module.exports = {

function checkDescendant(baseDepth, children) {
baseDepth += 1;
(children || []).forEach((node) => {
if (!hasJSX(node)) {
return;
}

(children || []).filter((node) => hasJSX(node)).forEach((node) => {
if (baseDepth > maxDepth) {
report(node, baseDepth);
} else if (!isLeaf(node)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

const propName = require('jsx-ast-utils/propName');
const includes = require('array-includes');
const toSorted = require('array.prototype.tosorted');

const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');
const report = require('../util/report');
Expand Down Expand Up @@ -239,7 +241,7 @@ function generateFixerFunction(node, context, reservedList) {
const sortableAttributeGroups = getGroupsOfSortableAttributes(attributes, context);
const sortedAttributeGroups = sortableAttributeGroups
.slice(0)
.map((group) => group.slice(0).sort((a, b) => contextCompare(a, b, options)));
.map((group) => toSorted(group, (a, b) => contextCompare(a, b, options)));

return function fixFunction(fixer) {
const fixers = [];
Expand Down
11 changes: 3 additions & 8 deletions lib/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,7 @@ module.exports = {
if (!isReactImport) {
return;
}
node.specifiers.forEach((specifier) => {
if (!specifier.imported) {
return;
}
node.specifiers.filter(((s) => s.imported)).forEach((specifier) => {
checkDeprecation(node, `${MODULES[node.source.value][0]}.${specifier.imported.name}`);
});
},
Expand All @@ -212,10 +209,8 @@ module.exports = {
) {
return;
}
node.id.properties.forEach((property) => {
if (property.type !== 'RestElement' && property.key) {
checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`);
}
node.id.properties.filter((p) => p.type !== 'RestElement' && p.key).forEach((property) => {
checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`);
});
},

Expand Down
14 changes: 7 additions & 7 deletions lib/rules/no-direct-mutation-state.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 Components = require('../util/Components');
const componentUtil = require('../util/componentUtil');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -141,13 +143,11 @@ module.exports = {
},

'Program:exit'() {
const list = components.list();

Object.keys(list).forEach((key) => {
if (!isValid(list[key])) {
reportMutations(list[key]);
}
});
values(components.list())
.filter((component) => !isValid(component))
.forEach((component) => {
reportMutations(component);
});
},
};
}),
Expand Down
15 changes: 8 additions & 7 deletions lib/rules/no-multi-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

const values = require('object.values');

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -64,15 +66,14 @@ module.exports = {
return;
}

const list = components.list();

Object.keys(list).filter((component) => !isIgnored(list[component])).forEach((component, i) => {
if (i >= 1) {
values(components.list())
.filter((component) => !isIgnored(component))
.slice(1)
.forEach((component) => {
report(context, messages.onlyOneComponent, 'onlyOneComponent', {
node: list[component].node,
node: component.node,
});
}
});
});
},
};
}),
Expand Down
21 changes: 8 additions & 13 deletions lib/rules/no-object-type-as-default-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ function hasUsedObjectDestructuringSyntax(params) {

function verifyDefaultPropsDestructuring(context, properties) {
// Loop through each of the default params
properties.filter((prop) => prop.type === 'Property').forEach((prop) => {
properties.filter((prop) => prop.type === 'Property' && prop.value.type === 'AssignmentPattern').forEach((prop) => {
const propName = prop.key.name;
const propDefaultValue = prop.value;

if (propDefaultValue.type !== 'AssignmentPattern') {
return;
}

const propDefaultValueType = propDefaultValue.right.type;

if (
Expand Down Expand Up @@ -95,14 +91,13 @@ module.exports = {
create: Components.detect((context, components) => ({
'Program:exit'() {
const list = components.list();
values(list).forEach((component) => {
const node = component.node;
if (!hasUsedObjectDestructuringSyntax(node.params)) {
return;
}
const properties = node.params[0].properties;
verifyDefaultPropsDestructuring(context, properties);
});
values(list)
.filter((component) => hasUsedObjectDestructuringSyntax(component.node.params))
.forEach((component) => {
const node = component.node;
const properties = node.params[0].properties;
verifyDefaultPropsDestructuring(context, properties);
});
},
})),
};
11 changes: 7 additions & 4 deletions lib/rules/no-set-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

const values = require('object.values');

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -75,10 +77,11 @@ module.exports = {
},

'Program:exit'() {
const list = components.list();
Object.keys(list).filter((component) => !isValid(list[component])).forEach((component) => {
reportSetStateUsages(list[component]);
});
values(components.list())
.filter((component) => !isValid(component))
.forEach((component) => {
reportSetStateUsages(component);
});
},
};
}),
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,9 @@ module.exports = {
return;
}

node.properties.forEach((property) => {
if (property.type !== 'SpreadElement') {
reportErrorIfPropertyCasingTypo(property.value, property.key, false);
reportErrorIfLifecycleMethodCasingTypo(property);
}
node.properties.filter((property) => property.type !== 'SpreadElement').forEach((property) => {
reportErrorIfPropertyCasingTypo(property.value, property.key, false);
reportErrorIfLifecycleMethodCasingTypo(property);
});
},
};
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/no-unused-class-component-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ module.exports = {

// detect `{ foo, bar: baz } = this`
if (node.init && isThisExpression(node.init) && node.id.type === 'ObjectPattern') {
node.id.properties.forEach((prop) => {
if (prop.type === 'Property' && isKeyLiteralLike(prop, prop.key)) {
node.id.properties
.filter((prop) => prop.type === 'Property' && isKeyLiteralLike(prop, prop.key))
.forEach((prop) => {
addUsedProperty(prop.key);
}
});
});
}
},
};
Expand Down
14 changes: 7 additions & 7 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

const values = require('object.values');

// As for exceptions for props.children or props.className (and alike) look at
// https://github.com/jsx-eslint/eslint-plugin-react/issues/7

Expand Down Expand Up @@ -159,14 +161,12 @@ module.exports = {

return {
'Program:exit'() {
const list = components.list();
// Report undeclared proptypes for all classes
Object.keys(list).filter((component) => mustBeValidated(list[component])).forEach((component) => {
if (!mustBeValidated(list[component])) {
return;
}
reportUnusedPropTypes(list[component]);
});
values(components.list())
.filter((component) => mustBeValidated(component))
.forEach((component) => {
reportUnusedPropTypes(component);
});
},
};
}),
Expand Down
20 changes: 9 additions & 11 deletions lib/rules/prefer-read-only-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

'use strict';

const flatMap = require('array.prototype.flatmap');
const values = require('object.values');

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -49,17 +52,12 @@ module.exports = {

create: Components.detect((context, components) => ({
'Program:exit'() {
const list = components.list();

Object.keys(list).forEach((key) => {
const component = list[key];

if (!component.declaredPropTypes) {
return;
}

Object.keys(component.declaredPropTypes).forEach((propName) => {
const prop = component.declaredPropTypes[propName];
flatMap(
values(components.list()),
(component) => component.declaredPropTypes || []
).forEach((declaredPropTypes) => {
Object.keys(declaredPropTypes).forEach((propName) => {
const prop = declaredPropTypes[propName];

if (!prop.node || !isFlowPropertyType(prop.node)) {
return;
Expand Down
Loading

0 comments on commit f17395e

Please sign in to comment.