Skip to content
← Back to rules

unicorn/no-immediate-mutation Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

무엇을 하는가

변수를 초기화한 직후에 변경하는 것을 금지합니다.

왜 나쁜가요?

변수를 초기화한 다음 바로 변경하는 경우, 변경 작업을 초기화 과정에 포함시키는 것이 더 깔끔합니다. 이렇게 하면 코드의 가독성이 높아지고 문장 수가 줄어듭니다.

예시

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

js
const array = [1, 2];
array.push(3);

const object = { foo: 1 };
object.bar = 2;

const set = new Set([1, 2]);
set.add(3);

const map = new Map([["foo", 1]]);
map.set("bar", 2);

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

js
const array = [1, 2, 3];

const object = { foo: 1, bar: 2 };

const set = new Set([1, 2, 3]);

const map = new Map([
  ["foo", 1],
  ["bar", 2],
]);

사용 방법

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

json
{
  "rules": {
    "unicorn/no-immediate-mutation": "error"
  }
}
bash
oxlint --deny unicorn/no-immediate-mutation

참고 자료