Skip to content

Latest commit

Β 

History

History
75 lines (51 loc) Β· 3.48 KB

prefer-set-methods.md

File metadata and controls

75 lines (51 loc) Β· 3.48 KB

prefer-set-methods

πŸ“ Prefer Set methods for Set operations.

πŸ’ΌπŸš« 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 and manually fixable by editor suggestions.

Prefer modern Set methods for composing Sets and checking their relationships.

The built-in methods express set operations directly and avoid spelling out temporary arrays and membership-check loops.

Examples

// ❌
const union = new Set([...set, ...otherSet]);

// βœ…
const union = set.union(otherSet);
// ❌
const intersection = [...set].filter(value => otherSet.has(value));

// βœ…
const intersection = set.intersection(otherSet);
// ❌
const difference = [...set].filter(value => !otherSet.has(value));

// βœ…
const difference = set.difference(otherSet);

The new Set([...set, ...otherSet]) case is autofixed only when every spread operand is known to be a Set or ReadonlySet, and the operands are safe to reuse in the replacement.

// βœ…
new Set([...iterable, ...otherIterable]);

Intersection and difference filter patterns are reported as suggestions instead of autofixes because bare filters produce arrays. Direct new Set([...set].filter(…)) wrappers are also suggestions: intersection() can change Set iteration order, and difference() follows the same opt-in model for consistency. For filter patterns, the rule only reports bare filter calls and direct new Set([...set].filter(…)) wrappers.

Set predicates

The rule autofixes membership checks and empty intersection or difference checks when both operands are known to be a Set or ReadonlySet:

// ❌
const subset = [...set].every(value => otherSet.has(value));
const overlaps = [...set].some(value => otherSet.has(value));
const disjoint = set.intersection(otherSet).size === 0;
const contained = set.difference(otherSet).size === 0;

// βœ…
const subset = set.isSubsetOf(otherSet);
const overlaps = !set.isDisjointFrom(otherSet);
const disjoint = set.isDisjointFrom(otherSet);
const contained = set.isSubsetOf(otherSet);

Strict inequality (!== 0), reversed comparisons (0 === set.intersection(otherSet).size), and positive size comparisons (set.intersection(otherSet).size > 0 or 0 < set.intersection(otherSet).size) are also supported. Strict inequality and positive size comparisons negate the corresponding predicate.

Negated membership checks are also supported: [...set].every(value => !otherSet.has(value)) becomes set.isDisjointFrom(otherSet), while [...set].some(value => !otherSet.has(value)) becomes !set.isSubsetOf(otherSet).

Callbacks must be synchronous arrow functions with one identifier parameter and a direct otherSet.has(value) or !otherSet.has(value) expression body. Patterns with optional chaining or computed method access are ignored. These autofixes assume ordinary built-in Set behavior.

For both filter and predicate callbacks, evaluating the Set used for membership checks must not have side effects or depend on the callback parameter.