在线文档教程
Eslint
规则 | Rules

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]*/ /*eslint-env es6*/ 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 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. };

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

/*eslint max-statements: ["error", 10]*/ /*eslint-env es6*/ 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; return function () { // The number of statements in the inner function does not count toward the // statement maximum. return 42; }; } let 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; return function () { // The number of statements in the inner function does not count toward the // statement maximum. return 42; }; }

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; }

相关规则

  • complexity

  • max-depth

  • max-len

  • max-nested-callbacks

  • max-params

版本

规则在ESLint 0.0.9中引入。

资源