Skip to content

Latest commit

Β 

History

History
30 lines (20 loc) Β· 1.48 KB

File metadata and controls

30 lines (20 loc) Β· 1.48 KB

prefer-get-or-insert-computed

πŸ“ Prefer .getOrInsertComputed() when the default value has side effects.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

Map#getOrInsert() and WeakMap#getOrInsert() always evaluate the default value, even when the key already exists. Use Map#getOrInsertComputed() or WeakMap#getOrInsertComputed() when creating the default value has side effects.

Examples

// ❌
map.getOrInsert(key, call());

// βœ…
map.getOrInsertComputed(key, () => call());
// ❌
map.getOrInsert(key, call(key));

// βœ…
map.getOrInsertComputed(key, call);