no-empty-function
不允許空函數
空函數可能會降低可讀性,因為讀者需要猜測這是有意還是無意的。因此,為空函數編寫清晰的註解是一個好的實踐。
function foo() {
// do nothing.
}
尤其是,箭頭函數的空區塊可能會讓開發人員感到困惑。它與空的物件字面量非常相似。
list.map(() => {}); // This is a block, would return undefined.
list.map(() => ({})); // This is an empty object.
規則詳情
此規則旨在消除空函數。如果函數包含註解,則不會被視為問題。
此規則的錯誤程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: "error"*/
function foo()
const bar = function() ;
const bar1 = () => ;
function* baz()
const bar2 = function*() ;
const obj = {
foo: function() ,
foo: function*() ,
foo() ,
*foo() ,
get foo() ,
set foo(value)
};
class A {
constructor()
foo()
*foo()
get foo()
set foo(value)
static foo()
static *foo()
static get foo()
static set foo(value)
}
此規則的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: "error"*/
function foo() {
// do nothing.
}
const baz = function() {
// any clear comments.
};
const baz1 = () => {
bar();
};
function* foobar() {
// do nothing.
}
const baz2 = function*() {
// do nothing.
};
const obj = {
foo: function() {
// do nothing.
},
foo: function*() {
// do nothing.
},
foo() {
// do nothing.
},
*foo() {
// do nothing.
},
get foo() {
// do nothing.
},
set foo(value) {
// do nothing.
}
};
class A {
constructor() {
// do nothing.
}
foo() {
// do nothing.
}
*foo() {
// do nothing.
}
get foo() {
// do nothing.
}
set foo(value) {
// do nothing.
}
static foo() {
// do nothing.
}
static *foo() {
// do nothing.
}
static get foo() {
// do nothing.
}
static set foo(value) {
// do nothing.
}
}
選項
此規則有一個選項可以允許特定種類的函數為空。
allow
(string[]
) - 允許空函數的種類列表。列表項目是以下字串的其中一些。預設為空陣列 ([]
)。"functions"
- 一般函數。"arrowFunctions"
- 箭頭函數。"generatorFunctions"
- 生成器函數。"methods"
- 類別方法和物件字面量的簡寫方法。"generatorMethods"
- 帶有生成器的類別方法和物件字面量的簡寫方法。"getters"
- Getters。"setters"
- Setters。"constructors"
- 類別建構子。"asyncFunctions"
- 異步函數。"asyncMethods"
- 異步類別方法和物件字面量的簡寫方法。
allow: functions
{ "allow": ["functions"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["functions"] }]*/
function foo() {}
const bar = function() {};
const obj = {
foo: function() {}
};
allow: arrowFunctions
{ "allow": ["arrowFunctions"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["arrowFunctions"] }]*/
const foo = () => {};
allow: generatorFunctions
{ "allow": ["generatorFunctions"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["generatorFunctions"] }]*/
function* foo() {}
const bar = function*() {};
const obj = {
foo: function*() {}
};
allow: methods
{ "allow": ["methods"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["methods"] }]*/
const obj = {
foo() {}
};
class A {
foo() {}
static foo() {}
}
allow: generatorMethods
{ "allow": ["generatorMethods"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["generatorMethods"] }]*/
const obj = {
*foo() {}
};
class A {
*foo() {}
static *foo() {}
}
allow: getters
{ "allow": ["getters"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["getters"] }]*/
const obj = {
get foo() {}
};
class A {
get foo() {}
static get foo() {}
}
allow: setters
{ "allow": ["setters"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["setters"] }]*/
const obj = {
set foo(value) {}
};
class A {
set foo(value) {}
static set foo(value) {}
}
allow: constructors
{ "allow": ["constructors"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["constructors"] }]*/
class A {
constructor() {}
}
allow: asyncFunctions
{ "allow": ["asyncFunctions"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/
async function a(){}
allow: asyncMethods
{ "allow": ["asyncMethods"] }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/
const obj = {
async foo() {}
};
class A {
async foo() {}
static async foo() {}
}
何時不該使用
如果您不想要收到關於空函數的通知,那麼停用此規則是安全的。
相關規則
版本
此規則在 ESLint v2.0.0 中引入。