⚡ How to auto-format functions without parentheses
Notes
Have you ever wondered how Phoenix and Ecto have functions whose arguments the Elixir formatter doesn’t automatically wrap with parentheses?
For example, look at Ecto.Schema
’s field
functions:
schema "users" do
field :name, :string
end
How is it that mix format
doesn’t turn field :name, :string
into
field(:name, :string)
?
Or take Phoenix routes:
live "/users", UserLive
How is it that mix format
doesn’t change that to live("/users", UserLive)
?
Those functions act like a domain-specific language, and it’s nice when they can be formatted without parentheses.
Did you know you can do that for functions in your own project? 😲
All you have to do is declare which functions (along with their arities) you
want to format without parentheses in your .formatter.exs
file!
[
import_deps: [:ecto, :ecto_sql, :phoenix],
# ... other stuff
locals_without_parens: [echo: 2]
]
We’ve just added our echo/2
function to locals_without_parens
so it doesn’t
get wrapped in parentheses when the Elixir formatter runs!
Go ahead and write echo/2
without parens to your heart’s content!
- echo("hello", "world")
+ echo "hello", "world"