unicorn/explicit-length-check 細心
何を行うか
値の長さやサイズプロパティを明示的に比較することを強制します。
なぜこれが悪いのか
例
このルールに違反する不適切なコードの例:
javascript
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;
const isEmpty = !foo.length;
const isEmpty = !(foo.length > 0);
const isEmptySet = !foo.size;このルールに準拠する適切なコードの例:
javascript
const isEmpty = foo.length === 0;設定
このルールは、以下のプロパティを持つ設定オブジェクトを受け入れます。
non-zero
型: "greater-than" | "not-equal"
デフォルト: "greater-than"
非ゼロ長さのチェックをどのように強制するかを指定する設定オプション。
greater-than: 非ゼロ長さのチェックを foo.length > 0 で強制する not-equal: 非ゼロ長さのチェックを foo.length !== 0 で強制する
使用方法
設定ファイルまたは CLI でこのルールを有効化するには、次のように使用できます:
json
{
"rules": {
"unicorn/explicit-length-check": "error"
}
}bash
oxlint --deny unicorn/explicit-length-check