sindresorhus/eslint-plugin-unicorn

Rule proposal: Prevent mistake in `Map` access

Fechada

#1.663 aberto em 28 de dez. de 2021

 (1 comentário) (2 reações) (0 responsável)JavaScript (468 forks)user submission
help wantednew rule

Métricas do repositório

Stars
 (5.022 estrelas)
Métricas de merge de PR
 (Mesclagem média 1d 16h) (399 fundiu PRs em 30d)

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

Guia do colaborador