eslint/no-empty-function 제한
작동 방식
빈 함수 사용을 금지합니다.
왜 문제가 되는가?
빈 함수는 가독성을 떨어뜨릴 수 있습니다. 읽는 사람이 해당 빈 함수가 의도적인 것인지 아닌지를 추측해야 하기 때문입니다. 따라서 빈 함수에는 명확한 주석을 추가하는 것이 좋은 관습입니다.
예시
이 규칙에 부적절한 코드 예시:
function foo() {}
const bar = () => {};
class Foo {
constructor();
someMethod() {}
set bar(value) {}
}이 규칙에 적절한 코드 예시:
function foo() {
// 아무 작업 없음
}
function foo() {
return;
}
const add = (a, b) => a + b;
class Foo {
// 생성자 본문은 비어 있지만, `_name`이라는 비공개 속성을 선언함
constructor(private _name: string) {}
public get name() {
return this._name;
}
}구성
이 규칙은 다음 속성을 가진 구성 객체를 수용합니다.
allow
type: array
비어 있는 것으로 허용되는 함수 종류.
기본적으로 어떤 종류의 함수도 비어 있게 허용되지 않지만, 이 옵션을 사용하여 특정 종류의 함수만 허용할 수 있습니다.
예시:
{
"no-empty-function": ["error", { "allow": ["constructors"] }]
}allow[n]
type: "functions" | "arrowFunctions" | "generatorFunctions" | "methods" | "generatorMethods" | "getters" | "setters" | "constructors" | "asyncFunctions" | "asyncMethods" | "privateConstructors" | "protectedConstructors" | "decoratedFunctions" | "overrideMethods"
비어 있도록 허용될 수 있는 함수 유형.
"functions"
빈 일반 함수를 허용합니다.
function foo() {}"arrowFunctions"
빈 화살표 함수를 허용합니다.
const foo = () => {};"generatorFunctions"
빈 제너레이터 함수를 허용합니다.
function* foo() {}"methods"
빈 메서드를 허용합니다.
class Foo {
bar() {}
}"generatorMethods"
빈 제너레이터 메서드를 허용합니다.
class Foo {
*bar() {}
}"getters"
빈 게터를 허용합니다.
class Foo {
get bar() {}
}"setters"
빈 세터를 허용합니다.
class Foo {
set bar(value) {}
}"constructors"
빈 생성자를 허용합니다.
class Foo {
constructor() {}
}"asyncFunctions"
빈 비동기 함수를 허용합니다.
async function foo() {}"asyncMethods"
빈 비동기 메서드를 허용합니다.
class Foo {
async bar() {}
}"privateConstructors"
빈 비공개 생성자를 허용합니다.
class Foo {
private constructor() {}
}"protectedConstructors"
빈 보호된 생성자를 허용합니다.
class Foo {
protected constructor() {}
}"decoratedFunctions"
빈 장식된 함수를 허용합니다.
class Foo {
@decorator()
bar() {}
}"overrideMethods"
빈 오버라이드 메서드를 허용합니다.
class Foo extends Base {
override bar() {}
}사용 방법
이 규칙을 설정 파일 또는 CLI에서 활성화하려면 다음을 사용할 수 있습니다:
{
"rules": {
"no-empty-function": "error"
}
}oxlint --deny no-empty-function