sindresorhus/eslint-plugin-unicorn
Rule proposal: `no-complex-iteratee-expression`
Closed
#1,044 opened on Jan 20, 2021
help wantednew rule
Repository metrics
- Stars
- (5,022 stars)
- PR merge metrics
- (Avg merge 1d 16h) (399 merged PRs in 30d)
Description
How about forcing the looped expression to be one of:
- a variable (
x)- a property access (
x.a.b)- a single function call with no arguments (
x(),x.a.b())- one of built-in function calls commonly used for iteration (
Object.keys(x)/Object.values(x)/Object.entries(x))Otherwise force the user to define a variable before the loop (may be possible to auto-fix, the iteratee variable name could be the pluralized name of the element variable).
Originally from https://github.com/sindresorhus/eslint-plugin-unicorn/issues/846#issuecomment-703317406
Fail
for (const x of xs.filter(x => shouldKeep(x))) {
console.log(x)
}
Pass
const filteredXs = xs.filter(x => shouldKeep(x))
for (const x of filteredXs) {
console.log(x)
}
for (const x of Object.values(xs)) {
console.log(x)
}