Skip to content

Latest commit

 

History

History
57 lines (40 loc) · 1.51 KB

no-useless-length-check.md

File metadata and controls

57 lines (40 loc) · 1.51 KB

Disallow useless array length check

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

  • Array#some() returns false for an empty array. There is no need to check if the array is not empty.
  • Array#every() returns true for an empty array. There is no need to check if the array is empty.

We only check .length === 0, .length !== 0, and .length > 0. These zero and non-zero length check styles are allowed in the unicorn/explicit-length-check rule. It is recommended to use them together.

Fail

if (array.length === 0 || array.every(Boolean));
if (array.length !== 0 && array.some(Boolean));
if (array.length > 0 && array.some(Boolean));
const isAllTrulyOrEmpty = array.length === 0 || array.every(Boolean);

Pass

if (array.every(Boolean));
if (array.some(Boolean));
const isAllTrulyOrEmpty = array.every(Boolean);
if (array.length === 0 || anotherCheck() || array.every(Boolean));
const isNonEmptyAllTrulyArray = array.length > 0 && array.every(Boolean);
const isEmptyArrayOrAllTruly = array.length === 0 || array.some(Boolean);