noDynamicDelete (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noDynamicDelete - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noDynamicDelete": "error" } } }}Description
Section titled “Description”Disallow using the delete operator with a property name that is computed at runtime.
The delete operator removes a property from an object. When the property name
is written inside square brackets and comes from a variable, a function call, or
another expression, the code is using a plain object to store a changing set of keys.
It is hard to tell which property gets removed just by reading the code.
A Map
or a Set
is built for this use. Both have a delete() method for removing entries.
This rule allows square brackets that contain a string literal, a number literal, or a negative number literal, because the property name is fixed in the code.
In Node.js, process.env holds the environment variables of the running program,
and it can’t be replaced with a Map. The rule still reports delete process.env[name],
and suggests deleting each environment variable by its name instead.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const name = "name";delete container[name];code-block.js:2:18 lint/nursery/noDynamicDelete ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid using delete with a property name that is computed at runtime.
1 │ const name = "name";
> 2 │ delete container[name];
│ ^^^^
3 │
ℹ Removing properties by a changing name makes it hard to tell which property gets removed.
ℹ Use a Map or a Set and call its delete() method instead.
ℹ 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.
delete container[name.toUpperCase()];code-block.js:1:18 lint/nursery/noDynamicDelete ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid using delete with a property name that is computed at runtime.
> 1 │ delete container[name.toUpperCase()];
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Removing properties by a changing name makes it hard to tell which property gets removed.
ℹ Use a Map or a Set and call its delete() method instead.
ℹ 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 variable = "API_KEY";delete process.env[variable];code-block.js:2:20 lint/nursery/noDynamicDelete ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid using delete with an environment variable name that is computed at runtime.
1 │ const variable = "API_KEY";
> 2 │ delete process.env[variable];
│ ^^^^^^^^
3 │
ℹ Removing environment variables by a changing name makes it hard to tell which variables get removed.
ℹ Delete each environment variable by its name instead, for example delete process.env.API_KEY.
ℹ 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.
delete container.aaa;delete container[7];delete container[-1];delete container["aaa"];delete container["Infinity"];
const map = new Map();map.delete(name);
delete process.env.API_KEY;See Also
Section titled “See Also”- If you want to disallow the
deleteoperator on fixed property names, seenoDelete.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.