Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with statement: drop singleton identity else. closes #84 #85

Merged
merged 6 commits into from
Nov 13, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## main

### Improvements

* `with`: remove identity singleton else clause (eg `else {:error, e} -> {:error, e} end`, `else error -> error end`

## v0.10.1

### Fixes
Expand Down
48 changes: 27 additions & 21 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,35 @@ defmodule Styler.Style.Blocks do
{postroll, reversed_clauses} = Enum.split_while(reversed_clauses, &(not left_arrow?(&1)))
[{:<-, _, [lhs, rhs]} = _final_clause | rest] = reversed_clauses

# Credo.Check.Refactor.RedundantWithClauseResult
rewrite_body? = Enum.empty?(postroll) and Enum.empty?(elses) and nodes_equivalent?(lhs, do_body)
{_, do_body_meta, _} = do_body
# drop singleton identity else clauses like `else foo -> foo end`
elses =
case elses do
[{{_, _, [:else]}, [{:->, _, [[left], right]}]}] -> if nodes_equivalent?(left, right), do: [], else: elses
_ -> elses
end

{reversed_clauses, do_body} =
if rewrite_body?,
do: {rest, [rhs]},
else: {reversed_clauses, Enum.reverse(postroll, [do_body])}

do_else = [{do_block, {:__block__, do_body_meta, do_body}} | elses]
children = Enum.reverse(reversed_clauses, [do_else])

# only rewrite if it needs rewriting!
cond do
Enum.any?(preroll) ->
{:__block__, m, preroll ++ [{:with, m, children}]}

rewrite_body? or Enum.any?(postroll) ->
{:with, m, children}

true ->
with
end
cond do
# Put the postroll into the body
Enum.any?(postroll) ->
{_, do_body_meta, _} = do_body
do_body = {:__block__, do_body_meta, Enum.reverse(postroll, [do_body])}
{reversed_clauses, do_body}

# Credo.Check.Refactor.RedundantWithClauseResult
Enum.empty?(elses) and nodes_equivalent?(lhs, do_body) ->
{rest, rhs}

# no change
true ->
{reversed_clauses, do_body}
end

children = Enum.reverse(reversed_clauses, [[{do_block, do_body} | elses]])

if Enum.any?(preroll),
do: {:__block__, m, preroll ++ [{:with, m, children}]},
else: {:with, m, children}
else
# maybe this isn't a with statement - could be a function named `with`
# or it's just a with statement with no arrows, but that's too saddening to imagine
Expand Down
25 changes: 21 additions & 4 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ defmodule Styler.Style.BlocksTest do
""")
end

test "removes identity else clauses" do
assert_style(
"""
with :ok <- b(), :ok <- b() do
weeee()
:ok
else
:what -> :what
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure if i want to replace all equivalent nodes, or only variables. letting people specify patterns here at least tells the reader "the caller doesn't have to worry about anything but this value", whereas else error -> error end conveys 0 information and so can be trivially removed.

end
""",
"""
with :ok <- b(), :ok <- b() do
weeee()
:ok
end
"""
)
end

test "Credo.Check.Readability.WithSingleClause" do
assert_style(
"""
Expand Down Expand Up @@ -164,8 +183,6 @@ defmodule Styler.Style.BlocksTest do
a = bop
boop
:horay!
else
:error -> :error
end
"""
)
Expand All @@ -183,8 +200,6 @@ defmodule Styler.Style.BlocksTest do
a = bop
boop
:horay!
else
:error -> :error
end
"""
)
Expand All @@ -196,6 +211,8 @@ defmodule Styler.Style.BlocksTest do
with {:ok, a} <- foo(),
{:ok, b} <- bar(a) do
{:ok, b}
else
error -> error
end
""",
"""
Expand Down