curly

Require braces around the bodies of if, else and loops

fixable

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:

bunfig.toml
[lint.rules]
"curly" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"curly" = ["error", "all"] # "all", "multi", "multi-line" or "multi-or-nest"

Examples#

example.js2 problems
if (!user) redirect("/login");

for (const item of items) total += item.price;
example.jsNo problems
if (!user) {
  redirect("/login");
}

for (const item of items) {
  total += item.price;
}

Options#

bunfig.toml
[lint.rules]
curly = ["error", "multi-line", "consistent"]
OptionWhat 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.
example.jswith "multi"1 problem
if (!user) {
  redirect("/login");
}

Braces that the code needs are never reported, for example around a const:

example.jswith "multi"No problems
if (!user) redirect("/login");

if (debug) {
  const started = Date.now();
}

With "multi-line", the first if is allowed and the second one is reported:

example.jswith "multi-line"1 problem
if (!user) redirect("/login");

if (!user.active)
  redirect("/activate");

With "multi-or-nest", the body below is one statement, but it takes three lines:

example.jswith "multi-or-nest"1 problem
if (!user)
  save({
    name: "guest",
  });
example.jswith "multi-or-nest"No problems
if (!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.

example.jswith "multi", "consistent"1 problem
if (user.admin) {
  grant(user);
  notify(user);
} else
  deny(user);
example.jswith "multi", "consistent"No problems
if (user.admin) {
  grant(user);
  notify(user);
} else {
  deny(user);
}