Skip to content
← Back to rules

unicorn/explicit-length-check Pedantic

🛠️ 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"

기본값: "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

참고 자료