enforce a maximum function length (max-lines-per-function)

强制函数最大行数 (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.

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

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)
                        ])
                    ])
                })
            )
        ])
    ])
}

Options

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

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

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

此外,你也可以为 max 选项指定一个整数:

"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 max value of 2:

最大行数为 2错误 代码示例:

/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    var x = 0;
}
/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    // a comment
    var x = 0;
}
/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    // a comment followed by a blank line

    var x = 0;
}

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

最大行数为 3正确 代码示例:

/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
    var x = 0;
}
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
    // a comment
    var x = 0;
}
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
    // a comment followed by a blank line

    var x = 0;
}

skipBlankLines

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

选项 { "skipBlankLines": true }错误 代码示例:

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

    var x = 0;
}

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

{ "skipBlankLines": true }正确 代码示例:

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

    var x = 0;
}

skipComments

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

选项 { "skipComments": true }错误 代码示例:

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

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

选项 { "skipComments": true }正确 代码示例:

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

IIFEs

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

选项 { "IIFEs": true }错误 代码示例:

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

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

选项 { "IIFEs": true }正确 代码示例:

/*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/
(function(){
    var 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.

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

Version

This rule was introduced in ESLint 5.0.0.

该规则在 ESLint 5.0.0 中被引入。

Resources