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 d86c665 commit a50f5d9
Show file tree
Hide file tree
Showing 23 changed files with 150 additions and 140 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
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
40 changes: 19 additions & 21 deletions lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

'use strict';

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

const Components = require('../util/Components');
const testReactVersion = require('../util/version').testReactVersion;
const astUtil = require('../util/ast');
Expand Down Expand Up @@ -363,29 +365,25 @@ module.exports = {

'Program:exit'() {
const list = components.list();
Object.keys(list).forEach((component) => {
if (
hasOtherProperties(list[component].node)
|| list[component].useThis
|| list[component].useRef
|| list[component].invalidReturn
|| list[component].hasChildContextTypes
|| list[component].useDecorators
|| (
!componentUtil.isES5Component(list[component].node, context)
&& !componentUtil.isES6Component(list[component].node, context)
values(list)
.filter((component) => (
!hasOtherProperties(component.node)
&& !component.useThis
&& !component.useRef
&& !component.invalidReturn
&& !component.hasChildContextTypes
&& !component.useDecorators
&& !component.hasSCU
&& (
componentUtil.isES5Component(component.node, context)
|| componentUtil.isES6Component(component.node, context)
)
) {
return;
}

if (list[component].hasSCU) {
return;
}
report(context, messages.componentShouldBePure, 'componentShouldBePure', {
node: list[component].node,
))
.forEach((component) => {
report(context, messages.componentShouldBePure, 'componentShouldBePure', {
node: component.node,
});
});
});
},
};
}),
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// As for exceptions for props.children or props.className (and alike) look at
// https://github.com/jsx-eslint/eslint-plugin-react/issues/7

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

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -188,9 +190,11 @@ module.exports = {
'Program:exit'() {
const list = components.list();
// Report undeclared proptypes for all classes
Object.keys(list).filter((component) => mustBeValidated(list[component])).forEach((component) => {
reportUndeclaredPropTypes(list[component]);
});
values(list)
.filter((component) => mustBeValidated(component))
.forEach((component) => {
reportUndeclaredPropTypes(component);
});
},
};
}),
Expand Down
Loading

0 comments on commit a50f5d9

Please sign in to comment.