Skip to content

Latest commit

Β 

History

History
39 lines (26 loc) Β· 1.27 KB

File metadata and controls

39 lines (26 loc) Β· 1.27 KB

no-impossible-length-comparison

πŸ“ Disallow impossible comparisons against .length or .size.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

The length property of arrays and strings, and the size property of built-in collections, represent a count, so they cannot be negative. Comparing them against a negative bound, or checking whether they are at least zero, is either always false or always true, which usually means the wrong value or comparison operator was used.

Examples

// ❌
if (array.length < 0) {}

// ❌
if (string.length === -1) {}

// ❌
if (set.size <= -1) {}

// ❌
if (array.length >= 0) {}
// βœ…
if (array.length === 0) {}

// βœ…
if (array.length > 0) {}

// βœ…
if (set.size >= 1) {}

This rule intentionally ignores obvious custom object shape checks such as dimensions.width && dimensions.length < 0, where length may be a domain-specific property instead of collection cardinality. For other intentional custom negative length or size properties, use an ESLint disable comment.