版本

max-lines-per-function

強制函數中程式碼的最大行數

有些人認為大型函數是一種程式碼壞味道。大型函數往往做很多事情,而且可能讓人難以追蹤正在發生的事情。許多程式碼風格指南規定了函數可以包含的行數限制。此規則可以幫助強制執行該風格。

規則詳情

此規則強制每個函數的最大行數,以幫助維護性和降低複雜度。

為何不使用 max-statements 或其他複雜度測量規則?

如下例所示,巢狀長方法鏈通常會為了可讀性而分成多行

function() {
    return m("div", [
        m("table", {className: "table table-striped latest-data"}, [
            m("tbody",
                data.map(function(db) {
                    return m("tr", {key: db.dbname}, [
                        m("td", {className: "dbname"}, db.dbname),
                        m("td", {className: "query-count"},  [
                            m("span", {className: db.lastSample.countClassName}, db.lastSample.nbQueries)
                        ])
                    ])
                })
            )
        ])
    ])
}
  • max-statements 只會將此回報為 1 個陳述式,儘管它有 16 行程式碼。
  • complexity 只會回報複雜度為 1
  • max-nested-callbacks 只會回報 1
  • max-depth 將回報深度為 0

選項

此規則具有以下選項,可以使用物件指定

  • "max"(預設值 50)強制函數中的最大行數。

  • "skipBlankLines"(預設值 false)忽略僅由空格組成的行。

  • "skipComments"(預設值 false)忽略僅包含註解的行。

  • "IIFEs"(預設值 false)包含 IIFE 中包含的任何程式碼。

或者,您可以為 max 選項指定單一整數

"max-lines-per-function": ["error", 20]

等同於

"max-lines-per-function": ["error", { "max": 20 }]

code

針對具有特定最大值的此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    const x = 0;
}
在 Playground 中開啟
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
    // a comment
    const x = 0;
}
在 Playground 中開啟
/*eslint max-lines-per-function: ["error", 4]*/
function foo() {
    // a comment followed by a blank line

    const x = 0;
}

針對具有特定最大值的此規則的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
    const x = 0;
}
在 Playground 中開啟
/*eslint max-lines-per-function: ["error", 4]*/
function foo() {
    // a comment
    const x = 0;
}
在 Playground 中開啟
/*eslint max-lines-per-function: ["error", 5]*/
function foo() {
    // a comment followed by a blank line

    const x = 0;
}

skipBlankLines

針對具有 { "skipBlankLines": true } 選項的此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", {"max": 2, "skipBlankLines": true}]*/
function foo() {

    const x = 0;
}

針對具有 { "skipBlankLines": true } 選項的此規則的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", {"max": 3, "skipBlankLines": true}]*/
function foo() {

    const x = 0;
}

skipComments

針對具有 { "skipComments": true } 選項的此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", {"max": 2, "skipComments": true}]*/
function foo() {
    // a comment
    const x = 0;
}

針對具有 { "skipComments": true } 選項的此規則的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", {"max": 3, "skipComments": true}]*/
function foo() {
    // a comment
    const x = 0;
}

IIFEs

針對具有 { "IIFEs": true } 選項的此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", {"max": 2, "IIFEs": true}]*/
(function(){
    const x = 0;
}());

(() => {
    const x = 0;
})();

針對具有 { "IIFEs": true } 選項的此規則的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/
(function(){
    const x = 0;
}());

(() => {
    const x = 0;
})();

何時不該使用它

如果您不關心函數中的行數,則可以關閉此規則。

版本

此規則在 ESLint v5.0.0 中引入。

資源

變更語言