Skip to content

Commit b35ea0c

Browse files
committed
Replace for..in with Array.prototype methods
1 parent 9924a16 commit b35ea0c

14 files changed

+52
-85
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const propsUtil = require('../util/props');
109
const docsUrl = require('../util/docsUrl');
@@ -248,7 +247,7 @@ module.exports = {
248247
}
249248
}
250249

251-
if (!has(list, component) || (list[component].invalidProps || []).length) {
250+
if ((list[component].invalidProps || []).length) {
252251
reportInvalidNaming(list[component]);
253252
}
254253
});

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
'use strict';
77

8-
const has = require('has');
98
const Components = require('../util/Components');
109
const variableUtil = require('../util/variable');
1110
const annotations = require('../util/annotations');
@@ -595,11 +594,7 @@ module.exports = {
595594
stack = null;
596595
const list = components.list();
597596

598-
for (const component in list) {
599-
if (!has(list, component)) {
600-
continue;
601-
}
602-
597+
Object.keys(list).forEach(component => {
603598
// If no defaultProps could be found, we don't report anything.
604599
if (!list[component].defaultProps) {
605600
return;
@@ -609,7 +604,7 @@ module.exports = {
609604
list[component].propTypes,
610605
list[component].defaultProps || {}
611606
);
612-
}
607+
});
613608
}
614609
};
615610
})

lib/rules/display-name.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const astUtil = require('../util/ast');
109
const docsUrl = require('../util/docsUrl');
@@ -216,12 +215,12 @@ module.exports = {
216215
'Program:exit': function() {
217216
const list = components.list();
218217
// Report missing display name for all components
219-
for (const component in list) {
220-
if (!has(list, component) || list[component].hasDisplayName) {
221-
continue;
218+
Object.keys(list).forEach(component => {
219+
if (list[component].hasDisplayName) {
220+
return;
222221
}
223222
reportMissingDisplayName(list[component]);
224-
}
223+
});
225224
}
226225
};
227226
})

lib/rules/no-multi-comp.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const docsUrl = require('../util/docsUrl');
109

@@ -61,15 +60,15 @@ module.exports = {
6160
const list = components.list();
6261
let i = 0;
6362

64-
for (const component in list) {
65-
if (!has(list, component) || isIgnored(list[component]) || ++i === 1) {
66-
continue;
63+
Object.keys(list).forEach(component => {
64+
if (isIgnored(list[component]) || ++i === 1) {
65+
return;
6766
}
6867
context.report({
6968
node: list[component].node,
7069
message: MULTI_COMP_MESSAGE
7170
});
72-
}
71+
});
7372
}
7473
};
7574
})

lib/rules/no-set-state.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const docsUrl = require('../util/docsUrl');
109

@@ -74,12 +73,12 @@ module.exports = {
7473

7574
'Program:exit': function() {
7675
const list = components.list();
77-
for (const component in list) {
78-
if (!has(list, component) || isValid(list[component])) {
79-
continue;
76+
Object.keys(list).forEach(component => {
77+
if (isValid(list[component])) {
78+
return;
8079
}
8180
reportSetStateUsages(list[component]);
82-
}
81+
});
8382
}
8483
};
8584
})

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

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

10-
const has = require('has');
1110
const Components = require('../util/Components');
1211
const docsUrl = require('../util/docsUrl');
1312

@@ -133,12 +132,12 @@ module.exports = {
133132
'Program:exit': function() {
134133
const list = components.list();
135134
// Report undeclared proptypes for all classes
136-
for (const component in list) {
137-
if (!has(list, component) || !mustBeValidated(list[component])) {
138-
continue;
135+
Object.keys(list).forEach(component => {
136+
if (!mustBeValidated(list[component])) {
137+
return;
139138
}
140139
reportUnusedPropTypes(list[component]);
141-
}
140+
});
142141
}
143142
};
144143
})

lib/rules/prefer-stateless-function.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
'use strict';
88

9-
const has = require('has');
109
const Components = require('../util/Components');
1110
const versionUtil = require('../util/version');
1211
const astUtil = require('../util/ast');
@@ -357,9 +356,8 @@ module.exports = {
357356

358357
'Program:exit': function() {
359358
const list = components.list();
360-
for (const component in list) {
359+
Object.keys(list).forEach(component => {
361360
if (
362-
!has(list, component) ||
363361
hasOtherProperties(list[component].node) ||
364362
list[component].useThis ||
365363
list[component].useRef ||
@@ -368,17 +366,17 @@ module.exports = {
368366
list[component].useDecorators ||
369367
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
370368
) {
371-
continue;
369+
return;
372370
}
373371

374372
if (list[component].hasSCU && list[component].usePropsOrContext) {
375-
continue;
373+
return;
376374
}
377375
context.report({
378376
node: list[component].node,
379377
message: 'Component should be written as a pure function'
380378
});
381-
}
379+
});
382380
}
383381
};
384382
})

lib/rules/prop-types.js

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

10-
const has = require('has');
1110
const Components = require('../util/Components');
1211
const docsUrl = require('../util/docsUrl');
1312

