跳到內容

unicorn/explicit-length-check 吹毛求疵

🛠️ 此規則的部分違規行為可使用自動修正。

功能說明

強制明確比較值的長度或大小屬性。

非零選項可以配置為以下其中一種:greater-than(預設)強制使用 foo.length > 0 檢查非零,而不是 not-equal 強制使用 foo.length !== 0 檢查非零。

為什麼這是不好的?

範例

此規則的不正確程式碼範例

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;

參考資料

根據 MIT 授權發布。