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
Type
boolean
Default
false
oneLineBodiesOptional
The statement kind or kinds that sit on one line, without braces, after a lone if. Declarations always keep their braces