curly
Require braces around the bodies of if, else and loops
Without braces, only the next statement belongs to the if or the loop. If you add a second line below it with the same indentation, that line runs every time, no matter what the condition says.
--fix adds the missing braces, or removes the ones your style doesn't want.
Configure#
Off by default. To turn it on:
[lint.rules]
"curly" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"curly" = ["error", "all"] # "all", "multi", "multi-line" or "multi-or-nest"Examples#
if (!user) redirect("/login");Expected { after if condition.
for (const item of items) total += item.price;Expected { after for-of.if (!user) {
redirect("/login");
}
for (const item of items) {
total += item.price;
}Options#
[lint.rules]
curly = ["error", "multi-line", "consistent"]| Option | What it does |
|---|---|
"all" (default) | Braces everywhere. |
"multi" | Braces only around more than one statement. Braces around a single statement are reported. |
"multi-line" | You can leave the braces out if the body is on the same line as the if or the loop. |
"multi-or-nest" | No braces around a single statement that fits on one line. Braces around a body that takes several lines. |
"multi"1 problemif (!user) {Unnecessary { after if condition.
redirect("/login");
}Braces that the code needs are never reported, for example around a const:
"multi"No problemsif (!user) redirect("/login");
if (debug) {
const started = Date.now();
}With "multi-line", the first if is allowed and the second one is reported:
"multi-line"1 problemif (!user) redirect("/login");
if (!user.active)
redirect("/activate");Expected { after if condition.With "multi-or-nest", the body below is one statement, but it takes three lines:
"multi-or-nest"1 problemif (!user)
save({Expected { after if condition.
name: "guest",
});"multi-or-nest"No problemsif (!user)
redirect("/login");
if (user.admin) {
save({
role: "admin",
});
}With any option except "all", you can add "consistent" as a second option. It makes all branches of an if / else chain look the same. If one of them needs braces, they all get braces.
"multi", "consistent"1 problemif (user.admin) {
grant(user);
notify(user);
} else
deny(user);Expected { after else."multi", "consistent"No problemsif (user.admin) {
grant(user);
notify(user);
} else {
deny(user);
}