useSetMethods (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useSetMethods - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/prefer-set-methods
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useSetMethods": "error" } } }}Description
Section titled “Description”Prefer the methods of Set for combining and comparing Sets.
A Set has methods that combine two Sets, such as union(), intersection(), and difference(), and methods that compare two Sets, such as isSubsetOf() and isDisjointFrom().
These methods say what the code does in a single call.
Copying the values of a Set into an array, and checking each value with has(), is longer and harder to read, and creates an array that is thrown away right after.
The rule reports the following code, where a and b are Sets:
| Code | Replacement |
|---|---|
new Set([...a, ...b]) |
a.union(b) |
[...a].filter(value => b.has(value)) |
a.intersection(b) |
[...a].filter(value => !b.has(value)) |
a.difference(b) |
[...a].every(value => b.has(value)) |
a.isSubsetOf(b) |
[...a].every(value => !b.has(value)) |
a.isDisjointFrom(b) |
[...a].some(value => b.has(value)) |
!a.isDisjointFrom(b) |
[...a].some(value => !b.has(value)) |
!a.isSubsetOf(b) |
a.intersection(b).size === 0 |
a.isDisjointFrom(b) |
a.difference(b).size === 0 |
a.isSubsetOf(b) |
The rule also reports a filter() call wrapped in new Set(), such as new Set([...a].filter(value => b.has(value))).
It reports the size checks written with !==, with the 0 on the left, and as size > 0 or 0 < size; these are replaced with the negated method, such as !a.isDisjointFrom(b).
The rule only reports code when it can tell that the values are Sets. This is the case when a value is:
- created with
new Set(); - a variable or a function parameter with the type
SetorReadonlySet; - a
constvariable that is initialized with aSet; - marked as a
Setor aReadonlySetwithasorsatisfies; - a conditional expression (
condition ? a : b) whose two results are both aSet.
The function passed to filter(), every(), or some() must be an arrow function with a single parameter, that only returns b.has(value) or !b.has(value).
The Set that has() is called on can’t be a function call or another operation that can change a value, because the function evaluates it once per value, while the replacement evaluates it only once.
For the same reason, the Sets spread into new Set([...a, ...b]) can’t contain such operations.
A filter() call that isn’t wrapped in new Set() is ignored when its result is used right away, such as in [...a].filter(value => b.has(value)).map(format) or format([...a].filter(value => b.has(value))), because the replacement returns a Set instead of an array.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const a = new Set([1, 2]);const b = new Set([2, 3]);const all = new Set([...a, ...b]);code-block.js:3:13 lint/nursery/useSetMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This code spreads Sets into an array to combine them into a new Set.
1 │ const a = new Set([1, 2]);
2 │ const b = new Set([2, 3]);
> 3 │ const all = new Set([...a, ...b]);
│ ^^^^^^^^^^^^^^^^^^^^^
4 │
ℹ Use the union() method instead. It does the same work in a single call, without creating a temporary array or Set.
ℹ 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.
ℹ Unsafe fix: Use union() instead.
1 1 │ const a = new Set([1, 2]);
2 2 │ const b = new Set([2, 3]);
3 │ - const·all·=·new·Set([...a,·...b]);
3 │ + const·all·=·a.union(b);
4 4 │
const a = new Set([1, 2]);const b = new Set([2, 3]);const shared = [...a].filter((value) => b.has(value));code-block.js:3:16 lint/nursery/useSetMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This code filters the values of a Set to keep the ones that another Set has.
1 │ const a = new Set([1, 2]);
2 │ const b = new Set([2, 3]);
> 3 │ const shared = [...a].filter((value) => b.has(value));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │
ℹ Use the intersection() method instead. It does the same work in a single call, without creating a temporary array or Set.
ℹ Unlike filter(), this method returns a Set instead of an array.
ℹ 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.
ℹ Unsafe fix: Use intersection() instead.
1 1 │ const a = new Set([1, 2]);
2 2 │ const b = new Set([2, 3]);
3 │ - const·shared·=·[...a].filter((value)·=>·b.has(value));
3 │ + const·shared·=·a.intersection(b);
4 4 │
function onlyInFirst(a: Set<number>, b: Set<number>) { return new Set([...a].filter((value) => !b.has(value)));}code-block.ts:2:12 lint/nursery/useSetMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This code filters the values of a Set to remove the ones that another Set has.
1 │ function onlyInFirst(a: Set<number>, b: Set<number>) {
> 2 │ return new Set([...a].filter((value) => !b.has(value)));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Use the difference() method instead. It does the same work in a single call, without creating a temporary array or Set.
ℹ 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.
ℹ Unsafe fix: Use difference() instead.
1 1 │ function onlyInFirst(a: Set<number>, b: Set<number>) {
2 │ - ····return·new·Set([...a].filter((value)·=>·!b.has(value)));
2 │ + ····return·a.difference(b);
3 3 │ }
4 4 │
function hasAll(a: Set<string>, b: ReadonlySet<string>) { return [...a].every((value) => b.has(value));}code-block.ts:2:12 lint/nursery/useSetMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This code checks whether every value of a Set is also in another Set.
1 │ function hasAll(a: Set<string>, b: ReadonlySet<string>) {
> 2 │ return [...a].every((value) => b.has(value));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Use the isSubsetOf() method instead. It does the same work in a single call, without creating a temporary array or Set.
ℹ 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.
ℹ Unsafe fix: Use isSubsetOf() instead.
1 1 │ function hasAll(a: Set<string>, b: ReadonlySet<string>) {
2 │ - ····return·[...a].every((value)·=>·b.has(value));
2 │ + ····return·a.isSubsetOf(b);
3 3 │ }
4 4 │
function hasNothingInCommon(a: Set<string>, b: Set<string>) { return a.intersection(b).size === 0;}code-block.ts:2:12 lint/nursery/useSetMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This code checks whether two Sets have no values in common.
1 │ function hasNothingInCommon(a: Set<string>, b: Set<string>) {
> 2 │ return a.intersection(b).size === 0;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Use the isDisjointFrom() method instead. It does the same work in a single call, without creating a temporary array or Set.
ℹ 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.
ℹ Unsafe fix: Use isDisjointFrom() instead.
1 1 │ function hasNothingInCommon(a: Set<string>, b: Set<string>) {
2 │ - ····return·a.intersection(b).size·===·0;
2 │ + ····return·a.isDisjointFrom(b);
3 3 │ }
4 4 │
function compare(a: Set<string>, b: Set<string>) { const all = a.union(b); const shared = a.intersection(b); const hasAll = a.isSubsetOf(b); const hasNothingInCommon = a.isDisjointFrom(b);}
// The rule can't tell whether `first` and `second` are `Set`s.const combined = new Set([...first, ...second]);
// The function doesn't only check whether a `Set` has the value.const large = [...new Set(numbers)].filter((value) => value > 10);See Also
Section titled “See Also”- If you want to disallow copying a value into a new
Setright before or after calling one of these methods, seenoUselessSetConstruction.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.