π Prefer a single Array#some() or Array#every() with a combined predicate.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π‘ This rule is manually fixable by editor suggestions.
Combining multiple predicates into a single Array#some() or Array#every() call is more efficient and readable. Calling the same method multiple times iterates the array multiple times unnecessarily.
This rule only provides suggestions (not autofix) because combining calls can change evaluation order when receivers or predicates have side effects.
// β - Iterates array twice
const hasErrors = messages.some(m => m.type === 'error') || messages.some(m => m.severity === 'critical');
// β
- Single iteration with combined predicate
const hasErrors = messages.some(m => m.type === 'error' || m.severity === 'critical');// β - Iterates array twice
const allValid = items.every(item => item.valid) && items.every(item => item.complete);
// β
- Single iteration
const allValid = items.every(item => item.valid && item.complete);// β - Multiple same-method calls
const hasAnyValue = values.some(v => v !== null) || values.some(v => v !== undefined);
// β
- Combine into one check
const hasAnyValue = values.some(v => v !== null && v !== undefined);// β
- Different predicates or different methods don't trigger this rule
array.some(x => x > 5) && array.every(x => x < 100); // OK - different methods
array.some(x => x > 5) || someOtherCondition; // OK - different receiver