跳转到内容

noDelete

Diagnostic Category: lint/performance/noDelete

Since: v1.0.0

Disallow the use of the delete operator.

The delete operator enables the removal of a property from an object.

The delete operator should be avoided because it can prevent some optimizations of JavaScript engines. Moreover, it can lead to unexpected results. For instance, deleting an array element does not change the length of the array.

The only legitimate use of delete is on an object that behaves like a map. To allow this pattern, this rule does not report delete on computed properties that are not literal values. Consider using Map instead of an object.

const arr = [1, 2, 3];
delete arr[0];
code-block.js:2:1 lint/performance/noDelete  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid the delete operator which can impact performance.

1 │ const arr = [1, 2, 3];
> 2 │ delete arr[0];
^^^^^^^^^^^^^
3 │

Unsafe fix: Use an undefined assignment instead.

1 1 const arr = [1, 2, 3];
2 - delete·arr[0];
2+ arr[0]·=·undefined;
3 3

const obj = {a: {b: {c: 123}}};
delete obj.a.b.c;
code-block.js:2:1 lint/performance/noDelete  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid the delete operator which can impact performance.

1 │ const obj = {a: {b: {c: 123}}};
> 2 │ delete obj.a.b.c;
^^^^^^^^^^^^^^^^
3 │

Unsafe fix: Use an undefined assignment instead.

1 1 const obj = {a: {b: {c: 123}}};
2 - delete·obj.a.b.c;
2+ obj.a.b.c·=·undefined;
3 3

const foo = new Set([1,2,3]);
foo.delete(1);
const map = Object.create(null);
const key = "key"
map[key] = "value"
delete map[key];
let x = 5;
delete f(); // uncovered by this rule.