javascript-prefer-set-size-over-length
Replace incorrect Set cardinality reads like set.length with set.size when the receiver is statically proven to be a built-in Set or ReadonlySet.
What It Rewrites
const s = new Set(values); s.length->s.sizefunction f(s: Set<number>) { return s.length; }->return s.sizemaybeSet?.length->maybeSet?.sizewhen the type isSet | nullishset['length']->set.sizeby default, orset['size']with--param computed_style=computed- Reads narrowed by
instanceof Set,Object.prototype.toString.call(x) === '[object Set]', or a user-defined type guard returningx is Set<...> - Safe
constaliases such asconst alias = set this.cache.lengthand typed property reads when the property is declared asSet<...>orReadonlySet<...>
Safety Rules
- Skips unknown or ambiguous receivers
- Skips arrays, strings, functions, typed arrays, and other normal
.lengthreads - Skips write/update/delete contexts such as
set.length = 1,set.length++, anddelete set.length - Skips dynamic keys like
set[key] - Skips visible subclass overrides such as
new CustomSet().length - Excludes
node_modules,dist,build,coverage, andvendorpaths by default
Usage
bash
Development
bash
License
MIT