sindresorhus/eslint-plugin-unicorn

Rule proposal: Prevent mistake in `Map` access

クローズ

#1,663 opened on 2021/12/28

 (1 件のコメント) (2 件のリアクション) (0 人の担当者)JavaScript (468 件のフォーク)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 個のスター)
PR merge metrics
 (平均マージ 1d 16h) (30d で 399 merged PRs)

説明

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

コントリビューターガイド