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]*/
let bar = ;
使用預設 { "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 中引入。