diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index f81fc14861..d1be3364aa 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -247,7 +247,7 @@ module.exports = { } } - if ((list[component].invalidProps || []).length) { + if (list[component].invalidProps && list[component].invalidProps.length > 0) { reportInvalidNaming(list[component]); } }); diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 9b1b25cc81..c1e02be3cd 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -215,10 +215,7 @@ module.exports = { 'Program:exit': function() { const list = components.list(); // Report missing display name for all components - Object.keys(list).forEach(component => { - if (list[component].hasDisplayName) { - return; - } + Object.keys(list).filter(component => !list[component].hasDisplayName).forEach(component => { reportMissingDisplayName(list[component]); }); } diff --git a/lib/rules/no-multi-comp.js b/lib/rules/no-multi-comp.js index 3c0cefa3e4..9bb61899d1 100644 --- a/lib/rules/no-multi-comp.js +++ b/lib/rules/no-multi-comp.js @@ -58,16 +58,14 @@ module.exports = { } const list = components.list(); - let i = 0; - Object.keys(list).forEach(component => { - if (isIgnored(list[component]) || ++i === 1) { - return; + Object.keys(list).filter(component => !isIgnored(list[component])).forEach((component, i) => { + if (i >= 1) { + context.report({ + node: list[component].node, + message: MULTI_COMP_MESSAGE + }); } - 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 80890cdc16..f5dc97f699 100644 --- a/lib/rules/no-set-state.js +++ b/lib/rules/no-set-state.js @@ -73,10 +73,7 @@ module.exports = { 'Program:exit': function() { const list = components.list(); - Object.keys(list).forEach(component => { - if (isValid(list[component])) { - return; - } + Object.keys(list).filter(component => !isValid(list[component])).forEach(component => { reportSetStateUsages(list[component]); }); } diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index 2a218238d6..0e80cfd179 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -132,7 +132,7 @@ module.exports = { 'Program:exit': function() { const list = components.list(); // Report undeclared proptypes for all classes - Object.keys(list).forEach(component => { + Object.keys(list).filter(component => mustBeValidated(list[component])).forEach(component => { if (!mustBeValidated(list[component])) { return; } diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index b6e4a82c6a..16eaf6ee24 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -189,10 +189,7 @@ module.exports = { 'Program:exit': function() { const list = components.list(); // Report undeclared proptypes for all classes - Object.keys(list).forEach(component => { - if (!mustBeValidated(list[component])) { - return; - } + Object.keys(list).filter(component => mustBeValidated(list[component])).forEach(component => { reportUndeclaredPropTypes(list[component]); }); } diff --git a/lib/rules/require-default-props.js b/lib/rules/require-default-props.js index 935564e632..ea8b4b153b 100644 --- a/lib/rules/require-default-props.js +++ b/lib/rules/require-default-props.js @@ -625,12 +625,7 @@ module.exports = { stack = null; const list = components.list(); - Object.keys(list).forEach(component => { - // If no propTypes could be found, we don't report anything. - if (!list[component].propTypes) { - return; - } - + Object.keys(list).filter(component => list[component].propTypes).forEach(component => { reportPropTypesWithoutDefault( list[component].propTypes, list[component].defaultProps || {} diff --git a/lib/rules/require-optimization.js b/lib/rules/require-optimization.js index b9b8623f1b..e447bdcc5a 100644 --- a/lib/rules/require-optimization.js +++ b/lib/rules/require-optimization.js @@ -220,10 +220,7 @@ module.exports = { const list = components.list(); // Report missing shouldComponentUpdate for all components - Object.keys(list).forEach(component => { - if (list[component].hasSCU) { - return; - } + Object.keys(list).filter(component => !list[component].hasSCU).forEach(component => { reportMissingOptimization(list[component]); }); } diff --git a/lib/util/Components.js b/lib/util/Components.js index 6c7b44304b..b7e2a99a63 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -124,10 +124,7 @@ class Components { const usedPropTypes = {}; // Find props used in components for which we are not confident - Object.keys(this._list).forEach(i => { - if (this._list[i].confidence >= 2) { - return; - } + Object.keys(this._list).filter(i => this._list[i].confidence < 2).forEach(i => { let component = null; let node = null; node = this._list[i].node; @@ -149,10 +146,7 @@ class Components { }); // Assign used props in not confident components to the parent component - Object.keys(this._list).forEach(j => { - if (this._list[j].confidence < 2) { - return; - } + Object.keys(this._list).filter(j => this._list[j].confidence >= 2).forEach(j => { const id = getId(this._list[j].node); list[j] = this._list[j]; if (usedPropTypes[id]) { @@ -716,8 +710,10 @@ function componentRule(rule, context) { const updatedRuleInstructions = util._extend({}, ruleInstructions); const propTypesInstructions = propTypes(context, components, utils); const usedPropTypesInstructions = usedPropTypesUtil(context, components, utils); - const allKeys = new Set(Object.keys(detectionInstructions).concat(Object.keys(propTypesInstructions)) - .concat(Object.keys(usedPropTypesInstructions))); + const allKeys = new Set(Object.keys(detectionInstructions).concat( + Object.keys(propTypesInstructions), + Object.keys(usedPropTypesInstructions) + )); allKeys.forEach(instruction => { updatedRuleInstructions[instruction] = function(node) { if (instruction in detectionInstructions) { diff --git a/lib/util/usedPropTypes.js b/lib/util/usedPropTypes.js index 269266096b..dd3becf331 100644 --- a/lib/util/usedPropTypes.js +++ b/lib/util/usedPropTypes.js @@ -35,10 +35,7 @@ function isPropAttributeName (node) { * @returns {Boolean} True if the component must be validated, false if not. */ function mustBeValidated(component) { - return Boolean( - component && - !component.ignorePropsValidation - ); + return !!(component && !component.ignorePropsValidation); } module.exports = function usedPropTypesInstructions(context, components, utils) { @@ -53,8 +50,10 @@ module.exports = function usedPropTypesInstructions(context, components, utils) let scope = context.getScope(); while (scope) { if ( - scope.block && scope.block.parent && - scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps' + scope.block + && scope.block.parent + && scope.block.parent.key + && scope.block.parent.key.name === 'componentWillReceiveProps' ) { return true; } @@ -75,7 +74,8 @@ module.exports = function usedPropTypesInstructions(context, components, utils) if (LIFE_CYCLE_METHODS.indexOf(name) >= 0) { return true; - } else if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(name) >= 0) { + } + if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(name) >= 0) { return true; } } @@ -94,9 +94,11 @@ module.exports = function usedPropTypesInstructions(context, components, utils) if (node.kind === 'constructor') { return true; - } else if (LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) { + } + if (LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) { return true; - } else if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) { + } + if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) { return true; } @@ -371,7 +373,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils) switch (type) { case 'direct': // Ignore Object methods - if (Object.prototype[name]) { + if (name in Object.prototype) { break; } @@ -503,22 +505,15 @@ module.exports = function usedPropTypesInstructions(context, components, utils) ObjectPattern: function(node) { // If the object pattern is a destructured props object in a lifecycle // method -- mark it for used props. - if (isNodeALifeCycleMethod(node.parent.parent)) { - node.properties.forEach((property, i) => { - if (i === 0) { - markPropTypesAsUsed(node.parent); - } - }); + if (isNodeALifeCycleMethod(node.parent.parent) && node.properties.length > 0) { + markPropTypesAsUsed(node.parent); } }, 'Program:exit': function() { const list = components.list(); - Object.keys(list).forEach(component => { - if (!mustBeValidated(list[component])) { - return; - } + Object.keys(list).filter(component => mustBeValidated(list[component])).forEach(component => { handleCustomValidators(list[component]); }); }