Skip to content

Commit

Permalink
add a regression test for what our cleanup fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 12, 2023
1 parent 1e328ad commit fd3474e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ 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
# 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} =
cond do
# Credo.Check.Refactor.RedundantWithClauseResult
Enum.empty?(postroll) and Enum.empty?(elses) and nodes_equivalent?(lhs, do_body) ->
{rest, rhs}

Expand All @@ -95,14 +102,7 @@ defmodule Styler.Style.Blocks do
{reversed_clauses, do_body}
end

# drop singleton identity else clauses like `else foo -> foo end`
new_elses =
case elses do
[{{_, _, [:else]}, [{:->, _, [[left], right]}]}] -> if nodes_equivalent?(left, right), do: [], else: elses
_ -> elses
end

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

if Enum.any?(preroll),
do: {:__block__, m, preroll ++ [{:with, m, children}]},
Expand Down
2 changes: 2 additions & 0 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -211,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

0 comments on commit fd3474e

Please sign in to comment.