useExplicitLengthCheck
此内容尚不支持你的语言。
Diagnostic Category: lint/style/useExplicitLengthCheck
Since: v1.7.3
Sources:
- Same as:
unicorn/explicit-length-check
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.
Zero comparison examples
Section titled Zero comparison examplesEnforce comparison with === 0
when checking for zero length.
Invalid
Section titled Invalidcode-block.js:1:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
> 1 │ const isEmpty = !foo.length;
│ ^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length === 0
1 │ - const·isEmpty·=·!foo.length;
1 │ + const·isEmpty·=·foo.length·===·0;
2 2 │
code-block.js:1:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
> 1 │ const isEmpty = foo.length == 0;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length === 0
1 │ const·isEmpty·=·foo.length·===·0;
│ +
code-block.js:1:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
> 1 │ const isEmpty = foo.length < 1;
│ ^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length === 0
1 │ - const·isEmpty·=·foo.length·<·1;
1 │ + const·isEmpty·=·foo.length·===·0;
2 2 │
code-block.js:1:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
> 1 │ const isEmpty = 0 === foo.length;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length === 0
1 │ - const·isEmpty·=·0·===·foo.length;
1 │ + const·isEmpty·=·foo.length·===·0;
2 2 │
code-block.js:1:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
> 1 │ const isEmpty = 0 == foo.length;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length === 0
1 │ - const·isEmpty·=·0·==·foo.length;
1 │ + const·isEmpty·=·foo.length·===·0;
2 2 │
code-block.js:1:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
> 1 │ const isEmpty = 1 > foo.length;
│ ^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length === 0
1 │ - const·isEmpty·=·1·>·foo.length;
1 │ + const·isEmpty·=·foo.length·===·0;
2 2 │
code-block.js:2:17 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length === 0 when checking .length is zero.
1 │ // Negative style is disallowed too
> 2 │ const isEmpty = !(foo.length > 0);
│ ^^^^^^^^^^^^^^^^^
3 │
ℹ Unsafe fix: Replace .length with .length === 0
1 1 │ // Negative style is disallowed too
2 │ - const·isEmpty·=·!(foo.length·>·0);
2 │ + const·isEmpty·=·foo.length·===·0;
3 3 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .size === 0 when checking .size is zero.
> 1 │ const isEmptySet = !foo.size;
│ ^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .size with .size === 0
1 │ - const·isEmptySet·=·!foo.size;
1 │ + const·isEmptySet·=·foo.size·===·0;
2 2 │
Valid
Section titled ValidNon-zero comparison examples
Section titled Non-zero comparison examplesEnforce comparison with > 0
when checking for non-zero length.
Invalid
Section titled Invalidcode-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = foo.length !== 0;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·foo.length·!==·0;
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = foo.length != 0;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·foo.length·!=·0;
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = foo.length >= 1;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·foo.length·>=·1;
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = 0 !== foo.length;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·0·!==·foo.length;
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = 0 != foo.length;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·0·!=·foo.length;
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = 1 <= foo.length;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·1·<=·foo.length;
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:1:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const isNotEmpty = Boolean(foo.length);
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ - const·isNotEmpty·=·Boolean(foo.length);
1 │ + const·isNotEmpty·=·foo.length·>·0;
2 2 │
code-block.js:2:20 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
1 │ // Negative style is disallowed too
> 2 │ const isNotEmpty = !(foo.length === 0);
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ Unsafe fix: Replace .length with .length > 0
1 1 │ // Negative style is disallowed too
2 │ - const·isNotEmpty·=·!(foo.length·===·0);
2 │ + const·isNotEmpty·=·foo.length·>·0;
3 3 │
code-block.js:1:5 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ if (foo.length) {}
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ if·(foo.length·>·0)·{}
│ ++++
code-block.js:1:15 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ const biome = foo.length ? 1 : 2
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ const·biome·=·foo.length·>·0?·1·:·2
│ +++
code-block.js:1:8 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ while (foo.length) {}
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ while·(foo.length·>·0)·{}
│ ++++
code-block.js:1:14 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ do {} while (foo.length);
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ do·{}·while·(·foo.length·>·0);
│ + ++++
code-block.js:1:8 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .length > 0 when checking .length is not zero.
> 1 │ for (; foo.length; ) {};
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .length with .length > 0
1 │ for·(;·foo.length·>·0;·)·{};
│ ++++
Valid
Section titled ValidCaveats
Section titled CaveatsThis 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.
code-block.js:1:37 lint/style/useExplicitLengthCheck FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use .size > 0 when checking .size is not zero.
> 1 │ const foo1 = { size: “small” }; if (foo1.size) {}
│ ^^^^^^^^^
2 │
ℹ Unsafe fix: Replace .size with .size > 0
1 │ const·foo1·=·{·size:·“small”·};·if·(foo1.size·>·0)·{}
│ ++++
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.