[Proposal] Add auto-formatting of alias/import/use statements to mix format

150 views
Skip to first unread message

Tim Gent

unread,
Mar 18, 2022, 8:56:40 AM3/18/22
to elixir-lang-core
The Elixir style guide has guidance on how to organise module attributes, directives, and macros - https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering 

What do people think about applying these conventions as part of mix format?

Background - we have linting rules using credo which check these conventions, but no way to automatically apply fixes at the moment, which means it can be a bit painful to do that manually. Feels like mix format might be a good place for this to live.

Wojtek Mach

unread,
Mar 18, 2022, 9:01:28 AM3/18/22
to elixir-lang-core
I don't think the proposal is going to be accepted. A very important design goal of `mix format` is never to change the semantics of the code and changing the order of statements is changing semantics.

 --
 You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
 To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/598e7f82-aa62-45a6-ab33-4f5f3593befcn%40googlegroups.com.

Andrea Leopardi

unread,
Mar 18, 2022, 12:41:52 PM3/18/22
to elixir-lang-core
Hey Tim, thanks for the proposal.

As Wojtek mentioned, a strict goal of "mix format" is to never change the AST. This means that, in principle, we should not reorder statements.

In this particular case though, we have a much bigger issue: these statements might depend on each other. Take this snippet:

defmodule MyModule do
  @some_mod GenServer
  use @some_mod
end


You would not be able to reorder these statements without breaking things.

These kinds of checks belong in a linter, not a formatter, such as Credo (as you already mentioned). The fact that these changes are not applied automatically right now doesn't mean that they couldn't in the future 🙃. It might be useful for Credo to be able to apply some automatic fixes if it doesn't already have this ability.

Thanks for the proposal though!

Andrea

Tim Gent

unread,
Mar 21, 2022, 10:04:09 AM3/21/22
to elixir-lang-core
Many thanks for the responses guys! Makes total sense :)
Reply all
Reply to author
Forward
0 new messages