From cf43b2e71d445b73dc4f93b542faf42fe83affcb Mon Sep 17 00:00:00 2001 From: Alex Zherdev Date: Tue, 21 Aug 2018 08:25:53 -0700 Subject: [PATCH] Replace for..in with Array.prototype methods --- lib/rules/boolean-prop-naming.js | 3 +-- lib/rules/default-props-match-prop-types.js | 9 ++----- lib/rules/display-name.js | 9 ++++--- lib/rules/no-multi-comp.js | 9 ++++--- lib/rules/no-set-state.js | 9 ++++--- lib/rules/no-unused-prop-types.js | 9 ++++--- lib/rules/prefer-stateless-function.js | 10 ++++---- lib/rules/prop-types.js | 9 ++++--- lib/rules/require-default-props.js | 11 +++------ lib/rules/require-optimization.js | 9 ++++--- lib/rules/require-render-return.js | 8 +++---- lib/rules/sort-comp.js | 7 ++---- lib/util/Components.js | 26 +++++++-------------- lib/util/usedPropTypes.js | 9 ++++--- 14 files changed, 52 insertions(+), 85 deletions(-) diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index f65b8101f4..f81fc14861 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -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'); @@ -248,7 +247,7 @@ module.exports = { } } - if (!has(list, component) || (list[component].invalidProps || []).length) { + if ((list[component].invalidProps || []).length) { reportInvalidNaming(list[component]); } }); diff --git a/lib/rules/default-props-match-prop-types.js b/lib/rules/default-props-match-prop-types.js index 79723f18a8..203bd2db2d 100644 --- a/lib/rules/default-props-match-prop-types.js +++ b/lib/rules/default-props-match-prop-types.js @@ -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'); @@ -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; @@ -609,7 +604,7 @@ module.exports = { list[component].propTypes, list[component].defaultProps || {} ); - } + }); } }; }) diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 4e40d3fcb7..9b1b25cc81 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -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'); @@ -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]); - } + }); } }; }) diff --git a/lib/rules/no-multi-comp.js b/lib/rules/no-multi-comp.js index 4d6082d767..3c0cefa3e4 100644 --- a/lib/rules/no-multi-comp.js +++ b/lib/rules/no-multi-comp.js @@ -4,7 +4,6 @@ */ 'use strict'; -const has = require('has'); const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); @@ -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 }); - } + }); } }; }) diff --git a/lib/rules/no-set-state.js b/lib/rules/no-set-state.js index 3b18b5c182..80890cdc16 100644 --- a/lib/rules/no-set-state.js +++ b/lib/rules/no-set-state.js @@ -4,7 +4,6 @@ */ 'use strict'; -const has = require('has'); const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); @@ -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]); - } + }); } }; }) diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index ed4681470a..2a218238d6 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -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'); @@ -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]); - } + }); } }; }) diff --git a/lib/rules/prefer-stateless-function.js b/lib/rules/prefer-stateless-function.js index 13f79193e5..4550cb54d1 100644 --- a/lib/rules/prefer-stateless-function.js +++ b/lib/rules/prefer-stateless-function.js @@ -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'); @@ -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 || @@ -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' }); - } + }); } }; }) diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 235502766d..b6e4a82c6a 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -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'); @@ -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]); - } + }); } }; }) diff --git a/lib/rules/require-default-props.js b/lib/rules/require-default-props.js index 0704e8e323..935564e632 100644 --- a/lib/rules/require-default-props.js +++ b/lib/rules/require-default-props.js @@ -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'); @@ -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 || {} ); - } + }); } }; }) diff --git a/lib/rules/require-optimization.js b/lib/rules/require-optimization.js index 670c7fc2ed..b9b8623f1b 100644 --- a/lib/rules/require-optimization.js +++ b/lib/rules/require-optimization.js @@ -4,7 +4,6 @@ */ 'use strict'; -const has = require('has'); const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); @@ -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]); - } + }); } }; }) diff --git a/lib/rules/require-render-return.js b/lib/rules/require-render-return.js index 139465045e..0c20334482 100644 --- a/lib/rules/require-render-return.js +++ b/lib/rules/require-render-return.js @@ -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'); @@ -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' }); - } + }); } }; }) diff --git a/lib/rules/sort-comp.js b/lib/rules/sort-comp.js index 4e2602a74e..3997021ad0 100644 --- a/lib/rules/sort-comp.js +++ b/lib/rules/sort-comp.js @@ -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(); } diff --git a/lib/util/Components.js b/lib/util/Components.js index 40f174441c..6c7b44304b 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -4,7 +4,6 @@ */ 'use strict'; -const has = require('has'); const util = require('util'); const doctrine = require('doctrine'); const variableUtil = require('./variable'); @@ -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; @@ -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; } @@ -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; } } diff --git a/lib/util/usedPropTypes.js b/lib/util/usedPropTypes.js index dde379d898..269266096b 100644 --- a/lib/util/usedPropTypes.js +++ b/lib/util/usedPropTypes.js @@ -3,7 +3,6 @@ */ 'use strict'; -const has = require('has'); const astUtil = require('./ast'); const versionUtil = require('./version'); @@ -516,12 +515,12 @@ module.exports = function usedPropTypesInstructions(context, components, utils) 'Program:exit': function() { const list = components.list(); - for (const component in list) { - if (!has(list, component) || !mustBeValidated(list[component])) { - continue; + Object.keys(list).forEach(component => { + if (!mustBeValidated(list[component])) { + return; } handleCustomValidators(list[component]); - } + }); } }; };