Skip to content

Latest commit

Β 

History

History
67 lines (47 loc) Β· 1.81 KB

no-useless-delete-check.md

File metadata and controls

67 lines (47 loc) Β· 1.81 KB

no-useless-delete-check

πŸ“ Disallow unnecessary existence checks before deletion.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§πŸ’‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Deletion is already safe when the property, key, or value does not exist, so an existence guard is usually unnecessary.

This rule reports simple in checks before deleting from known objects, and .has() checks before deleting from local const Map, Set, WeakMap, and WeakSet instances.

The rule is conservative. It ignores else branches, compound conditions, unknown receivers, Object.hasOwn(), hasOwnProperty(), custom collections, type-only collection annotations, type-only TypeScript intersection types, and cases where removing the guard may be observable.

Collection cases are automatically fixed when doing so preserves comments. Object deletion is provided as a suggestion because Proxy traps can make unconditional delete observable.

Examples

// ❌
const object = {};

if (key in object) {
	delete object[key];
}

// βœ…
delete object[key];
// ❌
const map = new Map();

if (map.has(key)) {
	map.delete(key);
}

// βœ…
map.delete(key);
// ❌
const set = new Set();

if (set.has(value)) {
	set.delete(value);
}

// βœ…
set.delete(value);
// βœ…
const map = new Map();

if (map.has(key)) {
	map.delete(key);
} else {
	handleMissingKey();
}