As you may know, ReScript’s formatter has no configuration possibilities. ReScript code should look very similar no matter who wrote it.
However, with line length as the main constraint, you often end up with funny formatting. That’s why @Maxim implemented “smart printing” for records and variants when he wrote the ReScript parser to give the user at least a little bit of control.
If you have a variant type
type animal = Dog | Cat | Mouse
and put a newline anywhere in between the variants
type animal = Dog | Cat
| Mouse
the formatter will yield
type animal =
| Dog
| Cat
| Mouse
Similarly, if you have a record (type or value)
type dog = {name: string, age: int, weight: int}
and put a newline anywhere in between the attributes
type dog = {name: string,
age: int, weight: int}
the formatter will yield
type dog = {
name: string,
age: int,
weight: int,
}
New: Smart linebreaks for pipes
We are happy to announce that the same logic is applied to pipe-chains with the ->
operator now!
For instance, if you have a pipe-chain like this one
let _ = "P"->String.concat("I")->String.concat("P")->String.concat("E")
and put a newline anywhere in between the pipes
let _ = "P"->String.concat("I")
->String.concat("P")->String.concat("E")
the formatter will yield
let _ =
"P"
->String.concat("I")
->String.concat("P")
->String.concat("E")
All of the above examples that are one-liners stay one-liners (since they do not hit the 100 character threshold).
Please enjoy!