sindresorhus/eslint-plugin-unicorn

Rule proposal: Prevent mistake in `Map` access

Fermée

#1 663 ouverte le 28 déc. 2021

 (1 commentaire) (2 réactions) (0 personne assignée)JavaScript (468 forks)user submission
help wantednew rule

Métriques du dépôt

Stars
 (5 022 étoiles)
Métriques de merge PR
 (Merge moyen 1j 16h) (399 PRs mergées en 30 j)

Description

Description

Prevent mistake when check existence with .has(), but .get() or .set() with another key, I believe this is mistake in most cases. Especially in ternary.

Fail

const foo = map.has(key) ? map.get(anotherKey) : value;
if (!map.has(key)) {
	const foo = map.get(anotherKey);
}
if (!map.has(key)) {
	map.set(anotherKey, value);
}

Pass

const foo = map.has(key) ? map.get(key) : value;
if (map.has(key)) {
	map.set(key, value);
	map.set(anotherKey, value);
}

Guide contributeur