Skip to content

Commit b8a6c11

Browse files
committed
[Refactor] avoid loops
1 parent 72a469e commit b8a6c11

8 files changed

+73
-109
lines changed

lib/rules/prop-types.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,19 @@ module.exports = {
162162
* @param {Object} component The component to process
163163
*/
164164
function reportUndeclaredPropTypes(component) {
165-
for (let i = 0, j = component.usedPropTypes.length; i < j; i++) {
166-
const allNames = component.usedPropTypes[i].allNames;
167-
const node = component.usedPropTypes[i].node;
168-
if (
169-
isIgnored(allNames[0]) ||
170-
isDeclaredInComponent(component.node, allNames) ||
171-
!node
172-
) {
173-
continue;
174-
}
165+
const undeclareds = component.usedPropTypes.filter(propType => (
166+
propType.node &&
167+
!isIgnored(propType.allNames[0]) &&
168+
!isDeclaredInComponent(component.node, propType.allNames)
169+
));
170+
undeclareds.forEach((propType) => {
175171
context.report(
176-
node,
172+
propType.node,
177173
MISSING_MESSAGE, {
178-
name: allNames.join('.').replace(/\.__COMPUTED_PROP__/g, '[]')
174+
name: propType.allNames.join('.').replace(/\.__COMPUTED_PROP__/g, '[]')
179175
}
180176
);
181-
}
177+
});
182178
}
183179

184180
// --------------------------------------------------------------------------

lib/rules/require-optimization.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
* @param {ASTNode} node The AST node being checked.
9393
* @returns {Boolean} True if we are declaring a shouldComponentUpdate method, false if not.
9494
*/
95-
const isSCUDeclarеd = function (node) {
95+
const isSCUDeclared = function (node) {
9696
return Boolean(
9797
node &&
9898
node.name === 'shouldComponentUpdate'
@@ -196,23 +196,19 @@ module.exports = {
196196
},
197197

198198
MethodDefinition(node) {
199-
if (!isSCUDeclarеd(node.key)) {
199+
if (!isSCUDeclared(node.key)) {
200200
return;
201201
}
202202
markSCUAsDeclared(node);
203203
},
204204

205205
ObjectExpression(node) {
206206
// Search for the shouldComponentUpdate declaration
207-
for (let i = 0, l = node.properties.length; i < l; i++) {
208-
if (
209-
!node.properties[i].key || (
210-
!isSCUDeclarеd(node.properties[i].key) &&
211-
!isPureRenderDeclared(node.properties[i])
212-
)
213-
) {
214-
continue;
215-
}
207+
const found = node.properties.some(property => (
208+
property.key &&
209+
(isSCUDeclared(property.key) || isPureRenderDeclared(property))
210+
));
211+
if (found) {
216212
markSCUAsDeclared(node);
217213
}
218214
},

lib/rules/require-render-return.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,18 @@ module.exports = {
5151
ReturnStatement(node) {
5252
const ancestors = context.getAncestors(node).reverse();
5353
let depth = 0;
54-
for (let i = 0, j = ancestors.length; i < j; i++) {
55-
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
54+
ancestors.forEach((ancestor) => {
55+
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
5656
depth++;
5757
}
5858
if (
59-
!/(MethodDefinition|(Class)?Property)$/.test(ancestors[i].type) ||
60-
astUtil.getPropertyName(ancestors[i]) !== 'render' ||
61-
depth > 1
59+
/(MethodDefinition|(Class)?Property)$/.test(ancestor.type) &&
60+
astUtil.getPropertyName(ancestor) === 'render' &&
61+
depth <= 1
6262
) {
63-
continue;
63+
markReturnStatementPresent(node);
6464
}
65-
markReturnStatementPresent(node);
66-
}
65+
});
6766
},
6867

6968
ArrowFunctionExpression(node) {

lib/rules/sort-comp.js

+24-46
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
'use strict';
77

88
const has = require('has');
9-
9+
const entries = require('object.entries');
1010
const arrayIncludes = require('array-includes');
11+
1112
const Components = require('../util/Components');
1213
const astUtil = require('../util/ast');
1314
const docsUrl = require('../util/docsUrl');
@@ -298,19 +299,11 @@ module.exports = {
298299
function reportErrors() {
299300
dedupeErrors();
300301

301-
let nodeA;
302-
let nodeB;
303-
let indexA;
304-
let indexB;
305-
for (const i in errors) {
306-
if (!has(errors, i)) {
307-
continue;
308-
}
309-
310-
nodeA = errors[i].node;
311-
nodeB = errors[i].closest.ref.node;
312-
indexA = i;
313-
indexB = errors[i].closest.ref.index;
302+
entries(errors).forEach((entry) => {
303+
const nodeA = entry[1].node;
304+
const nodeB = entry[1].closest.ref.node;
305+
const indexA = entry[0];
306+
const indexB = entry[1].closest.ref.index;
314307

315308
context.report({
316309
node: nodeA,
@@ -321,7 +314,7 @@ module.exports = {
321314
position: indexA < indexB ? 'before' : 'after'
322315
}
323316
});
324-
}
317+
});
325318
}
326319

327320
/**
@@ -400,44 +393,29 @@ module.exports = {
400393
typeAnnotation: !!node.typeAnnotation && node.value === null
401394
}));
402395

403-
let i;
404-
let j;
405-
let k;
406-
let l;
407-
let propA;
408-
let propB;
409-
let order;
410-
411396
// Loop around the properties
412-
for (i = 0, j = propertiesInfos.length; i < j; i++) {
413-
propA = propertiesInfos[i];
414-
397+
propertiesInfos.forEach((propA, i) => {
415398
// Loop around the properties a second time (for comparison)
416-
for (k = 0, l = propertiesInfos.length; k < l; k++) {
399+
propertiesInfos.forEach((propB, k) => {
417400
if (i === k) {
418-
continue;
401+
return;
419402
}
420403

421-
propB = propertiesInfos[k];
422-
423404
// Compare the properties order
424-
order = comparePropsOrder(propertiesInfos, propA, propB);
425-
426-
// Continue to next comparison is order is correct
427-
if (order.correct === true) {
428-
continue;
405+
const order = comparePropsOrder(propertiesInfos, propA, propB);
406+
407+
if (!order.correct) {
408+
// Store an error if the order is incorrect
409+
storeError({
410+
node: properties[i],
411+
index: order.indexA
412+
}, {
413+
node: properties[k],
414+
index: order.indexB
415+
});
429416
}
430-
431-
// Store an error if the order is incorrect
432-
storeError({
433-
node: properties[i],
434-
index: order.indexA
435-
}, {
436-
node: properties[k],
437-
index: order.indexB
438-
});
439-
}
440-
}
417+
});
418+
});
441419
}
442420

443421
return {

lib/util/Components.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,13 @@ function componentRule(rule, context) {
583583

584584
// Try to find the component using variable references
585585
const refs = variableInScope.references;
586-
let refId;
587-
for (i = 0, j = refs.length; i < j; i++) {
588-
refId = refs[i].identifier;
586+
refs.some((ref) => {
587+
let refId = ref.identifier;
589588
if (refId.parent && refId.parent.type === 'MemberExpression') {
590589
refId = refId.parent;
591590
}
592591
if (sourceCode.getText(refId) !== componentName) {
593-
continue;
592+
return false;
594593
}
595594
if (refId.type === 'MemberExpression') {
596595
componentNode = refId.parent.right;
@@ -602,23 +601,21 @@ function componentRule(rule, context) {
602601
) {
603602
componentNode = refId.parent.init;
604603
}
605-
break;
606-
}
604+
return true;
605+
});
607606

608607
if (componentNode) {
609608
// Return the component
610609
return components.add(componentNode, 1);
611610
}
612611

613612
// Try to find the component using variable declarations
614-
let defInScope;
615613
const defs = variableInScope.defs;
616-
for (i = 0, j = defs.length; i < j; i++) {
617-
if (defs[i].type === 'ClassName' || defs[i].type === 'FunctionName' || defs[i].type === 'Variable') {
618-
defInScope = defs[i];
619-
break;
620-
}
621-
}
614+
const defInScope = defs.find(def => (
615+
def.type === 'ClassName' ||
616+
def.type === 'FunctionName' ||
617+
def.type === 'Variable'
618+
));
622619
if (!defInScope || !defInScope.node) {
623620
return null;
624621
}

lib/util/makeNoMethodSetStateRule.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
7171
}
7272
const ancestors = context.getAncestors(callee).reverse();
7373
let depth = 0;
74-
for (let i = 0, j = ancestors.length; i < j; i++) {
75-
const ancestor = ancestors[i];
74+
ancestors.some((ancestor) => {
7675
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
7776
depth++;
7877
}
@@ -81,14 +80,14 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
8180
!nameMatches(ancestor.key.name) ||
8281
(mode !== 'disallow-in-func' && depth > 1)
8382
) {
84-
continue;
83+
return false;
8584
}
8685
context.report({
8786
node: callee,
8887
message: `Do not use setState in ${ancestor.key.name}`
8988
});
90-
break;
91-
}
89+
return true;
90+
});
9291
}
9392
};
9493
}

lib/util/propTypes.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
517517
}
518518
case 'Identifier': {
519519
const variablesInScope = variableUtil.variablesInScope(context);
520-
for (let i = 0, j = variablesInScope.length; i < j; i++) {
521-
if (variablesInScope[i].name !== propTypes.name) {
522-
continue;
523-
}
524-
const defInScope = variablesInScope[i].defs[variablesInScope[i].defs.length - 1];
520+
const firstMatchingVariable = variablesInScope
521+
.find(variableInScope => variableInScope.name === propTypes.name);
522+
if (firstMatchingVariable) {
523+
const defInScope = firstMatchingVariable.defs[firstMatchingVariable.defs.length - 1];
525524
markPropTypesAsDeclared(node, defInScope.node && defInScope.node.init);
526525
return;
527526
}

lib/util/usedPropTypes.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
294294
break;
295295
}
296296
case 'VariableDeclarator':
297-
for (let i = 0, j = node.id.properties.length; i < j; i++) {
297+
node.id.properties.some((property) => {
298298
// let {props: {firstname}} = this
299299
const thisDestructuring = (
300-
node.id.properties[i].key && (
301-
(node.id.properties[i].key.name === 'props' || node.id.properties[i].key.value === 'props') &&
302-
node.id.properties[i].value.type === 'ObjectPattern'
300+
property.key && (
301+
(property.key.name === 'props' || property.key.value === 'props') &&
302+
property.value.type === 'ObjectPattern'
303303
)
304304
);
305305
// let {firstname} = props
@@ -309,15 +309,15 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
309309
);
310310

311311
if (thisDestructuring) {
312-
properties = node.id.properties[i].value.properties;
312+
properties = property.value.properties;
313313
} else if (genericDestructuring) {
314314
properties = node.id.properties;
315315
} else {
316-
continue;
316+
return false;
317317
}
318318
type = 'destructuring';
319-
break;
320-
}
319+
return true;
320+
});
321321
break;
322322
default:
323323
throw new Error(`${node.type} ASTNodes are not handled by markPropTypesAsUsed`);

0 commit comments

Comments
 (0)