Skip to content

Commit

Permalink
Replace for..in with Array.prototype methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Sep 8, 2018
1 parent 9924a16 commit bdd9d22
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 85 deletions.
3 changes: 1 addition & 2 deletions lib/rules/boolean-prop-naming.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const propsUtil = require('../util/props');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -248,7 +247,7 @@ module.exports = {
}
}

if (!has(list, component) || (list[component].invalidProps || []).length) {
if ((list[component].invalidProps || []).length) {
reportInvalidNaming(list[component]);
}
});
Expand Down
9 changes: 2 additions & 7 deletions lib/rules/default-props-match-prop-types.js
Expand Up @@ -5,7 +5,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const variableUtil = require('../util/variable');
const annotations = require('../util/annotations');
Expand Down Expand Up @@ -595,11 +594,7 @@ module.exports = {
stack = null;
const list = components.list();

for (const component in list) {
if (!has(list, component)) {
continue;
}

Object.keys(list).forEach(component => {
// If no defaultProps could be found, we don't report anything.
if (!list[component].defaultProps) {
return;
Expand All @@ -609,7 +604,7 @@ module.exports = {
list[component].propTypes,
list[component].defaultProps || {}
);
}
});
}
};
})
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/display-name.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -216,12 +215,12 @@ module.exports = {
'Program:exit': function() {
const list = components.list();
// Report missing display name for all components
for (const component in list) {
if (!has(list, component) || list[component].hasDisplayName) {
continue;
Object.keys(list).forEach(component => {
if (list[component].hasDisplayName) {
return;
}
reportMissingDisplayName(list[component]);
}
});
}
};
})
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/no-multi-comp.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -61,15 +60,15 @@ module.exports = {
const list = components.list();
let i = 0;

for (const component in list) {
if (!has(list, component) || isIgnored(list[component]) || ++i === 1) {
continue;
Object.keys(list).forEach(component => {
if (isIgnored(list[component]) || ++i === 1) {
return;
}
context.report({
node: list[component].node,
message: MULTI_COMP_MESSAGE
});
}
});
}
};
})
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/no-set-state.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -74,12 +73,12 @@ module.exports = {

'Program:exit': function() {
const list = components.list();
for (const component in list) {
if (!has(list, component) || isValid(list[component])) {
continue;
Object.keys(list).forEach(component => {
if (isValid(list[component])) {
return;
}
reportSetStateUsages(list[component]);
}
});
}
};
})
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/no-unused-prop-types.js
Expand Up @@ -7,7 +7,6 @@
// As for exceptions for props.children or props.className (and alike) look at
// https://github.com/yannickcr/eslint-plugin-react/issues/7

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -133,12 +132,12 @@ module.exports = {
'Program:exit': function() {
const list = components.list();
// Report undeclared proptypes for all classes
for (const component in list) {
if (!has(list, component) || !mustBeValidated(list[component])) {
continue;
Object.keys(list).forEach(component => {
if (!mustBeValidated(list[component])) {
return;
}
reportUnusedPropTypes(list[component]);
}
});
}
};
})
Expand Down
10 changes: 4 additions & 6 deletions lib/rules/prefer-stateless-function.js
Expand Up @@ -6,7 +6,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const versionUtil = require('../util/version');
const astUtil = require('../util/ast');
Expand Down Expand Up @@ -357,9 +356,8 @@ module.exports = {

'Program:exit': function() {
const list = components.list();
for (const component in list) {
Object.keys(list).forEach(component => {
if (
!has(list, component) ||
hasOtherProperties(list[component].node) ||
list[component].useThis ||
list[component].useRef ||
Expand All @@ -368,17 +366,17 @@ module.exports = {
list[component].useDecorators ||
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
) {
continue;
return;
}

if (list[component].hasSCU && list[component].usePropsOrContext) {
continue;
return;
}
context.report({
node: list[component].node,
message: 'Component should be written as a pure function'
});
}
});
}
};
})
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/prop-types.js
Expand Up @@ -7,7 +7,6 @@
// As for exceptions for props.children or props.className (and alike) look at
// https://github.com/yannickcr/eslint-plugin-react/issues/7

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -190,12 +189,12 @@ module.exports = {
'Program:exit': function() {
const list = components.list();
// Report undeclared proptypes for all classes
for (const component in list) {
if (!has(list, component) || !mustBeValidated(list[component])) {
continue;
Object.keys(list).forEach(component => {
if (!mustBeValidated(list[component])) {
return;
}
reportUndeclaredPropTypes(list[component]);
}
});
}
};
})
Expand Down
11 changes: 3 additions & 8 deletions lib/rules/require-default-props.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const variableUtil = require('../util/variable');
const annotations = require('../util/annotations');
Expand Down Expand Up @@ -626,21 +625,17 @@ module.exports = {
stack = null;
const list = components.list();

for (const component in list) {
if (!has(list, component)) {
continue;
}

Object.keys(list).forEach(component => {
// If no propTypes could be found, we don't report anything.
if (!list[component].propTypes) {
continue;
return;
}

reportPropTypesWithoutDefault(
list[component].propTypes,
list[component].defaultProps || {}
);
}
});
}
};
})
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/require-optimization.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -221,12 +220,12 @@ module.exports = {
const list = components.list();

// Report missing shouldComponentUpdate for all components
for (const component in list) {
if (!has(list, component) || list[component].hasSCU) {
continue;
Object.keys(list).forEach(component => {
if (list[component].hasSCU) {
return;
}
reportMissingOptimization(list[component]);
}
});
}
};
})
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/require-render-return.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -79,20 +78,19 @@ module.exports = {

'Program:exit': function() {
const list = components.list();
for (const component in list) {
Object.keys(list).forEach(component => {
if (
!has(list, component) ||
!hasRenderMethod(list[component].node) ||
list[component].hasReturnStatement ||
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
) {
continue;
return;
}
context.report({
node: list[component].node,
message: 'Your render method should have return statement'
});
}
});
}
};
})
Expand Down
7 changes: 2 additions & 5 deletions lib/rules/sort-comp.js
Expand Up @@ -444,13 +444,10 @@ module.exports = {
return {
'Program:exit': function() {
const list = components.list();
for (const component in list) {
if (!has(list, component)) {
continue;
}
Object.keys(list).forEach(component => {
const properties = astUtil.getComponentProperties(list[component].node);
checkPropsOrder(properties);
}
});

reportErrors();
}
Expand Down
26 changes: 9 additions & 17 deletions lib/util/Components.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const util = require('util');
const doctrine = require('doctrine');
const variableUtil = require('./variable');
Expand Down Expand Up @@ -125,9 +124,9 @@ class Components {
const usedPropTypes = {};

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

usedPropTypes[componentId] = mergeUsedPropTypes(usedPropTypes[componentId] || [], newUsedProps);
}
}
});

// Assign used props in not confident components to the parent component
for (const j in this._list) {
if (!has(this._list, j) || this._list[j].confidence < 2) {
continue;
Object.keys(this._list).forEach(j => {
if (this._list[j].confidence < 2) {
return;
}
const id = getId(this._list[j].node);
list[j] = this._list[j];
if (usedPropTypes[id]) {
list[j].usedPropTypes = mergeUsedPropTypes(list[j].usedPropTypes || [], usedPropTypes[id]);
}
}
});
return list;
}

Expand All @@ -170,14 +169,7 @@ class Components {
* @returns {Number} Components list length
*/
length() {
let length = 0;
for (const i in this._list) {
if (!has(this._list, i) || this._list[i].confidence < 2) {
continue;
}
length++;
}
return length;
return Object.keys(this._list).filter(i => this._list[i].confidence >= 2).length;
}
}

Expand Down

0 comments on commit bdd9d22

Please sign in to comment.