Skip to content

Releases: adobe/elixir-styler

v1.7.0

12 Aug 20:44
Compare
Choose a tag to compare

1.7.0

Surprising how fast numbers go up when you're following semver.

Two new features, one being a pipe optimization and the other a style-consistency-enforcer in cond statements.

Improvements

  • |> Enum.filter(fun) |> List.first([default]) => |> Enum.find([default], fun) (#242, h/t @janpieper)

cond

If the last clause's left-hand-side is a truthy atom, map literal, or tuple, rewrite it to be true

# before
cond do
  a -> b
  c -> d
  :else -> e
end

# styled
cond do
  a -> b
  c -> d
  true -> e
end

This also helps Styler identify 2-clause conds that can be rewritten to if/else more readily, like the following:

# before
cond do
  a -> b
  :else -> c
end

# styled
if a do
  b
else
  c
end

v1.6.0: ExUnit assertions edition

28 Jul 20:15
Compare
Choose a tag to compare

That's right, a feature release again so soon!

Improvements

This version of Styler adds many readability improvements around ExUnit assert and refute, specifically when working with 1. negations or 2. some Enum stdlib functions.

Some of these rewrites are not semantically equivalent; for example, refute is_nil(false) will be rewritten to assert false, which will fail.

ExUnit assert/refute rewrites

Styler now inverts negated (!, not) assert/refute (eg assert !x => refute x) statements, and further inverts refute with boolean comparison operators (refute x < y => assert x >= y) because non-trivial refutes are harder to reason about [ citation needed ]. Asserting something is not nil is the same as just asserting that something, so that's gone too now.

These changes are best summarized by the following table:

before styled
assert !x refute x
assert not x refute x
assert !!x assert x
assert x != nil assert x
assert x == nil no change
assert is_nil(x) no change
assert !is_nil(x) assert x
assert x not in y refute x in y
refute negated
refute x no change
refute !x assert x
refute not x assert x
refute x != y assert x == y
refute x !== y assert x === y
refute x != nil assert x == nil
refute x not in y assert x in y
refute comparison
refute x < y assert x >= y
refute x <= y assert x > y
refute x > y assert x <= y
refute x >= y assert x < y
  • assert Enum.member?(y, x) -> assert x in y
  • assert Enum.find(x, y) -> assert Enum.any?(x, y) (nb. not semantically equivalent in theory, but equivalent in practice)
  • assert Enum.any?(y, & &1 == x) -> assert x in y
  • assert Enum.any?(y, fn var -> var == x end) -> assert x in y

Fixes

  • alias lifting: fix bug lifting in snippets with a single ast node at the root level (like a credo config file) (#240, h/t @defndaines)

v1.5.1

20 Jul 17:16
Compare
Choose a tag to compare

Fixes

  • alias lifting: handle comments in snippets with no existing directives (#239, h/t @kerryb)

v1.5.0

16 Jul 02:14
Compare
Choose a tag to compare

Improvements

  • apply aliases to code. if a module is aliased, and then later referenced with its full name, Styler will now shorten it to its alias. (#235, h/t me)
  • added :minimum_supported_elixir_version configuration to better support libraries using Styler (#231, h/t @maennchen)
  • # styler:sort will now sort keys for struct/map typespecs (#213, h/t @rojnwa)

Fixes

  • apply alias lifting to snippets with no modules or module directives in them. (#189, @h/t @halfdan)
  • fix de-sugaring of syntax-sugared keyword lists whose values weren't atoms in map values (#236, h/t @RisPNG)
  • fix mix config sorting mangling floating comment blocks in some cases (#230 again, h/t @ryoung786)

v1.4.2

08 May 05:14
Compare
Choose a tag to compare

Fixes

  • Fix comment misplacement for large comment blocks in config files and # styler:sort (#230, h/t @cschmatzler)

v1.4.1

26 Mar 16:27
Compare
Choose a tag to compare

Improvements

  • to_timeout/1 rewrites to use the next largest unit in some simple instances

    # before
    to_timeout(second: 60 * m)
    to_timeout(day: 7)
    # after
    to_timeout(minute: m)
    to_timeout(week: 1)

Fixes

  • fixed styler raising when encountering invalid function definition ast

v1.4.0

21 Feb 19:10
Compare
Choose a tag to compare

Improvements

Alias Lifting

This release taught Styler to try just that little bit harder when doing alias lifting.

  • general improvements around conflict detection, lifting in more correct places and fewer incorrect places (#193, h/t @jsw800)

  • use knowledge of existing aliases to shorten invocations (#201, h/t me)

    example:

      alias A.B.C
    
      A.B.C.foo()
    

    becomes:

      alias A.B.C
    
      C.foo()
    

Struct Updates => Map Updates

1.19 deprecates struct update syntax in favor of map update syntax.

# This
%Struct{x | y}
# Styles to this
%{x | y}

WARNING Double check your diffs to make sure your variable is pattern matching against the same struct if you want to harness 1.19's type checking features. Apologies to folks who hoped Styler would do this step for you <3 (#199, h/t @SteffenDE)

Ex1.17+

  • Replace :timer.units(x) with the new to_timeout(unit: x) for hours|minutes|seconds (This style is only applied if you're on 1.17+)

Fixes

  • pipes: handle pipifying when the first arg is itself a pipe: c(a |> b, d) => a |> b() |> c(d) (#214, h/t @kybishop)
  • pipes: handle pipifying nested functions d(c(a |> b)) => a |> b |> c() |> d (#216, h/t @emkguts)
  • with: fix a stabby with , else: (_ -> :ok) being rewritten to a case (#219, h/t @iamhassangm)

v1.3.3

21 Jan 23:55
Compare
Choose a tag to compare

Improvements

  • with do: body and variations with no arrows in the head will be rewritten to just body (or ...head statements; body)

  • # styler:sort will sort arbitrary ast nodes within a do end block:

    Given:

      # styler:sort
      my_macro "some arg" do
        another_macro :q
        another_macro :w
        another_macro :e
        another_macro :r
        another_macro :t
        another_macro :y
      end
    

    We get:

      # styler:sort
      my_macro "some arg" do
        another_macro :e
        another_macro :q
        another_macro :r
        another_macro :t
        another_macro :w
        another_macro :y
      end
    

Fixes

  • fix a bug in comment-movement when multiple # styler:sort directives are added to a file at the same time

v1.3.2

14 Jan 20:27
Compare
Choose a tag to compare

# styler:sort will sort key values, like:

# given 
%{
  z: ...,
  # styler:sort
  b: ~w(a list to be sorted)
}

# styled
%{
  z: ...,
  # styler:sort
  b: ~w(a be list sorted to)
}

v1.3.1

13 Jan 19:52
Compare
Choose a tag to compare
  • # styler: sort can now be used with defstruct and maps
  • # styler: sort doesn't blow up on keyword lists anymore :X