@@ -190,12 +189,12 @@ module.exports = {
190189
'Program:exit': function() {
191190
const list = components.list();
192191
// Report undeclared proptypes for all classes
193-
for (const component in list) {
194-
if (!has(list, component) || !mustBeValidated(list[component])) {
195-
continue;
192+
Object.keys(list).forEach(component => {
193+
if (!mustBeValidated(list[component])) {
194+
return;
196195
}
197196
reportUndeclaredPropTypes(list[component]);
198-
}
197+
});
199198
}
200199
};
201200
})

lib/rules/require-default-props.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const variableUtil = require('../util/variable');
109
const annotations = require('../util/annotations');
@@ -626,21 +625,17 @@ module.exports = {
626625
stack = null;
627626
const list = components.list();
628627

629-
for (const component in list) {
630-
if (!has(list, component)) {
631-
continue;
632-
}
633-
628+
Object.keys(list).forEach(component => {
634629
// If no propTypes could be found, we don't report anything.
635630
if (!list[component].propTypes) {
636-
continue;
631+
return;
637632
}
638633

639634
reportPropTypesWithoutDefault(
640635
list[component].propTypes,
641636
list[component].defaultProps || {}
642637
);
643-
}
638+
});
644639
}
645640
};
646641
})

lib/rules/require-optimization.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const docsUrl = require('../util/docsUrl');
109

@@ -221,12 +220,12 @@ module.exports = {
221220
const list = components.list();
222221

223222
// Report missing shouldComponentUpdate for all components
224-
for (const component in list) {
225-
if (!has(list, component) || list[component].hasSCU) {
226-
continue;
223+
Object.keys(list).forEach(component => {
224+
if (list[component].hasSCU) {
225+
return;
227226
}
228227
reportMissingOptimization(list[component]);
229-
}
228+
});
230229
}
231230
};
232231
})

lib/rules/require-render-return.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const astUtil = require('../util/ast');
109
const docsUrl = require('../util/docsUrl');
@@ -79,20 +78,19 @@ module.exports = {
7978

8079
'Program:exit': function() {
8180
const list = components.list();
82-
for (const component in list) {
81+
Object.keys(list).forEach(component => {
8382
if (
84-
!has(list, component) ||
8583
!hasRenderMethod(list[component].node) ||
8684
list[component].hasReturnStatement ||
8785
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
8886
) {
89-
continue;
87+
return;
9088
}
9189
context.report({
9290
node: list[component].node,
9391
message: 'Your render method should have return statement'
9492
});
95-
}
93+
});
9694
}
9795
};
9896
})

lib/rules/sort-comp.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,10 @@ module.exports = {
444444
return {
445445
'Program:exit': function() {
446446
const list = components.list();
447-
for (const component in list) {
448-
if (!has(list, component)) {
449-
continue;
450-
}
447+
Object.keys(list).forEach(component => {
451448
const properties = astUtil.getComponentProperties(list[component].node);
452449
checkPropsOrder(properties);
453-
}
450+
});
454451

455452
reportErrors();
456453
}

lib/util/Components.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const util = require('util');
98
const doctrine = require('doctrine');
109
const variableUtil = require('./variable');
@@ -125,9 +124,9 @@ class Components {
125124
const usedPropTypes = {};
126125

127126
// Find props used in components for which we are not confident
128-
for (const i in this._list) {
129-
if (!has(this._list, i) || this._list[i].confidence >= 2) {
130-
continue;
127+
Object.keys(this._list).forEach(i => {
128+
if (this._list[i].confidence >= 2) {
129+
return;
131130
}
132131
let component = null;
133132
let node = null;
@@ -147,19 +146,19 @@ class Components {
147146

148147
usedPropTypes[componentId] = mergeUsedPropTypes(usedPropTypes[componentId] || [], newUsedProps);
149148
}
150-
}
149+
});
151150

152151
// Assign used props in not confident components to the parent component
153-
for (const j in this._list) {
154-
if (!has(this._list, j) || this._list[j].confidence < 2) {
155-
continue;
152+
Object.keys(this._list).forEach(j => {
153+
if (this._list[j].confidence < 2) {
154+
return;
156155
}
157156
const id = getId(this._list[j].node);
158157
list[j] = this._list[j];
159158
if (usedPropTypes[id]) {
160159
list[j].usedPropTypes = mergeUsedPropTypes(list[j].usedPropTypes || [], usedPropTypes[id]);
161160
}
162-
}
161+
});
163162
return list;
164163
}
165164

@@ -170,14 +169,7 @@ class Components {
170169
* @returns {Number} Components list length
171170
*/
172171
length() {
173-
let length = 0;
174-
for (const i in this._list) {
175-
if (!has(this._list, i) || this._list[i].confidence < 2) {
176-
continue;
177-
}
178-
length++;
179-
}
180-
return length;
172+
return Object.keys(this._list).filter(i => this._list[i].confidence >= 2).length;
181173
}
182174
}
183175

0 commit comments

Comments
 (0)