Note Styler's only public API is its usage as a formatter plugin. While you're welcome to play with its internals, they can and will change without that change being reflected in Styler's semantic version.
Styler will now keep a user-designated list or wordlist (~w
sigil) sorted as part of formatting via the use of comments.
The intention is to remove comments to humans, like # Please keep this list sorted!
, in favor of comments to robots: # styler:sort
. Personally speaking, Styler is much better at alphabetical-order than I ever will be.
To use the new directive, put it on the line before a list or wordlist.
This example:
# styler:sort
[:c, :a, :b]
# styler:sort
~w(a list of words)
# styler:sort
@country_codes ~w(
en_US
po_PO
fr_CA
ja_JP
)
# styler:sort
a_var =
[
Modules,
In,
A,
List
]
Would yield:
# styler:sort
[:a, :b, :c]
# styler:sort
~w(a list of words)
# styler:sort
@country_codes ~w(
en_US
fr_CA
ja_JP
po_PO
)
# styler:sort
a_var =
[
A,
In,
List,
Modules
]
Sorting is done according to erlang term ordering, so lists with elements of multiple types will work just fine.
|>
don't pipify when the call is itself in a pipe (aka don't toucha |> b(c |> d() |>e()) |> f()
) (Closes #204, h/t @paulswartz)
pipes
: pipe-ifies when first arg to a function is a pipe. reach out if this happens in unstylish places in your code (Closes #133)pipes
: unpiping assignments will make the assignment one-line when possible (Closes #181)deprecations
: 1.18 deprecationsList.zip
=>Enum.zip
first..last = range
=>first..last//_ = range
pipes
: optimizations are less likely to move comments (Closes #176)
- Config Sorting: improve comment handling when only sorting a few nodes (Closes #187)
unless
: rewriteunless a |> b |> c
asunless !(a |> b() |> c())
rather thanunless a |> b() |> c() |> Kernel.!()
(h/t @gregmefford)
The big change here is the rewrite/removal of unless
due to unless "eventually" being deprecated. Thanks to @janpieper and @ypconstante for bringing this up in #190.
unless
: rewrite allunless
toif
(#190)pipes
: optimize|> Stream.{each|map}(fun) |> Stream.run()
to|> Enum.each(fun)
pipes
: optimizations reducing 2 pipes to 1 no longer squeeze all pipes onto one line (#180)if
: fix infinite loop rewriting negated if with empty do bodyif x != y, do: (), else: :ok
(#196, h/t @itamm15)
Styler's two biggest outstanding bugs have been fixed, both related to compilation breaking during module directive organization. One was references to aliases being moved above where the aliases were declared, and the other was similarly module directives being moved after their uses in module directives.
In both cases, Styler is now smart enough to auto-apply the fixes we recommended in the old Readme.
Other than that, a slew of powerful new features have been added, the neatest one (in the author's opinion anyways) being Alias Lifting.
Thanks to everyone who reported bugs that contributed to all the fixes released in 1.0.0 as well.
Along the lines of Credo.Check.Design.AliasUsage
, Styler now "lifts" deeply nested aliases (depth >= 3, ala A.B.C....
) that are used more than once.
Put plainly, this code:
defmodule A do
def lift_me() do
A.B.C.foo()
A.B.C.baz()
end
end
will become
defmodule A do
@moduledoc false
alias A.B.C
def lift_me do
C.foo()
C.baz()
end
end
To exclude modules ending in .Foo
from being lifted, add styler: [alias_lifting_exclude: [Foo]]
to your .formatter.exs
A long outstanding breakage of a first pass with Styler was breaking directives that relied on module attributes which Styler moved after their uses. Styler now detects these potential breakages and automatically applies our suggested fix, which is creating a variable before the module. This usually happened when folks were using a library that autogenerated their moduledocs for them.
In code, this module:
defmodule MyGreatLibrary do
@library_options [...]
@moduledoc make_pretty_docs(@library_options)
use OptionsMagic, my_opts: @library_options
...
end
Will now be styled like so:
library_options = [...]
defmodule MyGreatLibrary do
@moduledoc make_pretty_docs(library_options)
use OptionsMagic, my_opts: unquote(library_options)
@library_options library_options
...
end
Styler now organizes Mix.Config.config/2,3
stanzas according to erlang term sorting. This helps manage large configuration files, removing the "where should I put this" burden from developers AND helping find duplicated configuration stanzas.
See the moduledoc for Styler.Style.Configs
for more.
Enum.into(x, [])
=>Enum.to_list(x)
Enum.into(x, [], mapper)
=>Enum.map(x, mapper)
a |> Enum.map(m) |> Enum.join()
tomap_join(a, m)
. we already did this forjoin/2
, but missed the case forjoin/1
lhs |> Enum.reverse() |> Kernel.++(enum)
=>lhs |> Enum.reverse(enum)
- remove
with
structure with no left arrows in its head to be normal code (#174) with true <- x(), do: y
=>if x(), do: y
(#173)
if
/unless
: invert if and unless with!=
or!==
, like we do for!
andnot
#132@derive
: move@derive
beforedefstruct|schema|embedded_schema
declarations (fixes compiler warning!) #134- strings: rewrite double-quoted strings to use
~s
when there's 4+ escaped double-quotes ("\"\"\"\""
->~s("""")
) (Credo.Check.Readability.StringSigils
) #146 Map.drop(foo, [single_key])
=>Map.delete(foo, single_key)
#161 (also in pipes)Keyword.drop(foo, [single_key])
=>Keyword.delete(foo, single_key)
#161 (also in pipes)
- don't blow up on
def function_head_with_no_body_nor_parens
(#185, h/t @ypconstante) - fix
with
arrow replacement + redundant body removal creating invalid statements (#184, h/t @JesseHerrick) - allow Kernel unary
!
andnot
as valid pipe starts (#183, h/t @nherzing) - fix
Map.drop(x, [a | b])
registering as a chance to refactor toMap.delete
alias
: expands aliases when moving an alias after another directive that relied on it (#137)- module directives: various fixes for unreported obscure crashes
- pipes: fix a comment-shifting scenario when unpiping
Timex.now/1
will no longer be rewritten toDateTime.now!/1
due to Timex accepting a wider domain of "timezones" than the stdlib (#145, h/t @ivymarkwell)with
: skip nodes which (unexpectedly) do not contain ado
body (#158, h/t @DavidB59)then(&fun/1)
: fix false positives on arithmetic&1 + x / 1
(#164, h/t @aenglisc)
- drop support for elixir
1.14
- ModuleDirectives: group callback attributes (
before_compile after_compile after_verify
) with nondirectives (previously, were grouped withuse
, their relative order maintained). to keep the desired behaviour, you can make newuse
macros that wrap these callbacks. Apologies if this makes using Styler untenable for your codebase, but it's probably not a good tool for macro-heavy libraries. - sorting configs for the first time can change your configuration; see Mix Configs docs for more