版本

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 中引入。

资源

更改语言