版本

max-statements

强制执行函数块中允许的最大语句数

max-statements 规则允许您指定函数中允许的最大语句数。

function foo() {
  var bar = 1; // one statement
  var baz = 2; // two statements
  var qux = 3; // three statements
}

规则详情

此规则强制执行函数块中允许的最大语句数。

选项

此规则具有数字或对象选项

  • "max" (默认值为 10) 强制执行函数块中允许的最大语句数

已弃用:对象属性 maximum 已弃用;请改用对象属性 max

此规则具有对象选项

  • "ignoreTopLevelFunctions": true 忽略顶级函数

max

此规则的不正确代码示例,使用默认 { "max": 10 } 选项

在游乐场中打开
/*eslint max-statements: ["error", 10]*/

function foo() {
  var foo1 = 1;
  var foo2 = 2;
  var foo3 = 3;
  var foo4 = 4;
  var foo5 = 5;
  var foo6 = 6;
  var foo7 = 7;
  var foo8 = 8;
  var foo9 = 9;
  var foo10 = 10;

  var foo11 = 11; // Too many.
}

let bar = () => {
  var foo1 = 1;
  var foo2 = 2;
  var foo3 = 3;
  var foo4 = 4;
  var foo5 = 5;
  var foo6 = 6;
  var foo7 = 7;
  var foo8 = 8;
  var foo9 = 9;
  var foo10 = 10;

  var foo11 = 11; // Too many.
};

此规则的正确代码示例,使用默认 { "max": 10 } 选项

在游乐场中打开
/*eslint max-statements: ["error", 10]*/

function foo() {
  var foo1 = 1;
  var foo2 = 2;
  var foo3 = 3;
  var foo4 = 4;
  var foo5 = 5;
  var foo6 = 6;
  var foo7 = 7;
  var foo8 = 8;
  var foo9 = 9;
  return function () { // 10

    // The number of statements in the inner function does not count toward the
    // statement maximum.

    var bar;
    var baz;
    return 42;
  };
}

let bar = () => {
  var foo1 = 1;
  var foo2 = 2;
  var foo3 = 3;
  var foo4 = 4;
  var foo5 = 5;
  var foo6 = 6;
  var foo7 = 7;
  var foo8 = 8;
  var foo9 = 9;
  return function () { // 10

    // The number of statements in the inner function does not count toward the
    // statement maximum.

    var bar;
    var baz;
    return 42;
  };
}

请注意,此规则不适用于类静态块,并且类静态块中的语句不计入封闭函数中的语句。

此规则的正确代码示例,使用 { "max": 2 } 选项

在游乐场中打开
/*eslint max-statements: ["error", 2]*/

function foo() {
    let one;
    let two = class {
        static {
            let three;
            let four;
            let five;
            if (six) {
                let seven;
                let eight;
                let nine;
            }
        }
    };
}

ignoreTopLevelFunctions

此规则的附加正确代码示例,使用 { "max": 10 }, { "ignoreTopLevelFunctions": true } 选项

在游乐场中打开
/*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/

function foo() {
  var foo1 = 1;
  var foo2 = 2;
  var foo3 = 3;
  var foo4 = 4;
  var foo5 = 5;
  var foo6 = 6;
  var foo7 = 7;
  var foo8 = 8;
  var foo9 = 9;
  var foo10 = 10;
  var foo11 = 11;
}

版本

此规则是在 ESLint v0.0.9 中引入的。

资源

更改语言