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