Skip to content

Commit

Permalink
Filter arrays instead of if conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Sep 20, 2018
1 parent cf43b2e commit fd03bd7
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 62 deletions.
2 changes: 1 addition & 1 deletion lib/rules/boolean-prop-naming.js
Expand Up @@ -247,7 +247,7 @@ module.exports = {
}
}

if ((list[component].invalidProps || []).length) {
if (list[component].invalidProps && list[component].invalidProps.length > 0) {
reportInvalidNaming(list[component]);
}
});
Expand Down
5 changes: 1 addition & 4 deletions lib/rules/display-name.js
Expand Up @@ -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]);
});
}
Expand Down
14 changes: 6 additions & 8 deletions lib/rules/no-multi-comp.js
Expand Up @@ -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
});
});
}
};
Expand Down
5 changes: 1 addition & 4 deletions lib/rules/no-set-state.js
Expand Up @@ -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]);
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unused-prop-types.js
Expand Up @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions lib/rules/prop-types.js
Expand Up @@ -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]);
});
}
Expand Down
7 changes: 1 addition & 6 deletions lib/rules/require-default-props.js
Expand Up @@ -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 || {}
Expand Down
5 changes: 1 addition & 4 deletions lib/rules/require-optimization.js
Expand Up @@ -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]);
});
}
Expand Down
16 changes: 6 additions & 10 deletions lib/util/Components.js
Expand Up @@ -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;
Expand All @@ -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]) {
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 15 additions & 20 deletions lib/util/usedPropTypes.js
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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]);
});
}
Expand Down

0 comments on commit fd03bd7

Please sign in to comment.