sindresorhus/eslint-plugin-unicorn
`no-array-for-each` bad autofix with `Map`
Fechada
#1.452 aberto em 27 de jul. de 2021
bughelp wantedtypes
Métricas do repositório
- Stars
- (5.022 estrelas)
- Métricas de merge de PR
- (Mesclagem média 4h 30m) (26 fundiu PRs em 30d)
Description
When the no-array-for-each rule is enabled (as recommended) it reports errors when forEach is used with other types that aren’t arrays, like Map or Set. (Or any custom object with a forEach method potentially.)
const map = new Map();
map.forEach((i) => console.log(i));
// ^^^^^^^
// Error: Do not use `Array#forEach(…)`
const set = new Set();
set.forEach((i) => console.log(i));
// ^^^^^^^
// Error: Do not use `Array#forEach(…)`
This is most important for Map, where forEach passes the mapped value as the first argument to its callback, but the current auto-fix (for...of) will pass [key, value]:
const map = new Map([['key', 'value']]);
// Before auto-fix
map.forEach((value) => console.log(value)); // 'value'
// After auto-fix
for (const value of map) {
console.log(value); // ['key', 'value']
}