Automatic Elixir Code Transforms (“advanced” `mix.format`)

We’re looking to make a style change in one of our projects, and would like to rewrite single-line function definitions to multi-line function definitions. That is, from:

def foo, do: true

to

def foo do
  true
end

Some of our definitions could be done with a regex search/replace, but not all of them, and even then I have…concerns about whether it would be the same code.

Is there something we could adapt from the code in mix.format to have Elixir do this code change for us?

Code.format_string!/2 has force_do_end_blocks option.

4 Likes

This option does produce a very interesting code formatting change under some circumstances. Given the following:

defp build(condition, do: do_clause, else: else_clause) do
  …
end

This gets formatted as:

defp (build(condition) do
        do_clause
      else
        else_clause
      end) do
…
end

It compiles, but it feels like a corner case that shouldn’t be formatted differently.

1 Like

You should create an issue so it gets the attention it deserves

1 Like