Skip to content

Commit f17395e

Browse files
committed
[Refactor] use toSorted, flatMap, filter, etc where appropriate
1 parent 902d555 commit f17395e

24 files changed

+158
-153
lines changed

lib/rules/boolean-prop-naming.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const values = require('object.values');
9+
810
const Components = require('../util/Components');
911
const propsUtil = require('../util/props');
1012
const docsUrl = require('../util/docsUrl');
@@ -362,10 +364,8 @@ module.exports = {
362364
return;
363365
}
364366

365-
const list = components.list();
366-
367-
Object.keys(list).forEach((component) => {
368-
const annotation = getComponentTypeAnnotation(list[component]);
367+
values(components.list()).forEach((component) => {
368+
const annotation = getComponentTypeAnnotation(component);
369369

370370
if (annotation) {
371371
let propType;
@@ -380,15 +380,15 @@ module.exports = {
380380
if (propType) {
381381
[].concat(propType).forEach((prop) => {
382382
validatePropNaming(
383-
list[component].node,
383+
component.node,
384384
prop.properties || prop.members
385385
);
386386
});
387387
}
388388
}
389389

390-
if (list[component].invalidProps && list[component].invalidProps.length > 0) {
391-
reportInvalidNaming(list[component]);
390+
if (component.invalidProps && component.invalidProps.length > 0) {
391+
reportInvalidNaming(component);
392392
}
393393
});
394394

lib/rules/default-props-match-prop-types.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'use strict';
88

9+
const values = require('object.values');
10+
911
const Components = require('../util/Components');
1012
const docsUrl = require('../util/docsUrl');
1113
const report = require('../util/report');
@@ -91,15 +93,15 @@ module.exports = {
9193

9294
return {
9395
'Program:exit'() {
94-
const list = components.list();
95-
9696
// If no defaultProps could be found, we don't report anything.
97-
Object.keys(list).filter((component) => list[component].defaultProps).forEach((component) => {
98-
reportInvalidDefaultProps(
99-
list[component].declaredPropTypes,
100-
list[component].defaultProps || {}
101-
);
102-
});
97+
values(components.list())
98+
.filter((component) => component.defaultProps)
99+
.forEach((component) => {
100+
reportInvalidDefaultProps(
101+
component.declaredPropTypes,
102+
component.defaultProps || {}
103+
);
104+
});
103105
},
104106
};
105107
}),

lib/rules/jsx-max-depth.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ module.exports = {
120120

121121
function checkDescendant(baseDepth, children) {
122122
baseDepth += 1;
123-
(children || []).forEach((node) => {
124-
if (!hasJSX(node)) {
125-
return;
126-
}
127-
123+
(children || []).filter((node) => hasJSX(node)).forEach((node) => {
128124
if (baseDepth > maxDepth) {
129125
report(node, baseDepth);
130126
} else if (!isLeaf(node)) {

lib/rules/jsx-sort-props.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
const propName = require('jsx-ast-utils/propName');
99
const includes = require('array-includes');
10+
const toSorted = require('array.prototype.tosorted');
11+
1012
const docsUrl = require('../util/docsUrl');
1113
const jsxUtil = require('../util/jsx');
1214
const report = require('../util/report');
@@ -239,7 +241,7 @@ function generateFixerFunction(node, context, reservedList) {
239241
const sortableAttributeGroups = getGroupsOfSortableAttributes(attributes, context);
240242
const sortedAttributeGroups = sortableAttributeGroups
241243
.slice(0)
242-
.map((group) => group.slice(0).sort((a, b) => contextCompare(a, b, options)));
244+
.map((group) => toSorted(group, (a, b) => contextCompare(a, b, options)));
243245

244246
return function fixFunction(fixer) {
245247
const fixers = [];

lib/rules/no-deprecated.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ module.exports = {
189189
if (!isReactImport) {
190190
return;
191191
}
192-
node.specifiers.forEach((specifier) => {
193-
if (!specifier.imported) {
194-
return;
195-
}
192+
node.specifiers.filter(((s) => s.imported)).forEach((specifier) => {
196193
checkDeprecation(node, `${MODULES[node.source.value][0]}.${specifier.imported.name}`);
197194
});
198195
},
@@ -212,10 +209,8 @@ module.exports = {
212209
) {
213210
return;
214211
}
215-
node.id.properties.forEach((property) => {
216-
if (property.type !== 'RestElement' && property.key) {
217-
checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`);
218-
}
212+
node.id.properties.filter((p) => p.type !== 'RestElement' && p.key).forEach((property) => {
213+
checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`);
219214
});
220215
},
221216

lib/rules/no-direct-mutation-state.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'use strict';
88

9+
const values = require('object.values');
10+
911
const Components = require('../util/Components');
1012
const componentUtil = require('../util/componentUtil');
1113
const docsUrl = require('../util/docsUrl');
@@ -141,13 +143,11 @@ module.exports = {
141143
},
142144

143145
'Program:exit'() {
144-
const list = components.list();
145-
146-
Object.keys(list).forEach((key) => {
147-
if (!isValid(list[key])) {
148-
reportMutations(list[key]);
149-
}
150-
});
146+
values(components.list())
147+
.filter((component) => !isValid(component))
148+
.forEach((component) => {
149+
reportMutations(component);
150+
});
151151
},
152152
};
153153
}),

lib/rules/no-multi-comp.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const values = require('object.values');
9+
810
const Components = require('../util/Components');
911
const docsUrl = require('../util/docsUrl');
1012
const report = require('../util/report');
@@ -64,15 +66,14 @@ module.exports = {
6466
return;
6567
}
6668

67-
const list = components.list();
68-
69-
Object.keys(list).filter((component) => !isIgnored(list[component])).forEach((component, i) => {
70-
if (i >= 1) {
69+
values(components.list())
70+
.filter((component) => !isIgnored(component))
71+
.slice(1)
72+
.forEach((component) => {
7173
report(context, messages.onlyOneComponent, 'onlyOneComponent', {
72-
node: list[component].node,
74+
node: component.node,
7375
});
74-
}
75-
});
76+
});
7677
},
7778
};
7879
}),

lib/rules/no-object-type-as-default-prop.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ function hasUsedObjectDestructuringSyntax(params) {
3737

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

44-
if (propDefaultValue.type !== 'AssignmentPattern') {
45-
return;
46-
}
47-
4844
const propDefaultValueType = propDefaultValue.right.type;
4945

5046
if (
@@ -95,14 +91,13 @@ module.exports = {
9591
create: Components.detect((context, components) => ({
9692
'Program:exit'() {
9793
const list = components.list();
98-
values(list).forEach((component) => {
99-
const node = component.node;
100-
if (!hasUsedObjectDestructuringSyntax(node.params)) {
101-
return;
102-
}
103-
const properties = node.params[0].properties;
104-
verifyDefaultPropsDestructuring(context, properties);
105-
});
94+
values(list)
95+
.filter((component) => hasUsedObjectDestructuringSyntax(component.node.params))
96+
.forEach((component) => {
97+
const node = component.node;
98+
const properties = node.params[0].properties;
99+
verifyDefaultPropsDestructuring(context, properties);
100+
});
106101
},
107102
})),
108103
};

lib/rules/no-set-state.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const values = require('object.values');
9+
810
const Components = require('../util/Components');
911
const docsUrl = require('../util/docsUrl');
1012
const report = require('../util/report');
@@ -75,10 +77,11 @@ module.exports = {
7577
},
7678

7779
'Program:exit'() {
78-
const list = components.list();
79-
Object.keys(list).filter((component) => !isValid(list[component])).forEach((component) => {
80-
reportSetStateUsages(list[component]);
81-
});
80+
values(components.list())
81+
.filter((component) => !isValid(component))
82+
.forEach((component) => {
83+
reportSetStateUsages(component);
84+
});
8285
},
8386
};
8487
}),

lib/rules/no-typos.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,9 @@ module.exports = {
247247
return;
248248
}
249249

250-
node.properties.forEach((property) => {
251-
if (property.type !== 'SpreadElement') {
252-
reportErrorIfPropertyCasingTypo(property.value, property.key, false);
253-
reportErrorIfLifecycleMethodCasingTypo(property);
254-
}
250+
node.properties.filter((property) => property.type !== 'SpreadElement').forEach((property) => {
251+
reportErrorIfPropertyCasingTypo(property.value, property.key, false);
252+
reportErrorIfLifecycleMethodCasingTypo(property);
255253
});
256254
},
257255
};

lib/rules/no-unused-class-component-methods.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ module.exports = {
245245

246246
// detect `{ foo, bar: baz } = this`
247247
if (node.init && isThisExpression(node.init) && node.id.type === 'ObjectPattern') {
248-
node.id.properties.forEach((prop) => {
249-
if (prop.type === 'Property' && isKeyLiteralLike(prop, prop.key)) {
248+
node.id.properties
249+
.filter((prop) => prop.type === 'Property' && isKeyLiteralLike(prop, prop.key))
250+
.forEach((prop) => {
250251
addUsedProperty(prop.key);
251-
}
252-
});
252+
});
253253
}
254254
},
255255
};

lib/rules/no-unused-prop-types.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const values = require('object.values');
9+
810
// As for exceptions for props.children or props.className (and alike) look at
911
// https://github.com/jsx-eslint/eslint-plugin-react/issues/7
1012

@@ -159,14 +161,12 @@ module.exports = {
159161

160162
return {
161163
'Program:exit'() {
162-
const list = components.list();
163164
// Report undeclared proptypes for all classes
164-
Object.keys(list).filter((component) => mustBeValidated(list[component])).forEach((component) => {
165-
if (!mustBeValidated(list[component])) {
166-
return;
167-
}
168-
reportUnusedPropTypes(list[component]);
169-
});
165+
values(components.list())
166+
.filter((component) => mustBeValidated(component))
167+
.forEach((component) => {
168+
reportUnusedPropTypes(component);
169+
});
170170
},
171171
};
172172
}),

lib/rules/prefer-read-only-props.js

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

66
'use strict';
77

8+
const flatMap = require('array.prototype.flatmap');
9+
const values = require('object.values');
10+
811
const Components = require('../util/Components');
912
const docsUrl = require('../util/docsUrl');
1013
const report = require('../util/report');
@@ -49,17 +52,12 @@ module.exports = {
4952

5053
create: Components.detect((context, components) => ({
5154
'Program:exit'() {
52-
const list = components.list();
53-
54-
Object.keys(list).forEach((key) => {
55-
const component = list[key];
56-
57-
if (!component.declaredPropTypes) {
58-
return;
59-
}
60-
61-
Object.keys(component.declaredPropTypes).forEach((propName) => {
62-
const prop = component.declaredPropTypes[propName];
55+
flatMap(
56+
values(components.list()),
57+
(component) => component.declaredPropTypes || []
58+
).forEach((declaredPropTypes) => {
59+
Object.keys(declaredPropTypes).forEach((propName) => {
60+
const prop = declaredPropTypes[propName];
6361

6462
if (!prop.node || !isFlowPropertyType(prop.node)) {
6563
return;

0 commit comments

Comments
 (0)