max-statements
Enforce a maximum number of statements in a function
A function with a long list of statements usually does several jobs, and you have to read all of it to change any part. This rule reports any function with more statements than the maximum, which is 10 by default.
Statements in nested blocks count too. An inner function's statements only count for the inner function.
Configure#
Off by default. To turn it on:
[lint.rules]
"max-statements" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"max-statements" = ["error", 10, {
ignoreTopLevelFunctions = false,
}]Examples#
With a maximum of 3, this function with 4 statements is reported:
31 problemfunction registerFunction register has too many statements (4). Maximum allowed is 3.(form) {
const user = parse(form);
validate(user);
save(user);
sendWelcomeMail(user);
}3No problemsfunction register(form) {
const user = parse(form);
validate(user);
return saveAndWelcome(user);
}This function has 3 statements, the if and the two in its block, so a maximum of 2 reports it:
21 problemfunction logFunction log has too many statements (3). Maximum allowed is 2.(message) {
if (verbose) {
console.log(message);
count++;
}
}Options#
[lint.rules]
max-statements = ["error", 15, { ignoreTopLevelFunctions = true }]The first option is the maximum, as a number or as { "max": number }. maximum is an older name for max. The second option is an object:
| Option | Default | What it does |
|---|---|---|
ignoreTopLevelFunctions | false | Don't report a file's only top-level function, such as a wrapper around a whole script. |
With it, this passes with a maximum of 2:
2, { "ignoreTopLevelFunctions": true }No problems(function () {
const app = createApp();
app.use(router);
app.listen(3000);
})();In a file with two or more top-level functions, the option has no effect:
2, { "ignoreTopLevelFunctions": true }1 problemfunction startFunction start has too many statements (3). Maximum allowed is 2.() {
const app = createApp();
app.use(router);
app.listen(3000);
}
function stop() {}