Skip to content

Commit a50f5d9

Browse files
committed
[Refactor] use toSorted, flatMap, filter, etc where appropriate
1 parent d86c665 commit a50f5d9

23 files changed

+150
-140
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 7 additions & 7 deletions
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

Lines changed: 10 additions & 8 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 8 deletions
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 8 additions & 7 deletions
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-set-state.js

Lines changed: 7 additions & 4 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 9 additions & 11 deletions
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;

lib/rules/prefer-stateless-function.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
'use strict';
99

10+
const values = require('object.values');
11+
1012
const Components = require('../util/Components');
1113
const testReactVersion = require('../util/version').testReactVersion;
1214
const astUtil = require('../util/ast');
@@ -363,29 +365,25 @@ module.exports = {
363365

364366
'Program:exit'() {
365367
const list = components.list();
366-
Object.keys(list).forEach((component) => {
367-
if (
368-
hasOtherProperties(list[component].node)
369-
|| list[component].useThis
370-
|| list[component].useRef
371-
|| list[component].invalidReturn
372-
|| list[component].hasChildContextTypes
373-
|| list[component].useDecorators
374-
|| (
375-
!componentUtil.isES5Component(list[component].node, context)
376-
&& !componentUtil.isES6Component(list[component].node, context)
368+
values(list)
369+
.filter((component) => (
370+
!hasOtherProperties(component.node)
371+
&& !component.useThis
372+
&& !component.useRef
373+
&& !component.invalidReturn
374+
&& !component.hasChildContextTypes
375+
&& !component.useDecorators
376+
&& !component.hasSCU
377+
&& (
378+
componentUtil.isES5Component(component.node, context)
379+
|| componentUtil.isES6Component(component.node, context)
377380
)
378-
) {
379-
return;
380-
}
381-
382-
if (list[component].hasSCU) {
383-
return;
384-
}
385-
report(context, messages.componentShouldBePure, 'componentShouldBePure', {
386-
node: list[component].node,
381+
))
382+
.forEach((component) => {
383+
report(context, messages.componentShouldBePure, 'componentShouldBePure', {
384+
node: component.node,
385+
});
387386
});
388-
});
389387
},
390388
};
391389
}),

lib/rules/prop-types.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// As for exceptions for props.children or props.className (and alike) look at
99
// https://github.com/jsx-eslint/eslint-plugin-react/issues/7
1010

11+
const values = require('object.values');
12+
1113
const Components = require('../util/Components');
1214
const docsUrl = require('../util/docsUrl');
1315
const report = require('../util/report');
@@ -188,9 +190,11 @@ module.exports = {
188190
'Program:exit'() {
189191
const list = components.list();
190192
// Report undeclared proptypes for all classes
191-
Object.keys(list).filter((component) => mustBeValidated(list[component])).forEach((component) => {
192-
reportUndeclaredPropTypes(list[component]);
193-
});
193+
values(list)
194+
.filter((component) => mustBeValidated(component))
195+
.forEach((component) => {
196+
reportUndeclaredPropTypes(component);
197+
});
194198
},
195199
};
196200
}),

0 commit comments

Comments
 (0)