Skip to content

noCollectionBracketAccess (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noCollectionBracketAccess": "error"
}
}
}
}

Disallow using square brackets to read, write, or delete the entries of a Map, Set, WeakMap, or WeakSet.

A Map, Set, WeakMap, or WeakSet doesn’t store its entries as properties. Square brackets, as in map["key"], read or change a property of the collection object, so they don’t find, add, or remove any entry. This is almost always a mistake.

Use the methods of the collection instead:

  • to read: get() and has() for a Map, has() for a Set;
  • to write: set() for a Map, add() for a Set;
  • to delete: delete().

The rule only reports values that it knows are one of these collections: values created with new Map(), new Set(), new WeakMap(), or new WeakSet(), const variables that hold such a value, and variables or parameters whose type is one of these collections.

Square brackets that access a real property or method of the collection, such as map["size"] or map[Symbol.iterator], aren’t reported.

The fix replaces map[key] = value; with map.set(key, value);, and delete map[key]; with map.delete(key);. It’s unsafe, because the fixed code changes the entries of the collection instead of its properties.

const map = new Map();
map["foo"] = "bar";
code-block.js:2:1 lint/nursery/noCollectionBracketAccess  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This changes a property of the Map, not one of its entries.

1 │ const map = new Map();
> 2 │ map["foo"] = "bar";
│ ^^^^^^^^^^
3 │

ℹ A Map doesn't store its entries as properties, so square brackets don't find or change them.

ℹ Use set() to add or change an entry.

ℹ 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.

ℹ Unsafe fix: Use set() instead.

1 1 │ const map = new Map();
2 │ - map["foo"]·=·"bar";
2 │ + map.set("foo",·"bar");
3 3 │

const map = new Map();
const value = map["foo"];
code-block.js:2:15 lint/nursery/noCollectionBracketAccess ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This reads a property of the Map, not one of its entries.

1 │ const map = new Map();
> 2 │ const value = map["foo"];
│ ^^^^^^^^^^
3 │

ℹ A Map doesn't store its entries as properties, so square brackets don't find or change them.

ℹ Use get() to read an entry, or has() to check whether it exists.

ℹ 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 map = new Map();
delete map["foo"];
code-block.js:2:8 lint/nursery/noCollectionBracketAccess  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This deletes a property of the Map, not one of its entries.

1 │ const map = new Map();
> 2 │ delete map["foo"];
│ ^^^^^^^^^^
3 │

ℹ A Map doesn't store its entries as properties, so square brackets don't find or change them.

ℹ Use delete() to remove an entry.

ℹ 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.

ℹ Unsafe fix: Use delete() instead.

1 1 │ const map = new Map();
2 │ - delete·map["foo"];
2 │ + map.delete("foo");
3 3 │

const set = new Set();
if (set["foo"]) {}
code-block.js:2:5 lint/nursery/noCollectionBracketAccess ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This reads a property of the Set, not one of its entries.

1 │ const set = new Set();
> 2 │ if (set["foo"]) {}
│ ^^^^^^^^^^
3 │

ℹ A Set doesn't store its entries as properties, so square brackets don't find or change them.

ℹ Use has() to check whether it contains a value.

ℹ 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 map = new Map();
map.set("foo", "bar");
const value = map.get("foo");
map.delete("foo");
const count = map["size"];
const set = new Set();
if (set.has("foo")) {}