max-statements-per-line
强制执行每行允许的最大语句数
This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin.
包含太多语句的代码行可能难以阅读。代码通常是自上而下阅读的,尤其是在扫描时,因此限制单行允许的语句数量对可读性和可维护性非常有益。
¥A line of code containing too many statements can be difficult to read. Code is generally read from the top down, especially when scanning, so limiting the number of statements allowed on a single line can be very beneficial for readability and maintainability.
function foo () { var bar; if (condition) { bar = 1; } else { bar = 2; } return true; } // too many statements
规则详情
¥Rule Details
此规则强制执行每行允许的最大语句数。
¥This rule enforces a maximum number of statements allowed per line.
选项
¥Options
max
“max” 对象属性是可选的(默认值:1)。
¥The “max” object property is optional (default: 1).
使用默认 { "max": 1 }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "max": 1 }
option:
/*eslint max-statements-per-line: ["error", { "max": 1 }]*/
var bar; var baz;
if (condition) { bar = 1; }
for (var i = 0; i < length; ++i) { bar = 1; }
switch (discriminant) { default: break; }
function foo() { bar = 1; }
var qux = function qux() { bar = 1; };
(function foo() { bar = 1; })();
使用默认 { "max": 1 }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "max": 1 }
option:
/*eslint max-statements-per-line: ["error", { "max": 1 }]*/
var bar, baz;
if (condition) bar = 1;
for (var i = 0; i < length; ++i);
switch (discriminant) { default: }
function foo() { }
var qux = function qux() { };
(function foo() { })();
使用 { "max": 2 }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "max": 2 }
option:
/*eslint max-statements-per-line: ["error", { "max": 2 }]*/
var bar; var baz; var qux;
if (condition) { bar = 1; } else { baz = 2; }
for (var i = 0; i < length; ++i) { bar = 1; baz = 2; }
switch (discriminant) { case 'test': break; default: break; }
function foo() { bar = 1; baz = 2; }
var qux = function qux() { bar = 1; baz = 2; };
(function foo() { bar = 1; baz = 2; })();
使用 { "max": 2 }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "max": 2 }
option:
/*eslint max-statements-per-line: ["error", { "max": 2 }]*/
var bar; var baz;
if (condition) bar = 1; if (condition) baz = 2;
for (var i = 0; i < length; ++i) { bar = 1; }
switch (discriminant) { default: break; }
function foo() { bar = 1; }
var qux = function qux() { bar = 1; };
(function foo() { var bar = 1; })();
何时不使用
¥When Not To Use It
如果你不关心每行的语句数,可以关闭此规则。
¥You can turn this rule off if you are not concerned with the number of statements on each line.
相关规则
版本
此规则是在 ESLint v2.5.0 中引入。