sindresorhus/eslint-plugin-unicorn

Rule proposal: Prevent mistake in `Map` access

已关闭

#1,663 创建于 2021年12月28日

 (1 条评论) (2 个反应) (0 位负责人)JavaScript (468 个派生)user submission
help wantednew rule

仓库指标

星标
 (5,022 个星标)
PR 合并指标
 (平均合并 1天 16小时) (30 天内合并 399 个 PR)

描述

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

贡献者指南