π 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.
// β
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.