π Disallow mutating a loop iterable during iteration.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
Mutating an iterable while a for...of loop consumes it can skip, repeat, or unexpectedly visit items.
This rule reports mutating Array, Set, and Map methods on the active iterable, including .keys(), .values(), and .entries() loops.
Same-current-entry Set and Map updates are allowed. Delete-then-reinsert patterns are not.
It intentionally ignores iterator aliases, non-method writes like array.length = 0, and snapshot loops from Object.keys(), Object.values(), and Object.entries().
// β
for (const item of items) {
items.push(item.clone());
}
// β
for (const item of [...items]) {
items.push(item.clone());
}
// β
for (const value of values) {
values.shift();
}
// β
for (const key of Object.keys(object)) {
delete object[key];
}
// β
for (const value of set) {
set.delete(value);
set.add(value);
}
// β
for (const value of set) {
set.add(value);
}
// β
for (const key of map.keys()) {
map.set(key, newValue);
}
// β
for (const [key] of map) {
map.delete(key);
}