Skip to content
← Back to rules

eslint/id-length 스타일

작동 방식

이 규칙은 주어진 식별자에 대해 그래프먼을 세어 최소 및/또는 최대 식별자 길이 규약을 적용합니다.

왜 나쁜가요?

매우 짧은 식별자 이름 예: e, x, _t 또는 매우 긴 이름 예: hashGeneratorResultOutputContainerObject는 코드를 읽기 어렵게 만들고 유지보수성이 떨어질 수 있습니다. 이를 방지하기 위해 최소 및/또는 최대 식별자 길이를 강제할 수 있습니다.

예시

이 규칙에 부적절한 코드 예시:

js
/* id-length: "error" */ // 기본값은 최소 2글자 ({ "min": 2 })

const x = 5;
obj.e = document.body;
const foo = function (e) {};
try {
  dangerousStuff();
} catch (e) {
  // 많은 사람들이 그러듯 무시
}
const myObj = { a: 1 };
(a) => {
  a * a;
};
class y {}
class Foo {
  x() {}
}
class Bar {
  #x() {}
}
class Baz {
  x = 1;
}
class Qux {
  #x = 1;
}
function bar(...x) {}
function baz([x]) {}
const [z] = arr;
const {
  prop: [i],
} = {};
function qux({ x }) {}
const { j } = {};
const { prop: a } = {};
({ prop: obj.x } = {});

이 규칙에 적절한 코드 예시:

js
/* id-length: "error" */ // 기본값은 최소 2글자 ({ "min": 2 })

const num = 5;
function _f() {
  return 42;
}
function _func() {
  return 42;
}
obj.el = document.body;
const foo = function (evt) {
  /* 작업 수행 */
};
try {
  dangerousStuff();
} catch (error) {
  // 많은 사람들이 그러듯 무시
}
const myObj = { apple: 1 };
(num) => {
  num * num;
};
function bar(num = 0) {}
class MyClass {}
class Foo {
  method() {}
}
class Bar {
  #method() {}
}
class Baz {
  field = 1;
}
class Qux {
  #field = 1;
}
function baz(...args) {}
function qux([longName]) {}
const { prop } = {};
const {
  prop: [name],
} = {};
const [longName] = arr;
function foobar({ prop }) {}
function foobaz({ a: prop }) {}
const { a: property } = {};
({ prop: obj.longName } = {});
const data = { x: 1 }; // 따옴표로 인해 제외됨
data["y"] = 3; // 계산된 속성 접근으로 인해 제외됨

구성

이 규칙은 다음 속성을 가진 구성 객체를 수락합니다.

exceptionPatterns

type: string[]

규칙에서 제외할 식별자를 위한 정규식 패턴 배열입니다. 예: ["^x.*"]은 "x"로 시작하는 모든 식별자를 제외합니다.

exceptions

type: string[]

default: []

규칙에서 제외할 식별자 이름 배열입니다. 예: ["x", "y", "z"]는 단일 문자 식별자 "x", "y", "z"를 허용합니다.

max

type: integer

default: 18446744073709551615

식별자에 허용되는 그래프먼 최대 수입니다. 기본값은 최대치 없음(실제로 무제한).

min

type: integer

default: 2

식별자에 필요한 그래프먼 최소 수입니다.

properties

type: "always" | "never"

default: "always"

"never"로 설정하면 속성 이름의 길이 검사가 수행되지 않습니다. "always"(기본값)로 설정하면 다른 식별자와 마찬가지로 속성 이름도 검사됩니다.

사용 방법

구성 파일이나 명령줄에서 이 규칙을 활성화하려면 다음과 같이 사용할 수 있습니다:

json
{
  "rules": {
    "id-length": "error"
  }
}
bash
oxlint --deny id-length

참고 자료