Skip to content

Latest commit

Β 

History

History
49 lines (33 loc) Β· 1.94 KB

File metadata and controls

49 lines (33 loc) Β· 1.94 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 over manually composing Sets with spread arrays and filters.

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. The rule only reports bare filter calls and direct new Set([...set].filter(…)) wrappers.