Skip to content

Curly

SuggestionAuto-fixable
small-rules/curly

Require one brace style for each if, else, and loop body: one line for a lone if with a short body that fits, braces for every other body.

Rationale

Core curly lets each body pick its own brace style, so one file mixes if (done) return;, the same guard wrapped onto two lines, and the same guard in braces. This rule gives each body one style. When an if has no else and its body is one return, break, or continue, the body goes on the same line without braces, as long as that line fits. Braces there are an error. Every other if body takes braces, including a throw and an object or array return. Every branch of an if with an else takes them, and so does every loop body. Turn core curly off when you turn this one on.

A guard that would pass printWidth on one line keeps its braces. That width check stops the rule and the formatter from undoing each other. Set printWidth and tabWidth to the formatter’s values. The defaults, 120 and 4, match this repository’s oxfmt configuration.

oneLineBodies picks the statements that go on one line after an if without an else. It takes the statement kinds that require-readable-spacing uses, such as throw or expression, plus break and continue. A declaration keeps its braces whatever the option says, since if (ready) const value = 1; doesn’t parse. Set allowLiteralReturns to put an object or array return on one line too.

The fix adds or removes braces. It leaves the code alone when a comment comes between the condition and the body, since moving the braces would drop the comment.

Diagnostic Messages

complexBody
Wrap this `if` body in braces. Only a lone `if` whose one statement fits on one line and is a kind `oneLineBodies` allows omits them. By default those are `return`, `break`, and `continue`, and an object or array `return` keeps its braces.
elseChain
Wrap this branch in braces. Every branch of an `if` with an `else` needs them.
loopBody
Wrap the loop body in braces.
simpleBody
Put this `if` and its body on one line, without braces. A lone `if` whose one statement fits on one line and is a kind `oneLineBodies` allows has no braces.

Configuration

This rule accepts one options object after the severity.

allowLiteralReturnsOptional

Let a return of an object or array literal sit on one line after a lone if

oneLineBodiesOptional

The statement kind or kinds that sit on one line, without braces, after a lone if. Declarations always keep their braces

printWidthOptional

The longest line, in columns, that a one-line if may make; match the formatter

tabWidthOptional

The columns a tab counts as when measuring a line; match the formatter

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/curly": [
"error",
{
"allowLiteralReturns": false,
"oneLineBodies": [
"break",
"continue",
"return"
],
"printWidth": 120,
"tabWidth": 4
}
]
}
}

Examples

Unbraced body that is not a jump
function effects(x, list, y) {
if (x) list.push(y);
if (x) y += 1;
}

After auto-fix

function effects(x, list, y) {
if (x) {
list.push(y);
}
if (x) {
y += 1;
}
}
Unbraced branches of an if with an else
function branches(a) {
if (a) return 1;
else return 2;
}

After auto-fix

function branches(a) {
if (a) {
return 1;
} else {
return 2;
}
}
Unbraced loop bodies
function loops(items, table) {
for (const item of items) visit(item);
for (const key in table) visit(key);
for (let index = 0; index < 3; index += 1) visit(index);
for (const item of items) if (item < 0) return;
for (const item of items)
visit(item);
while (pending()) step();
do step(); while (pending());
while (advance());
}

After auto-fix

function loops(items, table) {
for (const item of items) {
visit(item);
}
for (const key in table) {
visit(key);
}
for (let index = 0; index < 3; index += 1) {
visit(index);
}
for (const item of items) {
if (item < 0) return;
}
for (const item of items) {
visit(item);
}
while (pending()) {
step();
}
do {
step();
} while (pending());
while (advance()) {}
}
Braced jump that fits on one line
function guard(value) {
if (value === undefined) {
return 0;
}
}

After auto-fix

function guard(value) {
if (value === undefined) return 0;
}
One-line jumps and braced bodies
function guards(value, items) {
if (value === undefined) return 0;
if (value === "skip") return computeFallback(value, items);
for (const item of items) {
if (item < 0) continue;
if (item > 10) break;
}
}