max-statements

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

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

¥The max-statements rule allows you to specify the maximum number of statements allowed in a function.

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

规则详情

¥Rule Details

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

¥This rule enforces a maximum number of statements allowed in function blocks.

选项

¥Options

此规则有一个数字或对象选项:

¥This rule has a number or object option:

  • "max"(默认 10)强制功能块中允许的最大语句数

    ¥"max" (default 10) enforces a maximum number of statements allows in function blocks

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

¥Deprecated: The object property maximum is deprecated; please use the object property max instead.

此规则有一个对象选项:

¥This rule has an object option:

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

    ¥"ignoreTopLevelFunctions": true ignores top-level functions

max

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

¥Examples of incorrect code for this rule with the default { "max": 10 } option:

在线运行
/*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 } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the default { "max": 10 } option:

在线运行
/*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;
  };
}

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

¥Note that this rule does not apply to class static blocks, and that statements in class static blocks do not count as statements in the enclosing function.

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

¥Examples of correct code for this rule with { "max": 2 } option:

在线运行
/*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 } 选项的此规则的附加正确代码示例:

¥Examples of additional correct code for this rule with the { "max": 10 }, { "ignoreTopLevelFunctions": true } options:

在线运行
/*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 中引入。

资源

ESLint 中文网
粤ICP备13048890号