Skip to content
← Back to rules

unicorn/explicit-length-check 严格

🛠️ An auto-fix is available for this rule for some violations.

它做了什么

强制显式比较一个值的长度或大小属性。

为什么这是不好的?

示例

此规则的 错误 代码示例:

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

type: "greater-than" | "not-equal"

default: "greater-than"

用于指定如何强制执行非零长度检查的配置选项。

greater-than:强制使用 foo.length > 0 检查非零 not-equal:强制使用 foo.length !== 0 检查非零

如何使用

要通过配置文件或命令行 启用 此规则,可以使用:

json
{
  "rules": {
    "unicorn/explicit-length-check": "error"
  }
}
bash
oxlint --deny unicorn/explicit-length-check

参考资料