unicorn/no-useless-collection-argument
Disallow empty arguments and fallbacks in new Set(), Map, WeakSet, and WeakMap
has suggestions
new Set(), new Map(), new WeakSet() and new WeakMap() make an empty collection when they get no argument, undefined or null. So new Set([]) passes an array for nothing, and the fallback in new Set(items ?? []) never changes the result.
The rule reports [], "", null and undefined, either as the only argument or on the right of ??.
--fix-suggestions removes the argument or the fallback.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-useless-collection-argument" = "error" # or "warn"Examples#
example.js3 problems
const seen = new Set([]The empty array is useless);
const cache = new Map(undefinedThe undefined is useless);
function unique(items) {
return new Set(items ?? []The empty array is useless);
}example.jsNo problems
const seen = new Set();
const cache = new Map();
function unique(items) {
return new Set(items);
}
const defaults = new Set(items ?? ["all"]);