Index

max-lines-per-function

在函数中强制执行最大代码行数

有些人认为大型函数是一种代码异味。大型函数往往执行许多操作,这可能会让人难以理解发生了什么。许多编码风格指南规定函数的行数上限。这个规则可以帮助强制执行这种风格。

🌐 Some people consider large functions a code smell. Large functions tend to do a lot of things and can make it hard following what’s going on. Many coding style guides dictate a limit of the number of lines that a function can comprise of. This rule can help enforce that style.

规则详情

🌐 Rule Details

此规则强制每个函数的最大行数,以帮助可维护性并降低复杂性。

🌐 This rule enforces a maximum number of lines per function, in order to aid in maintainability and reduce complexity.

为什么不使用 max-statements 或其他复杂性测量规则呢?

🌐 Why not use max-statements or other complexity measurement rules instead?

为了便于阅读,像下面的示例这样的嵌套长方法链通常被分成单独的行:

🌐 Nested long method chains like the below example are often broken onto separate lines for readability:

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 仅会把这算作一条语句,尽管它有16行代码。
  • complexity 将只报告复杂度为 1
  • max-nested-callbacks 只会报告 1
  • max-depth 将报告深度为 0

选项

🌐 Options

此规则具有以下可使用对象指定的选项:

🌐 This rule has the following options that can be specified using an object:

  • "max"(默认 50)强制限制函数中的最大行数。
  • "skipBlankLines"(默认 false)忽略由纯空白组成的行。
  • "skipComments"(默认 false)忽略仅包含注释的行。
  • "IIFEs"(默认 false)包括任何包含在 IIFE 中的代码。

或者,你可以为 max 选项指定一个整数:

🌐 Alternatively, you may specify a single integer for the max option:

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

相当于

🌐 is equivalent to

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

code

此规则在特定最大值下的错误代码示例:

🌐 Examples of incorrect code for this rule with a particular max value:

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

    const x = 0;
}

此规则的正确代码示例,带有特定的最大值:

🌐 Examples of correct code for this rule with a particular max value:

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

    const x = 0;
}

skipBlankLines

使用 { "skipBlankLines": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "skipBlankLines": true } option:

在线运行
/*eslint max-lines-per-function: ["error", {"max": 2, "skipBlankLines": true}]*/
function foo() {

    const x = 0;
}

使用 { "skipBlankLines": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "skipBlankLines": true } option:

在线运行
/*eslint max-lines-per-function: ["error", {"max": 3, "skipBlankLines": true}]*/
function foo() {

    const x = 0;
}

skipComments

使用 { "skipComments": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "skipComments": true } option:

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

使用 { "skipComments": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "skipComments": true } option:

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

立即调用函数表达式

🌐 IIFEs

使用 { "IIFEs": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "IIFEs": true } option:

在线运行
/*eslint max-lines-per-function: ["error", {"max": 2, "IIFEs": true}]*/
(function(){
    const x = 0;
}());

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

使用 { "IIFEs": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "IIFEs": true } option:

在线运行
/*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/
(function(){
    const x = 0;
}());

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

何时不使用

🌐 When Not To Use It

如果你不关心函数中的行数,可以关闭此规则。

🌐 You can turn this rule off if you are not concerned with the number of lines in your functions.

版本

此规则是在 ESLint v5.0.0 中引入。

资源