noMismatchedMapKey (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noMismatchedMapKey - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
unicorn/no-mismatched-map-key
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noMismatchedMapKey": "error" } } }}Description
Section titled “Description”Disallow checking a map for one key and then using a different key.
After map.has(key) checks whether a key exists, the code that runs because
of that check usually reads or writes the same key with map.get(key) or
map.set(key, value). Using a different key there is almost always a copy-paste
mistake.
This rule checks the branches of if statements, while loops, ternaries
(? :), and the right side of && and || that run because of a has() check.
It only reports simple cases where the keys are clearly different: two different
literal values, or two different variables or properties.
A branch that is a block with more than one statement isn’t checked.
A branch that assigns a new value to the map isn’t checked either, because the
get() or set() call may use a different map.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const value = map.has(key) ? map.get(otherKey) : fallback;code-block.js:1:38 lint/nursery/noMismatchedMapKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This get() call uses a different key than the has() check before it.
> 1 │ const value = map.has(key) ? map.get(otherKey) : fallback;
│ ^^^^^^^^
2 │
ℹ The map is checked for this key.
> 1 │ const value = map.has(key) ? map.get(otherKey) : fallback;
│ ^^^
2 │
ℹ Checking one key and then using another is usually a copy-paste mistake, so this call may use the wrong entry.
ℹ Use the same key in both calls.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
if (!map.has(key)) { map.set(otherKey, value);}code-block.js:2:13 lint/nursery/noMismatchedMapKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This set() call uses a different key than the has() check before it.
1 │ if (!map.has(key)) {
> 2 │ map.set(otherKey, value);
│ ^^^^^^^^
3 │ }
4 │
ℹ The map is checked for this key.
> 1 │ if (!map.has(key)) {
│ ^^^
2 │ map.set(otherKey, value);
3 │ }
ℹ Checking one key and then using another is usually a copy-paste mistake, so this call may use the wrong entry.
ℹ Use the same key in both calls.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function getUser(cache) { if (cache.has("user")) { return cache.get("users"); }}code-block.js:3:26 lint/nursery/noMismatchedMapKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This get() call uses a different key than the has() check before it.
1 │ function getUser(cache) {
2 │ if (cache.has("user")) {
> 3 │ return cache.get("users");
│ ^^^^^^^
4 │ }
5 │ }
ℹ The map is checked for this key.
1 │ function getUser(cache) {
> 2 │ if (cache.has("user")) {
│ ^^^^^^
3 │ return cache.get("users");
4 │ }
ℹ Checking one key and then using another is usually a copy-paste mistake, so this call may use the wrong entry.
ℹ Use the same key in both calls.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const value = map.has(key) ? map.get(key) : fallback;
if (!map.has(key)) { map.set(key, value);}
if (map.has(key)) { otherMap.get(otherKey);}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.