Function parentheses and `mix format`

Hi there,

I’m currently following an Elixir tutorial and I have the following lines of code in my project:

Path.join([path, file])
    |> File.read!
    |> split
    |> process(post)

When I run mix format, it gets turned into this:

Path.join([path, file])
    |> File.read!()
    |> split
    |> process(post)

I know this is a minor gripe, but I find it annoying/ugly that (optional) parentheses are added to File.read!/1 but not split/1

How can I configure mix format not to add (optional) parentheses to functions with no/implicit arguments?

I am aware of the .formatter.exs options import_deps: [...] and locals_without_parens: [...] but could not figure out how to add File.read! to these in a way that mix format would accept.

Any ideas?

1 Like

You can not configure the treatment of File.read!/1, as it is not a “local”. :locals_without_parens will only ever work for unqualified calls, those you write without a module in fron tof them.

And parenthesis aren’t really optional, they are considered best practice by the community and most of us actually agree on the formatter in the regard of adding them unconditionally. I’d even consider it a bug, that none are added to split/1.

The main reason why parens have been made optional for the compiler, is probably to make things like def and defmodule writable without having to wrap parenthesis to their arguments.

4 Likes

This is fine – conversely then, how can I make mix format respect best practices and add parentheses to the call to split/1?

I’d like to understand how to enforce parentheses everywhere possible as well.

For those interested, the latest formatter plugin freedom_formatter now has the option to add parenthesis for these calls.

foo |> bar |> baz
# becomes, if `local_pipe_with_parens` option set to `true`:
foo |> bar() |> baz()
2 Likes