max-statements-per-line

强制执行每行允许的最大语句数

包含太多语句的代码行可能难以阅读。代码通常是自上而下阅读的,尤其是在扫描时,因此限制单行允许的语句数量对可读性和可维护性非常有益。

function foo () { var bar; if (condition) { bar = 1; } else { bar = 2; } return true; } // too many statements

规则详情

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

选项

max

"max" 对象属性是可选的(默认值:1).

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

/*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 foo = function foo() { bar = 1; };
(function foo() { bar = 1; })();

vGUxWBhIHkIwFaCplge82/dSLl+xgR4P5wY7J2DJ0ZWVXD3cCbgkzlxce4hmhyPNfHkUSBc9bYeQOW93iELqrE4hRB439Sqw3HNn1v36QJg=

/*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 foo = function foo() { };
(function foo() { })();

qVCMgjR5tKUNSBmRY5fDOcTBKBpUfR5et8dM0dzP4VZJLLPZKE6idp37XHXtDFTvEZMR1GeNhzB1lffd8TcO7XYgkXPpZso6l+Lw+utgwrI=

/*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 foo = function foo() { bar = 1; };
(function foo() { bar = 1; baz = 2; })();

qVCMgjR5tKUNSBmRY5fDOcTBKBpUfR5et8dM0dzP4VZJLLPZKE6idp37XHXtDFTv59DfDi4cVPYT9oARMyqrEqVrhDzY2pI4rlYuoKomD8g=

/*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 foo = function foo() { bar = 1; };
(function foo() { var bar = 1; })();

何时不使用

dvppF/nq3GR2SuN5vH7IkE9xHRH0/Hlu85RwmVIgtfITLsdKN2hzWCER9xHOlhJpU/R0lwSeMXx3/VY8OuOmlA==