Add eslint rule to prevent Map.has call immediately before Map.delete

This commit is contained in:
Dmitriy Vasyura
2025-11-21 01:12:58 -08:00
parent 76fb5a42b1
commit fbef8a93e9
25 changed files with 208 additions and 73 deletions

View File

@@ -0,0 +1,38 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export function testRedundantHasBeforeDelete() {
const m = new Map<string, number>();
const s = new Set<string>();
// Invalid cases
m.delete('key');
s.delete('key');
// Cases with else clause
if (!m.delete('key')) {
console.log('not found');
}
if (m.delete('key')) {
console.log('deleted');
} else {
console.log('not found');
}
// Valid cases
m.delete('key');
s.delete('key');
if (m.has('key')) {
console.log('deleting');
m.delete('key');
}
if (m.has('key')) {
m.delete('otherKey');
}
}