Enforce explicitly checking the length of a value array in an if
condition, rather than checking the truthiness of the length.
if (string.length) {}
if (array.length) {}
if (!array.length) {}
if (string.length > 0) {}
if (array.length > 0) {}
if (array.length !== 0) {}
if (array.length === 0) {}
Enforce comparison with !== 0
when checking for zero length.
if (string.length < 1) {}
if (array.length !== 0) {}
You can define your preferred way of checking non-zero length by providing a non-zero
option:
{
'unicorn/explicit-length-check': ['error', {
'non-zero': 'not-equal'
}]
}
The non-zero
option can be configured with one of the following:
not-equal
- Enforces non-zero to be checked with:
array.length !== 0
- Enforces non-zero to be checked with:
greater-than
- Enforces non-zero to be checked with:
array.length > 0
- Enforces non-zero to be checked with:
greater-than-or-equal
- Enforces non-zero to be checked with:
array.length >= 1
- Enforces non-zero to be checked with:
It can be auto-fixed.