eslint-plugin-unicorn: Rule proposal: `no-duplicate-loops`
I’ve seen this a couple of times
Error
for (const gamma of bars.map(alpha => alpha.beta.gamma)) {
gamma('ray')
}
for (const act of actions.filter(act => typeof act === 'function')) {
act('a fool')
}
Pass
for (const alpha of bars) {
alpha.beta.gamma('ray')
}
for (const act of actions) {
if (typeof act === 'function') {
act('a fool')
}
}
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 7
- Comments: 23 (8 by maintainers)
@fisker Because you end up iterating through
bars
twice.That sounds sensible, but wouldn’t that be a completely different rule? This rule is about preventing duplicate iterations.
Hmm, I think functions with side-effects inside
map
andfilter
are incorrect anyway, so if that gives a lint error that is fine by me. Maybe the error message can be:@fregante Your last example can’t be safely converted to a single loop… (in general)
Apparently that part is called “header”, so perhaps it’s
no-array-loops-in-for-header
, just in case the feature can handle more methods in the future.Agreed