コンテンツにスキップ

useExplicitLengthCheck

このコンテンツはまだ日本語訳がありません。

Diagnostic Category: lint/style/useExplicitLengthCheck

Since: v1.7.3

Sources:

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;
code-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

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 == 0;
^^^^^^^^^^^^^^^
2 │

Unsafe fix: Replace .length with .length === 0

1 │ const·isEmpty·=·foo.length·===·0;
+
const isEmpty = foo.length < 1;
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

const isEmpty = 0 === foo.length;
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

const isEmpty = 0 == foo.length;
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

const isEmpty = 1 > foo.length;
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

// Negative style is disallowed too
const isEmpty = !(foo.length > 0);
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

const isEmptySet = !foo.size;
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

const isEmpty = foo.length === 0;

Enforce comparison with > 0 when checking for non-zero length.

const isNotEmpty = foo.length !== 0;
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

const isNotEmpty = foo.length != 0;
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

const isNotEmpty = foo.length >= 1;
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

const isNotEmpty = 0 !== foo.length;
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

const isNotEmpty = 0 != foo.length;
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

const isNotEmpty = 1 <= foo.length;
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

const isNotEmpty = Boolean(foo.length);
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

// Negative style is disallowed too
const isNotEmpty = !(foo.length === 0);
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

if (foo.length) {}
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)·{}
++++
const biome = foo.length ? 1 : 2
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
+++
while (foo.length) {}
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)·{}
++++
do {} while (foo.length);
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);
+ ++++
for (; foo.length; ) {};
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;·)·{};
++++
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) {}
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.