Linter · Rules
unicorn/explicit-length-check
Require length and size to be compared with a number
if (items.length) relies on 0 being falsy. if (items.length > 0) says what you're testing. It also keeps a number out of places where you want a boolean. items.length && render(items) evaluates to 0 for an empty list, not false.
The rule also settles on one spelling for each check: === 0 for empty and > 0 for not empty. It reports other ways to write them, such as < 1, >= 1 and !== 0.
--fix rewrites the check wherever only its truthiness matters. In const first = items.length && items[0] the value itself is used, so that's reported but not fixed.
Configure
Off by default. To turn it on:
[lint.rules]
"unicorn/explicit-length-check" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"unicorn/explicit-length-check" = ["error", {
non-zero = "greater-than",
}]
Examples
Reported:
if (items.length) {
render(items);
}
if (!selected.size) {
disable(button);
}
const hasErrors = errors.length >= 1;
const first = items.length && items[0];
Not reported:
if (items.length > 0) {
render(items);
}
if (selected.size === 0) {
disable(button);
}
const hasErrors = errors.length > 0;
const first = items.length > 0 && items[0];
Optional chains, this.length and a fallback to a number are left alone:
if (items?.length) {
render(items);
}
class Queue extends Array {
isEmpty() {
return !this.length;
}
}
const columns = headers.length || 1;
Options
| Option | Default | What it does |
|---|---|---|
non-zero | "greater-than" | How to write "not empty": "greater-than" requires > 0, "not-equal" requires !== 0. |
if (items.length !== 0) {
render(items);
}
if (items.length > 0) {
render(items);
}