Skip to content

Commit 5678833

Browse files
committed
Filter arrays instead of if conditions
1 parent cf43b2e commit 5678833

9 files changed

+28
-54
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ module.exports = {
247247
}
248248
}
249249

250-
if ((list[component].invalidProps || []).length) {
250+
if (list[component].invalidProps && list[component].invalidProps.length > 0) {
251251
reportInvalidNaming(list[component]);
252252
}
253253
});

lib/rules/display-name.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,7 @@ module.exports = {
215215
'Program:exit': function() {
216216
const list = components.list();
217217
// Report missing display name for all components
218-
Object.keys(list).forEach(component => {
219-
if (list[component].hasDisplayName) {
220-
return;
221-
}
218+
Object.keys(list).filter(component => !list[component].hasDisplayName).forEach(component => {
222219
reportMissingDisplayName(list[component]);
223220
});
224221
}

lib/rules/no-set-state.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ module.exports = {
7373

7474
'Program:exit': function() {
7575
const list = components.list();
76-
Object.keys(list).forEach(component => {
77-
if (isValid(list[component])) {
78-
return;
79-
}
76+
Object.keys(list).filter(component => !isValid(list[component])).forEach(component => {
8077
reportSetStateUsages(list[component]);
8178
});
8279
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ module.exports = {
132132
'Program:exit': function() {
133133
const list = components.list();
134134
// Report undeclared proptypes for all classes
135-
Object.keys(list).forEach(component => {
135+
Object.keys(list).filter(component => mustBeValidated(list[component])).forEach(component => {
136136
if (!mustBeValidated(list[component])) {
137137
return;
138138
}

lib/rules/prop-types.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ module.exports = {
189189
'Program:exit': function() {
190190
const list = components.list();
191191
// Report undeclared proptypes for all classes
192-
Object.keys(list).forEach(component => {
193-
if (!mustBeValidated(list[component])) {
194-
return;
195-
}
192+
Object.keys(list).filter(component => mustBeValidated(list[component])).forEach(component => {
196193
reportUndeclaredPropTypes(list[component]);
197194
});
198195
}

lib/rules/require-default-props.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,7 @@ module.exports = {
625625
stack = null;
626626
const list = components.list();
627627

628-
Object.keys(list).forEach(component => {
629-
// If no propTypes could be found, we don't report anything.
630-
if (!list[component].propTypes) {
631-
return;
632-
}
633-
628+
Object.keys(list).filter(component => list[component].propTypes).forEach(component => {
634629
reportPropTypesWithoutDefault(
635630
list[component].propTypes,
636631
list[component].defaultProps || {}

lib/rules/require-optimization.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ module.exports = {
220220
const list = components.list();
221221

222222
// Report missing shouldComponentUpdate for all components
223-
Object.keys(list).forEach(component => {
224-
if (list[component].hasSCU) {
225-
return;
226-
}
223+
Object.keys(list).filter(component => !list[component].hasSCU).forEach(component => {
227224
reportMissingOptimization(list[component]);
228225
});
229226
}

lib/util/Components.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ class Components {
124124
const usedPropTypes = {};
125125

126126
// Find props used in components for which we are not confident
127-
Object.keys(this._list).forEach(i => {
128-
if (this._list[i].confidence >= 2) {
129-
return;
130-
}
127+
Object.keys(this._list).filter(i => this._list[i].confidence < 2).forEach(i => {
131128
let component = null;
132129
let node = null;
133130
node = this._list[i].node;
@@ -149,10 +146,7 @@ class Components {
149146
});
150147

151148
// Assign used props in not confident components to the parent component
152-
Object.keys(this._list).forEach(j => {
153-
if (this._list[j].confidence < 2) {
154-
return;
155-
}
149+
Object.keys(this._list).filter(j => this._list[j].confidence >= 2).forEach(j => {
156150
const id = getId(this._list[j].node);
157151
list[j] = this._list[j];
158152
if (usedPropTypes[id]) {
@@ -716,8 +710,10 @@ function componentRule(rule, context) {
716710
const updatedRuleInstructions = util._extend({}, ruleInstructions);
717711
const propTypesInstructions = propTypes(context, components, utils);
718712
const usedPropTypesInstructions = usedPropTypesUtil(context, components, utils);
719-
const allKeys = new Set(Object.keys(detectionInstructions).concat(Object.keys(propTypesInstructions))
720-
.concat(Object.keys(usedPropTypesInstructions)));
713+
const allKeys = new Set(Object.keys(detectionInstructions).concat(
714+
Object.keys(propTypesInstructions),
715+
Object.keys(usedPropTypesInstructions)
716+
));
721717
allKeys.forEach(instruction => {
722718
updatedRuleInstructions[instruction] = function(node) {
723719
if (instruction in detectionInstructions) {

lib/util/usedPropTypes.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ function isPropAttributeName (node) {
3535
* @returns {Boolean} True if the component must be validated, false if not.
3636
*/
3737
function mustBeValidated(component) {
38-
return Boolean(
39-
component &&
40-
!component.ignorePropsValidation
41-
);
38+
return !!(component && !component.ignorePropsValidation);
4239
}
4340

4441
module.exports = function usedPropTypesInstructions(context, components, utils) {
@@ -53,8 +50,10 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
5350
let scope = context.getScope();
5451
while (scope) {
5552
if (
56-
scope.block && scope.block.parent &&
57-
scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps'
53+
scope.block
54+
&& scope.block.parent
55+
&& scope.block.parent.key
56+
&& scope.block.parent.key.name === 'componentWillReceiveProps'
5857
) {
5958
return true;
6059
}
@@ -75,7 +74,8 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
7574

7675
if (LIFE_CYCLE_METHODS.indexOf(name) >= 0) {
7776
return true;
78-
} else if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(name) >= 0) {
77+
}
78+
if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(name) >= 0) {
7979
return true;
8080
}
8181
}
@@ -94,9 +94,11 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
9494

9595
if (node.kind === 'constructor') {
9696
return true;
97-
} else if (LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) {
97+
}
98+
if (LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) {
9899
return true;
99-
} else if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) {
100+
}
101+
if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) {
100102
return true;
101103
}
102104

@@ -371,7 +373,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
371373
switch (type) {
372374
case 'direct':
373375
// Ignore Object methods
374-
if (Object.prototype[name]) {
376+
if (name in Object.prototype) {
375377
break;
376378
}
377379

@@ -503,22 +505,15 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
503505
ObjectPattern: function(node) {
504506
// If the object pattern is a destructured props object in a lifecycle
505507
// method -- mark it for used props.
506-
if (isNodeALifeCycleMethod(node.parent.parent)) {
507-
node.properties.forEach((property, i) => {
508-
if (i === 0) {
509-
markPropTypesAsUsed(node.parent);
510-
}
511-
});
508+
if (isNodeALifeCycleMethod(node.parent.parent) && node.properties.length > 0) {
509+
markPropTypesAsUsed(node.parent);
512510
}
513511
},
514512

515513
'Program:exit': function() {
516514
const list = components.list();
517515

518-
Object.keys(list).forEach(component => {
519-
if (!mustBeValidated(list[component])) {
520-
return;
521-
}
516+
Object.keys(list).filter(component => mustBeValidated(list[component])).forEach(component => {
522517
handleCustomValidators(list[component]);
523518
});
524519
}

0 commit comments

Comments
 (0)