sindresorhus/eslint-plugin-unicorn

Rule proposal: Prevent mistake in `Map` access

Chiusa

#1663 aperta il 28 dic 2021

 (1 commento) (2 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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

Guida contributor