Skip to content

Latest commit

Β 

History

History
61 lines (45 loc) Β· 1.5 KB

File metadata and controls

61 lines (45 loc) Β· 1.5 KB

no-loop-iterable-mutation

πŸ“ 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().

Examples

// ❌
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);
}