src/content/docs/linter/rules/use-explicit-length-check.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.7.3` - Diagnostic Category: [`lint/style/useExplicitLengthCheck`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`unicorn/explicit-length-check`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/explicit-length-check.md){
"linter": {
"rules": {
"style": {
"useExplicitLengthCheck": "error"
}
}
}
}
Enforce explicitly comparing the length, size, byteLength or byteOffset property of a value.
This rule enforces a specific style length comparisons to make them more clear.
Enforce comparison with === 0 when checking for zero length.
const isEmpty = !foo.length;
const isEmpty = foo.length == 0;
const isEmpty = foo.length < 1;
const isEmpty = 0 === foo.length;
const isEmpty = 0 == foo.length;
const isEmpty = 1 > foo.length;
// Negative style is disallowed too
const isEmpty = !(foo.length > 0);
const isEmptySet = !foo.size;
const isEmpty = foo.length === 0;
Enforce comparison with > 0 when checking for non-zero length.
const isNotEmpty = foo.length !== 0;
const isNotEmpty = foo.length != 0;
const isNotEmpty = foo.length >= 1;
const isNotEmpty = 0 !== foo.length;
const isNotEmpty = 0 != foo.length;
const isNotEmpty = 1 <= foo.length;
const isNotEmpty = Boolean(foo.length);
// Negative style is disallowed too
const isNotEmpty = !(foo.length === 0);
if (foo.length) {}
const biome = foo.length ? 1 : 2
while (foo.length) {}
do {} while (foo.length);
for (; foo.length; ) {};
const isNotEmpty = foo.length > 0;
if (foo.length > 0 || bar.length > 0) {}
This rule assumes that the length/size property is always numeric, even if it actually is not.
In the example below the rule will trigger a warning, even though the size property is a string.
const foo1 = { size: "small" }; if (foo1.size) {}
To properly handle this case, type inference would be required, which is not supported by Biome at the moment.
We recommend disabling this rule when working with non-numeric length/size properties.