Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ Other ways Styler can change your program:
- [config file sorting](https://hexdocs.pm/styler/mix_configs.html#this-can-break-your-program)
- and likely other ways. stay safe out there!

>>>>>>> upstream/main
## Thanks & Inspiration

### [Sourceror](https://github.com/doorgan/sourceror/)
Expand Down
10 changes: 6 additions & 4 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ defmodule Styler.Style.Blocks do
* Credo.Check.Refactor.WithClauses
"""

@behaviour Styler.Style

alias Styler.Style
alias Styler.Zipper

@behaviour Styler.Style

defguardp is_negator(n) when elem(n, 0) in [:!, :not, :!=, :!==]

# case statement with exactly 2 `->` cases
Expand Down Expand Up @@ -143,9 +143,11 @@ defmodule Styler.Style.Blocks do
[head, [do_block, {_, {:__block__, _, []}}]] ->
{:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx}

# We like keeping `else: nil` even if it's the same effect due to the explicitness.
#
# drop `else: nil`
[head, [do_block, {_, {:__block__, _, [nil]}}]] ->
{:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx}
# [head, [do_block, {_, {:__block__, _, [nil]}}]] ->
# {:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx}

[head, [do_, else_]] ->
if Style.max_line(do_) > Style.max_line(else_) do
Expand Down
4 changes: 2 additions & 2 deletions lib/style/comment_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ defmodule Styler.Style.CommentDirectives do
`# styler:sort` maintains sorting of wordlists (by string comparison) and lists (string comparison of code representation)
"""

@behaviour Styler.Style

alias Styler.Style
alias Styler.Zipper

@behaviour Styler.Style

def run(zipper, ctx) do
{zipper, comments} =
ctx.comments
Expand Down
2 changes: 1 addition & 1 deletion lib/zipper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ defmodule Styler.Zipper do
"""
@spec prepend_siblings(zipper, [tree]) :: zipper
def prepend_siblings({node, nil}, siblings), do: {:__block__, [], siblings ++ [node]} |> zip() |> down() |> rightmost()
def prepend_siblings({tree, {l, p, r}}, siblings), do: {tree, {Enum.reverse(siblings, l), p , r}}
def prepend_siblings({tree, {l, p, r}}, siblings), do: {tree, {Enum.reverse(siblings, l), p, r}}

@doc """
Inserts the item as the right sibling of the node at this zipper, without
Expand Down
6 changes: 4 additions & 2 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,14 @@ defmodule Styler.Style.BlocksTest do
end

describe "if" do
test "drops else nil" do
assert_style("if a, do: b, else: nil", "if a, do: b")
test "keeps else nil" do
assert_style("if a, do: b, else: nil", "if a, do: b, else: nil")

assert_style("if a do b else nil end", """
if a do
b
else
nil
end
""")

Expand Down
1 change: 1 addition & 0 deletions test/style/comment_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

defmodule Styler.Style.CommentDirectivesTest do
@moduledoc false

use Styler.StyleCase, async: true

describe "sort" do
Expand Down
2 changes: 1 addition & 1 deletion test/zipper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule StylerTest.ZipperTest do
{1, {[], {{:foo, [], [1, 2]}, nil}, [2]}}

assert {{:., [], [:a, :b]}, [], [1, 2]} |> Zipper.zip() |> Zipper.down() ==
{{:., [], [:a, :b]}, {[],{{{:., [], [:a, :b]}, [], [1, 2]}, nil}, [1, 2]}}
{{:., [], [:a, :b]}, {[], {{{:., [], [:a, :b]}, [], [1, 2]}, nil}, [1, 2]}}
end
end

Expand Down