Skip to content
← Back to rules

eslint/prefer-destructuring 스타일

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

작동 방식

배열과/또는 객체에서의 구조 분해를 요구합니다

왜 나쁜가요?

자바스크립트 ES2015부터 배열 인덱스 또는 객체 속성을 기반으로 변수를 생성하는 새로운 문법인 구조 분해(destructuring)가 도입되었습니다. 이 규칙은 멤버 표현식을 통해 속성에 접근하는 것보다 구조 분해를 사용하도록 강제합니다.

예시

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

js
// `array`가 활성화된 경우
const foo = array[0];
bar.baz = array[0];
// `object`가 활성화된 경우
const qux = object.qux;
const quux = object["quux"];

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

js
// `array`가 활성화된 경우
const [foo] = array;
const arr = array[someIndex];
[bar.baz] = array;

// `object`가 활성화된 경우
const { baz } = object;
const obj = object.bar;

구성

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

AssignmentExpression

type: object

기본값: {"array":true, "object":true}

할당 표현식에서의 구조 분해 설정이며, 배열과 객체별로 별도로 구성됩니다.

AssignmentExpression.array

type: boolean

기본값: true

AssignmentExpression.object

type: boolean

기본값: true

VariableDeclarator

type: object

기본값: {"array":true, "object":true}

변수 선언에서의 구조 분해 설정이며, 배열과 객체별로 별도로 구성됩니다.

VariableDeclarator.array

type: boolean

기본값: true

VariableDeclarator.object

type: boolean

기본값: true

enforceForRenamedProperties

type: boolean

기본값: false

객체 구조 분해 규칙이 이름이 변경된 변수에도 적용되는지를 결정합니다.

사용 방법

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

json
{
  "rules": {
    "prefer-destructuring": "error"
  }
}
bash
oxlint --deny prefer-destructuring

참고 자료